嗨,小朋友们,还记得我是谁吗?对了,我就是你们熟悉的B分之A。

首先,祝大家π节快乐~

今天我要跟你们分享一下,我今天AC的一道题的奇妙经历。

在AC完最苗条生成树后,我转往了下一题——POJ3026(我敢说我一辈子都他喵的会记住这个蛋疼的题号)

Borg Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11644 Accepted: 3824

Description

The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe the group consciousness of the Borg civilization. Each Borg individual is linked to the collective by a sophisticated subspace network that insures each member is given constant supervision and guidance.

Your task is to help the Borg (yes, really) by developing a program which helps the Borg to estimate the minimal cost of scanning a maze for the assimilation of aliens hiding in the maze, by moving in north, west, east, and south steps. The tricky thing is that the beginning of the search is conducted by a large group of over 100 individuals. Whenever an alien is assimilated, or at the beginning of the search, the group may split in two or more groups (but their consciousness is still collective.). The cost of searching a maze is definied as the total distance covered by all the groups involved in the search together. That is, if the original group walks five steps, then splits into two groups each walking three steps, the total distance is 11=5+3+3.

Input

On the first line of input there is one integer, N <= 50, giving the number of test cases in the input. Each test case starts with a line containg two integers x, y such that 1 <= x,y <= 50. After this, y lines follow, each which x characters. For each character, a space `` '' stands for an open space, a hash mark ``#'' stands for an obstructing wall, the capital letter ``A'' stand for an alien, and the capital letter ``S'' stands for the start of the search. The perimeter of the maze is always closed, i.e., there is no way to get out from the coordinate of the ``S''. At most 100 aliens are present in the maze, and everyone is reachable.

Output

For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.

Sample Input

2
6 5
##### 
#A#A##
# # A#
#S  ##
##### 
7 7
#####  
#AAA###
#    A#
# S ###
#     #
#AAA###
#####  

Sample Output

8
11

Source

这道题,考的比较综合啊。大致意思是说有一个蛋疼的迷宫里有一群蛋疼的外星人,而这群外星人要和其他不是他们种族的外星人搞基,从而让他们变成自己的种族,要求的就是要使这里面所有基佬外星人被同化的最小步数!

我他喵的一开始为他们会分组而感到蛋疼,然而更蛋疼的是我发现他们其实分不分都一样——b好吧这他喵是我的错,好吧?不予证明了,因为我他喵的根本不会(大雾)

好,既然都说的这么清楚了,那做法就他喵的非常明显了,就是一个BFS+MST,MST我选择的是惯用又好写的prim。

然后我开写了,写了大概加起来三个小时——b,其中用网上的数据发现了我的BFS有误,更正了后,嗯?完美嘛,网上的数据都能过,果断提交啦:

哎哟我擦?不会吧?回头一看,freopen删了啊,windows.h干掉了啊??

discuss一看,原来得开大一点:)

我开大了3倍啊我擦?!

难道是我程序有问题??

再进discuss,我他喵的发现了一件事情:

输入数据(x,y)有多余的空格!!!而且是一串!!!

然后,我用别的输入忽略掉了这串空格,提交之:AC!!

。 。 。 。 。 。

我严重鄙视这一题!!!

鲁棒性怪我咯???

不吐槽了,附上AC代码:

Source Code

Problem: 3026 User: aclolicon
Memory: 836K Time: 63MS
Language: G++ Result: Accepted
    • Source Code
#include<cstdio>
#include<iostream>
#include<queue>
#define MAXN 105
#define INF 0x7fffffff
using namespace std;
struct v{int x, y;};
v enemy[MAXN];
v r; 
int ecnt = -1;
char g[MAXN][MAXN];
int d1[4] = {1, -1, 0, 0};
int d2[4] = {0, 0, 1, -1};
bool used[MAXN];
int rg[MAXN][MAXN];
bool uj[MAXN][MAXN];
int mc[MAXN];
int jump[MAXN][MAXN]; 
int n, x, y;
queue<v> q;
void restore(){
	for (int i = 0; i < MAXN; i++)
		for (int j = 0; j < MAXN; j++){
			uj[i][j] = 0;
			jump[i][j] = INF; 
//			if(x) rg[i][j] = INF;
		} 

}
void solve(){	
	for (int e = 0; e <= ecnt; e++){
		restore();
		r.x = enemy[e].x;
		r.y = enemy[e].y;
		jump[r.x][r.y] = 0;
		uj[r.x][r.y] = 1;
		q.push(r);
//		cout << r.x << ',' << r.y << endl; 
		v add;
		while(!q.empty()){
			r = q.front();
			q.pop();
			for (int i = 0; i < 4; i++){
				add.x = r.x + d1[i];
				add.y = r.y + d2[i];

				if (!(add.x > (x - 1) || add.y > (y - 1) || add.x < 0 || add.y <0 || uj[add.x][add.y] || g[add.x][add.y] == '#')){ 
					jump[add.x][add.y] = min(jump[add.x][add.y], jump[r.x][r.y] + 1);
					q.push(add);
					uj[add.x][add.y] = 1;			
				}

			}
				 
		}
		for (int f = 0; f <= ecnt; f++){
			rg[e][f] =  jump[enemy[f].x][enemy[f].y];
		}
	}
 		for (int aa = 0; aa <= ecnt; aa++){
 			for (int bb = 0; bb <= ecnt; bb++){
 				rg[aa][bb] = min (rg[aa][bb], rg[bb][aa]); 
			}	
		 }
	mc[0] = 0;
	int u = -1;
	int res = 0;
	while(1){
		u = -1;
		for (int i = 0; i <= ecnt; i++) if (!used[i] && (u == -1 || mc[u] > mc[i])) u = i;
		if (u == -1) break;
		used[u] = 1;
		res += mc[u]; 
		for (int i = 0; i <= ecnt; i++) mc[i] = min(mc[i], rg[u][i]);
	}
	cout << res << endl;
}
bool init(){
	ecnt = -1;
	char bb[101];
	gets(bb); 
	sscanf(bb,"%d %d",&x,&y);
	if (x==0 || y==0) return 0;
	char a, b;
	for (int i = 0; i < MAXN; i++){
			used[i] = 0;
			mc[i] = INF;
			for (int j = 0; j < MAXN; j++)	
				jump[i][j] = INF;
	} 
	char cha;
	for (int i = 0; i < y; i++){
				scanf("%c",&cha);
			for (int j = 0; j < x; j++){
				scanf("%c",&cha);
				g[j][i] = cha; 
				if (cha == 'A' || cha == 'S'){

					ecnt++;
					enemy[ecnt].x = j;
					enemy[ecnt].y = i;
				}
								
			}
	}
	return 1;
	

}
int main(){
//	freopen("c.in","r",stdin);
//	freopen("c.out","w",stdout);
	cin >> n;
	n++;
	while(n--){
		if(init()) solve();
	}
	return 0;
}