poj3026

Borg Maze

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12952   Accepted: 4227

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

题目大意:从S点出发,把图中所有A字符连通的最短路径

思路:因为连通所有字符,想到用Prim算法,构造最小生成树,但是我们需要各个点的距离关系所以再用bfs求各个点的之间的距离。注意的是不要一个一个的求,否则很可能会超时,把一个点到其他所有点的距离一次求完,也就是每一次都遍历整个图

代码如下:
  1 #include <iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<queue>
  5 using namespace std;
  6 const int INF = 0x3f3f3f3f;
  7 const int maxs = 305;
  8 char a[maxs][maxs];
  9 struct Point
 10 {
 11     int col;
 12     int row;
 13     int step;
 14 }node[maxs];
 15 int col,row,nums;//nums需要被连通的所有点的个数
 16 int edge[maxs][maxs];
 17 int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上下左右
 18 bool judge(Point point)
 19 {
 20     if(point.col>0&&point.col<=col&&point.row>0&&point.row<=row&&a[point.row][point.col]!=‘#‘)
 21         return true;
 22     return false;
 23 }
 24
 25 void bfs(int i)
 26 {
 27     bool vis2[maxs][maxs];
 28     int dist[maxs][maxs];//用来打表
 29     memset(vis2,false,sizeof(vis2));
 30     queue<Point> q;
 31     node[i].step=0;
 32     q.push(node[i]);
 33     vis2[node[i].row][node[i].col]=true;
 34     Point cur,next;
 35     while(!q.empty())
 36     {
 37         cur = q.front();
 38         q.pop();
 39         for(int k=0;k<4;k++)
 40         {
 41             next.row=cur.row+dir[k][0];
 42             next.col = cur.col+dir[k][1];
 43             if(!vis2[next.row][next.col]&&judge(next))
 44             {
 45                 next.step=cur.step+1;
 46                 vis2[next.row][next.col]=true;
 47                 q.push(next);
 48                 if(a[next.row][next.col]==‘A‘)
 49                     dist[next.row][next.col]=next.step;
 50             }
 51         }
 52     }
 53     for(int j=2;j<=nums;j++)
 54     {
 55         int d = dist[node[j].row][node[j].col];
 56         edge[i][j]=d;
 57         edge[j][i]=d;
 58     }
 59 }
 60 int prim()
 61 {
 62     bool vis[maxs];
 63     memset(vis,false,sizeof(vis));
 64     vis[1]=true;
 65     int dist[maxs],ans=0;
 66     for(int i=1;i<=nums;i++)
 67         dist[i]=edge[1][i];
 68     for(int i=2;i<=nums;i++)
 69     {
 70         int mins = INF,k=1;
 71         for(int j=1;j<=nums;j++)
 72             if(!vis[j]&&dist[j]<mins)
 73             {
 74                 mins = dist[j];
 75                 k=j;
 76             }
 77         if(mins!=INF)
 78             ans+=mins;
 79         vis[k]=true;
 80         for(int j=1;j<=nums;j++)
 81             if(!vis[j]&&dist[j]>edge[k][j])
 82                 dist[j]=edge[k][j];
 83     }
 84     return ans;
 85 }
 86 int main()
 87 {
 88     freopen("in.txt","r",stdin);
 89     int T;
 90     scanf("%d",&T);
 91     while(T--)
 92     {
 93         nums=1;
 94         memset(node,0,sizeof(node));
 95         memset(a,0,sizeof(a));
 96         scanf("%d%d",&col,&row);
 97         char s[2];
 98         for(int i=1;i<=row;i++)
 99         {
100             gets(s);
101             for(int j=1;j<=col;j++)
102             {
103                 scanf("%c",&a[i][j]);
104                 if(a[i][j]==‘S‘)
105                 {
106                     node[1].row=i;node[1].col=j;
107                 }
108                 else if(a[i][j]==‘A‘)
109                 {
110                     node[++nums].row=i;node[nums].col=j;
111                 }
112             }
113         }
114         for(int i=1;i<=nums;i++)
115         {
116             edge[i][i]=0;
117             bfs(i);
118         }
119         printf("%d\n",prim());
120     }
121     return 0;
122 }
 
时间: 2024-10-03 23:27:08

poj3026的相关文章

POJ3026——Borg Maze(BFS+最小生成树)

Borg Maze DescriptionThe 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

POJ3026 最小生成树

问题: POJ3026 分析: 采用BFS算出两两之间的距离,再用PRIM算法计算最小生成树. AC代码: 1 //Memory: 220K Time: 32MS 2 #include <iostream> 3 #include <cstdio> 4 #include <string> 5 #include <cstring> 6 #include <queue> 7 8 using namespace std; 9 10 const int m

快速切题 poj3026

感受到出题人深深的~恶意 这提醒人们以后...数字后面要用gets~不要getchar 此外..不要相信那个100? Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8524   Accepted: 2872 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant o

poj3026(Borg Maze)

题目大意: 在银河系中有一强大生物个体Borg,每个个体之间都有一种联系.让我们帮忙写个程序扫描整个迷宫并同化隐藏在迷宫的相异个体的最小代价.A 代表相异个体.空格代表什么没有,#代表障碍,S为开始点.扫描可以上下左右.测试数据: 2 6 5 ##### #A#A## # # A# #S ## ##### 7 7 ##### #AAA### # A# # S ### # # #AAA### ##### 解题思路: 简化题意就是从最少需要多少步将S和所有的A联系起来.第一组事例:S->A(1,1)

POJ-3026 Borg Maze---BFS预处理+最小生成树

题目链接: https://vjudge.net/problem/POJ-3026 题目大意: 在一个y行 x列的迷宫中,有可行走的通路空格' ',不可行走的墙'#',还有两种英文字母A和S,现在从S出发,要求用最短的路径L连接所有字母,输出这条路径L的总长度. 思路: 先BFS预处理出所有的字母之间的距离,然后用prim模板 超级坑的是这里得用gets()吃掉回车符,用getchar()会WA 1 #include<iostream> 2 #include<cstdio> 3 #

poj3026(bfs+prim)

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

POJ3026 Borg Maze(Prim)(BFS)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12729   Accepted: 4153 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 desc

POJ3026(BFS + prim)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10554   Accepted: 3501 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 desc

acm常见算法及例题

转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法