Borg Maze
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12758 | Accepted: 4159 |
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
2 6 5 ##### #A#A## # # A# #S ## ##### 7 7 ##### #AAA### # A# # S ### # # #AAA### #####
Source
一道恨水的题 被自己做成如此狗样 心好累,还是辛苦了学长帮我 调了出来 也算是 长了知识了 当要用gets()的时候 前面要用getchar()吃掉换行 原谅我我真得不知道,另外还需要注意 变量名QAQ,因为变量名 错了多少了........ 还是太弱了 求解救.......
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
int x,y;
int step;
} s,w;
int m,n;//m表示列n表示行
char map[300][300];//储存地图
int dis[300][300];//记录两个字母之间的距离
int book[300][300];//标志
int wz[300][300];//记录字母的位置 记下它的编号
int dx[4]= {-1,0,1,0};
int dy[4]= {0,1,0,-1};
int minn;
int diss[300];
int vis[300];
int sum;
int pd(int x,int y)//判断是否合法
{
if(x>=0&&y>=0&&x<n&&y<m&&!book[x][y]&&map[x][y]!='#')
return 1;
return 0;
}
void bfs(int x,int y)
{
int i,j,flag;
queue<node>q;
memset(book,0,sizeof(book));
s.x=x;
s.y=y;
s.step=0;
book[x][y]=1;
q.push(s);
while(!q.empty())
{
s=q.front();
q.pop();
if(map[s.x][s.y]=='A'||map[s.x][s.y]=='S')
{
dis[wz[x][y]][wz[s.x][s.y]]= dis[wz[s.x][s.y]][wz[x][y]]=s.step;
}
for(i=0; i<4; i++)
{
int nx=s.x+dx[i];
int ny=s.y+dy[i];
if(pd(nx,ny))
{
w.x=nx;
w.y=ny;
w.step=s.step+1;
book[nx][ny]=1;
q.push(w);
}
}
}
}
void prim(int ans)
{
sum=0;
int i,j,u;
memset(vis,0,sizeof(vis));
for( i = 1;i<=ans;i++)
diss[i]=dis[1][i];
vis[1]=1;
//printf("%d\n",sum);
for(i=1;i<ans;i++)
{
minn=INF;
for(j=1;j<=ans;j++)
{
if(!vis[j]&&diss[j]<minn)
{
minn=diss[j];
u=j;
}
}
vis[u]=1;
sum+=minn;
for(int k=1;k<=ans;k++)
{
if(!vis[k]&&diss[k]>dis[u][k])
{
diss[k]=dis[u][k];
}
}
}
printf("%d\n",sum);
}
int main()
{
int t,i,j,ans;
scanf("%d",&t);
while(t--)
{
memset(wz,0,sizeof(wz));
memset(dis,0,sizeof(book));
memset(diss,0,sizeof(diss));
memset(map,0,sizeof(map));
scanf("%d%d",&m,&n);
//getchar();
gets(map[0]);
for(i=0; i<n; i++) //输入地图
gets(map[i]);
ans=0;
for(i=0; i<n; i++) //找到字母 并记录位置 和序号
for(j=0; j<m; j++)
{
if(map[i][j]=='A'||map[i][j]=='S')
wz[i][j]=++ans;//记录序号
}
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
if(wz[i][j]!=0)//如果是字母 就bfs寻找到其余点的距离
bfs(i,j);
}
}
// printf("%d\n",ans);
prim(ans);
//for(i=1; i<=ans; i++)
// for(j=1; j<=ans; j++)
// printf("%d %d %d\n",i,j,dis[i][j]);
// fflush(stdin);
}
return 0;
}