poj 2026 Borg Maze
来源:互联网 发布:浅谈人工智能 编辑:程序博客网 时间:2024/05/21 02:50
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
26 5##### #A#A### # A##S ####### 7 7##### #AAA#### A## S #### ##AAA########
Sample Output
811
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <queue>#include <stack>#include <algorithm>#define LL long long#define INF 0x3f3f3f3f#define MAX 51using namespace std;struct node{ int x,y; int step;};char str[MAX][MAX];int ant;int Map[MAX*MAX][MAX*MAX];int a[MAX][MAX];int eveco[MAX*MAX];bool vis[MAX*MAX];bool flag[MAX][MAX];int n,m;int top;int Dir[][2] = {{0,1},{1,0},{-1,0},{0,-1}};void BFS(int x,int y){ memset(flag,false,sizeof(flag)); queue<node >que; node b,c; b.x = x; b.y = y; b.step = 0; flag[x][y] = true; que.push(b); while(!que.empty()) { b = que.front(); que.pop(); if(a[b.x][b.y] != 0) { Map[a[x][y]][a[b.x][b.y]] = b.step; } for(int i=0; i<4; i++) { c.x = b.x + Dir[i][0]; c.y = b.y + Dir[i][1]; c.step = b.step + 1; if(!flag[c.x][c.y] && str[c.x][c.y] != '#') { flag[c.x][c.y] = true; que.push(c); } } }}int Prim(){ int cost = 0; memset(vis,false,sizeof(vis)); for(int i=1; i<=top; i++) { eveco[i] = Map[1][i]; } eveco[1] = 0; vis[1] = true; for(int i=1; i<top; i++) { int m = INF; int pos; for(int j=1; j<=top; j++) { if(!vis[j] && m > eveco[j]) { pos = j; m = eveco[j]; } } cost += m; vis[pos] = true; for(int j=1; j<=top; j++) { if(!vis[j] && eveco[j] > Map[pos][j]) { eveco[j] = Map[pos][j]; } } } return cost;}int main(){ //freopen("in.txt","r",stdin); int T; scanf("%d",&T); while(T--) { top = 0; memset(a,0,sizeof(a)); scanf("%d%d",&n,&m); gets(str[0]); for(int i=1; i<=n; i++) { gets(str[i]); for(int j=1; j<=m; j++) { if(str[i][j] == 'S' || str[i][j] == 'A') { a[i][j] = ++top; } } }// for(int i=1;i<=top;i++)// {// for(int j=1;j<=top;j++)// {// if(j == top)// cout<<a[i][j]<<endl;// else// cout<<a[i][j]<<" ";// }// } for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { if(a[i][j] != 0) BFS(i,j); } } printf("%d\n",Prim()); } return 0;}
0 0
- poj 2026 Borg Maze
- poj 3026 Borg Maze
- POJ 3026 Borg Maze
- POJ 3026 Borg Maze
- poj 3026 Borg Maze
- poj-3026-Borg Maze
- POJ 3026 Borg Maze
- POJ 3026 Borg Maze
- poj 3026 Borg Maze
- POJ-3026-Borg Maze
- poj 3026 Borg Maze
- POJ 3026 Borg Maze
- POJ 3026 Borg Maze
- poj 3026 Borg Maze
- poj 3026 Borg Maze
- POJ:3026 Borg Maze
- poj 3026 Borg Maze
- POJ 3026-Borg Maze
- 数据结构---单链表(3)
- 又跳了一天,懈怠无时无刻不在侵蚀我啊!!
- Linux_历史
- POJ2385Apple Catching【dp】
- hdu-1213 How Many Tables
- poj 2026 Borg Maze
- java的语法学习及小小编程(7.30)
- 嵌入式软件开发——嵌入式软件常见的笔试题(c)
- 用表单实现用户注册
- 第三天!!!!
- 【问与答】开复老师:硕士毕业后工作,还是出国读博?
- hdu-1272 小希的迷宫
- 提高ListView的性能
- hdu-1232 畅通工程