poj 3026 Borg Maze dfs+prim

来源:互联网 发布:java获取select的值 编辑:程序博客网 时间:2024/05/19 03:20
题目:

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

26 5##### #A#A### # A##S  ####### 7 7#####  #AAA####    A## S ####     ##AAA########  

Sample Output

811




题目类似最小生成树问题:注意点有两点,一是输入中坑爹的空格(感谢discuss老司机),二就是距离的处理,使用dfs。


代码:

//By Sean Chen#include<iostream>#include<cstring>#include<cstdio>#include<queue>#define inf 0x3f3f3f3fusing namespace std;struct data{int x,y;int step;      //data形用来记录每个'S''A'的位置};data point[110];int dis[110][110];char Map[60][60];char dist[110][110];char visit[110][110];int d[110];int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};void dfs(data start,int h,int w,int id)            //dfs求两点间最短距离{queue <data> q;q.push(start);while (q.size()){data temp1=q.front(),temp2;q.pop();for (int i=0;i<4;i++){temp2=temp1;temp2.x=temp1.x+dir[i][0];temp2.y=temp1.y+dir[i][1];if (temp2.x>0 && temp2.x<h &&temp2.y>0 && temp2.y<w&&Map[temp2.x][temp2.y]!='#' && !visit[temp2.x][temp2.y]){temp2.step++;visit[temp2.x][temp2.y]=1;if (Map[temp2.x][temp2.y]!=' '){dis[id][dist[temp2.x][temp2.y]]=temp2.step;}q.push(temp2);}}}}void prim(int n){for (int i=1;i<n;i++){d[i]=dis[0][i];}d[0]=0;int result=0;for (int i=1;i<n;i++){int M=inf;int temp;for (int i=1;i<n;i++){if (M>d[i] && d[i]!=0){M=d[i];temp=i;}}if (M==inf)break;result+=M;d[temp]=0;for (int i=0;i<n;i++){if (dis[i][temp]<d[i]){d[i]=dis[i][temp];}}}printf("%d\n",result);}int main(){char c,b[100];int T;gets(b);sscanf(b,"%d",&T); while (T--){memset(Map,0,sizeof(Map));memset(point,0,sizeof(point));memset(dist,-1,sizeof(dist));memset(dis,0,sizeof(dis));int w,h,cnt=0;gets(b);sscanf(b,"%d %d",&w,&h);      //对输入数据的处理!!!行末有空格,整行读入。for (int i=0;i<h;i++)        {            gets(Map[i]);            for (int j=0;j<w;j++)            {                if (Map[i][j]=='S' ||Map[i][j]=='A')          //记录每个'S''A'的位置                {                    point[cnt].x=i;                    point[cnt].y=j;                    point[cnt].step=0;                    dist[i][j]=cnt;                    cnt++;                }            }}for (int i=0;i<cnt;i++)        {memset(visit,0,sizeof(visit));visit[point[i].x][point[i].y]=1;dfs(point[i],h,w,i);}prim(cnt);}return 0;}

0 0