poj 3026 Brog Maze(BFS+MST)
Borg Maze
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 6300 | Accepted: 2120 |
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.
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
好坑人的题目,输入行和列后竟然还有许多空格。
题目描述中说group只能在起点S处或alien处才能分开,实际上就是重复的路可以忽略,那么就可以转化成最小生成树问题,但问题在于存在’#‘,无法直接得到两点间的距离,只能把A和S看成是最小生成树里的点,用BFS得到两点间距离。
View Code
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct node { int u,v,w; }edge[6250000]; int n,m; int mycount,num; char ch[55][55]; int dx[4] = {0,0,1,-1}; int dy[4] = {1,-1,0,0}; int p[2550]; void bfs(int x, int y) { int vis[55][55]; memset(vis,0,sizeof(vis)); int i,j,k; int cx,cy,step; int que[25500][3]; vis[x][y] = 1; que[1][0] = x; que[1][1] = y; que[1][2] = 0; int head = 0; int rear = 1; while (head<rear) { head++; cx = que[head][0]; cy = que[head][1]; step = que[head][2]; for (k=0; k<4; k++) { i = cx + dx[k]; j = cy + dy[k]; if (i>=0 && j>=0 && i<m && j<n && !vis[i][j] && ch[i][j]!='#') { rear++; que[rear][0] = i; que[rear][1] = j; que[rear][2] = step+1; if (ch[i][j]=='A' || ch[i][j]=='S') { edge[mycount].u = x*m+y; edge[mycount].v = i*m+j; edge[mycount++].w = step+1; } vis[i][j] = 1; } } } } bool cmp(node a, node b) { if (a.w!=b.w) return a.w<b.w; if (a.u!=b.u) return a.u<b.u; return a.v<b.v; } int Find(int x){ if(x!=p[x]) p[x] = Find(p[x]); return p[x]; } void kruskal() { sort(edge,edge+mycount,cmp); int i,k=0; int sum = 0; for (i=0; i<m*n; i++) { p[i] = i; } for (i=0; i<mycount; i++) { if (k == num-1) { printf("%d\n",sum); return; } int x = Find(edge[i].u); int y = Find(edge[i].v); if (x!=y) { p[x] = y; k++; sum += edge[i].w; } } } int main() { // freopen("in.txt","r",stdin); int T; int i,j; scanf("%d",&T); while (T--) { scanf("%d %d",&n,&m); char temp[50]; gets(temp); for (i=0; i<m; i++) gets(ch[i]); mycount = 0; num = 0; for (i=0; i<m; i++) { for (j=0; j<n; j++) { if (ch[i][j]=='A' || ch[i][j] == 'S') { num++; bfs(i,j); } } } kruskal(); } return 0; }
posted on 2013-03-09 15:09 shijianupc 阅读(157) 评论(0) 收藏 举报

浙公网安备 33010602011771号