Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3842 | Accepted: 1271 |
Description
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
Output
Sample Input
26 5##### #A#A### # A##S ####### 7 7##### #AAA#### A## S #### ##AAA########
Sample Output
811
解题思路:BFS搜索地图,找出各个节点相对距离
prim求最小生成树,然后计算edge和。
这是一个非常蛋疼的题,在x,y之后有很多空格,导致wa了几次。
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #define MAX_VALUE 65535 #define debug 0 using namespace std; typedef struct { int x,y; int step; }NODE; int x,y; int number; int map[50][50]; NODE node[101]; //node[0] for start, node[1...100] for aliens int graph[101][101]; int d[101]; void prim() { int minValue, minNode; int visit[101]; int totalDist; fill(d,d+number+1,MAX_VALUE); memset(visit,0,sizeof(visit)); d[0]=0; totalDist=0; minNode=0; for(int i=0; i<=number; i++) { minValue=MAX_VALUE; for(int j=0; j<=number; j++) { if(!visit[j]&&d[j]<minValue) { minValue=d[j]; minNode=j; } } visit[minNode]=true; totalDist+=d[minNode]; for(int j=0; j<=number; j++) { if(!visit[j]&&d[j]>graph[minNode][j]) { d[j]=graph[minNode][j]; } } } printf("%d\n",totalDist); } void BFS() { queue<NODE> Q; bool visit[50][50]; //record map visited NODE tmpNode1,tmpNode2; int find; for(int i=0; i<=number; i++) // for all nodes, do BFS { for(int j=0; j<=number; j++) { node[j].step=0; } memset(visit,0,sizeof(visit)); find=0; tmpNode1.x=node[i].x; tmpNode1.y=node[i].y; tmpNode1.step=node[i].step; //BFS while(!Q.empty()) { Q.pop(); } Q.push(tmpNode1); visit[tmpNode1.x][tmpNode1.y]=true; while(!Q.empty() && find!=number) { tmpNode1=Q.front(); Q.pop(); if(tmpNode1.x-1>=0 && !visit[tmpNode1.x-1][tmpNode1.y] && map[tmpNode1.x-1][tmpNode1.y]!=-2) //left move { tmpNode2.x=tmpNode1.x-1; tmpNode2.y=tmpNode1.y; tmpNode2.step=tmpNode1.step+1; visit[tmpNode2.x][tmpNode2.y]=true; if(map[tmpNode2.x][tmpNode2.y]>=0) { find++; graph[i][map[tmpNode2.x][tmpNode2.y]]=tmpNode2.step; } Q.push(tmpNode2); } if(tmpNode1.x+1<x && !visit[tmpNode1.x+1][tmpNode1.y] && map[tmpNode1.x+1][tmpNode1.y]!=-2) //right move { tmpNode2.x=tmpNode1.x+1; tmpNode2.y=tmpNode1.y; tmpNode2.step=tmpNode1.step+1; visit[tmpNode2.x][tmpNode2.y]=true; if(map[tmpNode2.x][tmpNode2.y]>=0) { find++; graph[i][map[tmpNode2.x][tmpNode2.y]]=tmpNode2.step; } Q.push(tmpNode2); } if(tmpNode1.y-1>=0 && !visit[tmpNode1.x][tmpNode1.y-1] && map[tmpNode1.x][tmpNode1.y-1]!=-2) //up move { tmpNode2.x=tmpNode1.x; tmpNode2.y=tmpNode1.y-1; tmpNode2.step=tmpNode1.step+1; visit[tmpNode2.x][tmpNode2.y]=true; if(map[tmpNode2.x][tmpNode2.y]>=0) { find++; graph[i][map[tmpNode2.x][tmpNode2.y]]=tmpNode2.step; } Q.push(tmpNode2); } if(tmpNode1.y+1<y && !visit[tmpNode1.x][tmpNode1.y+1] && map[tmpNode1.x][tmpNode1.y+1]!=-2) //down move { tmpNode2.x=tmpNode1.x; tmpNode2.y=tmpNode1.y+1; tmpNode2.step=tmpNode1.step+1; visit[tmpNode2.x][tmpNode2.y]=true; if(map[tmpNode2.x][tmpNode2.y]>=0) { find++; graph[i][map[tmpNode2.x][tmpNode2.y]]=tmpNode2.step; } Q.push(tmpNode2); } } } } int main() { int N; char ch[52]; cin>>N; while(N--) { cin>>x>>y; number=0; cin.getline(ch,sizeof(ch));//吃掉空格。。。。eggache for(int i=0; i<y; i++) { cin.getline(ch,sizeof(ch)); ch[x]='\n'; for(int j=0; ch[j]!='\n'; j++) { switch(ch[j]) { case '#': map[i][j]=-2; break; case '\n': break; case ' ': map[i][j]=-1; break; case 'A': number++; map[i][j]=number; node[number].x=i; node[number].y=j; break; case 'S': map[i][j]=0; node[0].x=i; node[0].y=j; break; } } } #if debug for(int p=0; p<y; p++) { for(int q=0; q<x; q++) { printf("%d ", map[p][q]); } printf("\n"); } #endif BFS(); #if debug for(int p=0; p<=number; p++) { for(int q=0; q<=number; q++) { printf("%d ", graph[p][q]); } printf("\n"); } #endif prim(); } return 0; }