POJ
来源:互联网 发布:金达莱花dj网络歌手 编辑:程序博客网 时间:2024/06/05 21:33
题意:
求把所有的A和S都经过的最小长度。bfs+最小生成树。
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.
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.
For every test case, output one line containing the minimal cost of a succesful search of the maze leaving no aliens alive.
26 5##### #A#A### # A##S ####### 7 7##### #AAA#### A## S #### ##AAA########
811
#include <algorithm>#include <iostream>#include <numeric>#include <cstring>#include <iomanip>#include <string>#include <vector>#include <cstdio>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>#define LL long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const LL Max = 1005;const double esp = 1e-6;//const double PI = acos(-1);const int INF = 0x3f3f3f3f;using namespace std;char arr[60][60];bool vis[60][60];struct node{ int x,y,step;}cnt,ant;int Map[105][105],n,m,top;int dis[105],pos[60][60];bool Vis[105];int dx[] = {0,0,1,-1};int dy[] = {1,-1,0,0};void bfs(int x,int y){ queue<node>q; memset(vis,0,sizeof(vis)); while(!q.empty()) q.pop(); node s; s.x=x; s.y=y; s.step=0; q.push(s); vis[s.x][s.y]=1; while(!q.empty()) { node temp=q.front(); q.pop(); if(arr[temp.x][temp.y]=='A'||arr[temp.x][temp.y]=='S') { Map[pos[x][y]][pos[temp.x][temp.y]]=Map[pos[temp.x][temp.y]][pos[x][y]]=temp.step; } for(int i=0;i<4;i++) { node temp1; temp1.x=temp.x+dx[i]; temp1.y=temp.y+dy[i]; if(temp1.x>=0&&temp1.x<m&&temp1.y>=0&&temp1.y<n&&!vis[temp1.x][temp1.y]&&arr[temp1.x][temp1.y]!='#') { vis[temp1.x][temp1.y]=1; temp1.step=temp.step+1; q.push(temp1); } } }}int Prim(int x){ memset(Vis,false,sizeof(Vis)); for(int i=1;i<top;i++) dis[i] = Map[1][i]; int ans = 0; Vis[x] = true; for(int i=1;i<top-1;i++){ int qmin = INF,k = i; for(int j=1;j<top;j++){ if(!Vis[j] && dis[j] < qmin){ qmin = dis[j]; k = j; } } ans += qmin; Vis[k] = true; for(int j=1;j<top;j++){ if(!Vis[j] && dis[j] > Map[k][j]){ dis[j] = Map[k][j]; } } } return ans;}int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d %d\n",&n,&m); top = 1; for(int i=0;i<m;i++){ gets(arr[i]); for(int j=0;j<n;j++){ if(arr[i][j] == 'S' || arr[i][j] == 'A'){ pos[i][j] = top; top += 1; } } } for(int i=0;i<=top;i++) for(int j=0;j<=top;j++) if(i == j) Map[i][j] = 0; else Map[i][j] = INF; for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(arr[i][j] == 'S' || arr[i][j] == 'A'){ bfs(i,j); } } } printf("%d\n",Prim(1)); } return 0;}
阅读全文
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- 线性代数基础知识
- struts2漏洞原理及解决办法
- Hibernate初识
- yii2一个页面两个验证码校验问题
- POJ2559 Largest Rectangle in a Histogram(比动态规划更快的方法——单调队列)
- POJ
- leetcode 658. Find K Closest Elements
- (Ubuntu系列)——项目短期学习目标
- js实现跨域
- 51nod 1344 走格子
- SVG学习--VectorDrawable的使用
- (java)实现发送email
- 【HDU 6119 小小粉丝度度熊】 & 尺取
- GateServer网关服务器(数据流切成包转发到相应地址)