Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12346 | Accepted: 4046 |
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
2 6 5 ##### #A#A## # # A# #S ## ##### 7 7 ##### #AAA### # A# # S ### # # #AAA### #####
Sample Output
8 11
題目大意
在一個y行 x列的迷宮中,有可行走的通路空格’ ‘,不可行走的牆’#’,還有兩種英文字母A和S,現在從S出發,要求用最短的路徑L連接所有字母,輸出這條路徑L的總長度。
題意明白之後就是解題方法了,最短的路徑我們可以通過BFS找出,最小生成樹可以使用Prim
坑點是數據的任意一行的最後都有一個空格,所以scanf("%d\n");的時候一定要加一個\n將多餘的空格過濾掉,字符串就用gets就可以了。
#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|1
const 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,z;
}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 cal(int x,int y){ //搜索找出所有字母之間的最短距離
memset(vis,false,sizeof(vis));
queue<node>Q;
ant.x = x;ant.y = y;ant.z = 0;
vis[ant.x][ant.y] = true;
Q.push(ant);
while(!Q.empty()){
ant = Q.front();Q.pop();
if(arr[ant.x][ant.y] == 'A' || arr[ant.x][ant.y] == 'S'){
Map[Pos[x][y]][Pos[ant.x][ant.y]] = Map[Pos[ant.x][ant.y]][Pos[x][y]] = ant.z;
}
for(int i=0;i<4;i++){
int nowx = ant.x + dx[i];
int nowy = ant.y + dy[i];
if(nowx >= 0 && nowx < m && nowy >= 0 && nowy < n && arr[nowx][nowy]!='#' && !vis[nowx][nowy]){
vis[nowx][nowy] = true;
cnt.x = nowx;cnt.y = nowy;cnt.z = ant.z + 1;
Q.push(cnt);
}
}
}
}
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=2;i<top;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'){
cal(i,j);
}
}
}
printf("%d\n",Prim(1));
}
return 0;
}