Borg Maze
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6124 | Accepted: 2055 |
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### #####
Sample Output
8 11
Source
今天过了训练2的最后一个题,先说说这个题吧 这个题只要是理清了思路,用bfs和最短路程序%99的就对了,但是确实很容易犯错误,因为这与长期以来我们编写程序的方式有关,我们在处理输入含有空格的字符的时候习惯用get(c语言) ,cin.get(c++), 但是这样最后的回车会作为一个字符,我们在处理的时候还是调用get,cin.get 来过滤掉这个回车。这是我们常用的方法,但是这种方法是有弊端的,比如 输入一个数n 代表输入n个字符,我们会这样输入
int n;
char c;
cin>>n;
cin.get();
for(i=1;i<=n;i++)
{
c=cin.get();
}
这是我们常用的方法吧,但是这样做有弊端,仔细想一想就会发现如果 再输入整数n之后没有接着按下回车,而是按空格。然后按回车,这就不对了,这题就是专门卡这一点,cin.get() 只能过滤掉一个字符。 所以在过滤的时候用gets(s1)《c语言》,getline(cin,s1)<C++> 这样就行了,以后再编写的程序的时候也这样写吧,毕竟我们不知道数据什么就会多出空格。
这个训练2的计划做起来还是比较吃力的,因为有最大流,这个新知识点,最大流主要就是建图,还有题目的题意很难理解。 不过最后还是挺过来了,终于完成了。
训练2做完了,也快考试了,可以安心的准备考试了。
#include <iostream>
#include <string.h>
using namespace std;
int a[100][100],b[120][120];
int c[120][4],res[120];
int status[120][120],queue[100000][2];
int vex[]={-1,1,0,0};
int vey[]={0,0,-1,1};
int INF=0x7fffffff;
int n,m;
string s1;
int main()
{
void bfs(int x,int y);
int i,j,s,t,k,min1,sum;
char d;
cin>>t;
while(t--)
{
cin>>m>>n;
getline(cin,s1);
s=2;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
d=cin.get();
if(d=='#')
{
a[i][j]=0;
}else if(d=='A')
{
a[i][j]=s;
c[s][0]=i; c[s][1]=j; s++;
}else if(d=='S')
{
a[i][j]=1;
c[1][0]=i; c[1][1]=j;
}else if(d==' ')
{
a[i][j]=-1;
}
}
cin.get();
}
for(i=1;i<=s-1;i++)
{
for(j=1;j<=s-1;j++)
{
b[i][j]=INF;
}
}
for(i=1;i<=s-1;i++)
{
bfs(c[i][0],c[i][1]);
}
s-=1;
for(i=1;i<=s;i++)
{
res[i]=b[1][i];
}
sum=0;
for(i=1;i<=s-1;i++)
{
min1=INF;
for(j=2;j<=s;j++)
{
if(min1>res[j]&&res[j]!=0)
{
min1=res[j];
k=j;
}
}
sum+=min1;
res[k]=0;
for(j=2;j<=s;j++)
{
if(res[j]>b[k][j])
{
res[j]=b[k][j];
}
}
}
cout<<sum<<endl;
}
return 0;
}
void bfs(int x,int y)
{
int i,j,base,top,posx,posy;
int xend,yend,sum[120][120];
posx=x; posy=y;
memset(status,0,sizeof(status));
memset(sum,0,sizeof(sum));
base=top=0;
queue[top][0]=x; queue[top++][1]=y;
status[x][y]=1;
while(base<top)
{
x=queue[base][0]; y=queue[base++][1];
for(i=0;i<=3;i++)
{
xend=x+vex[i]; yend=y+vey[i];
if(xend>=1&&xend<=n&¥d>=1&¥d<=m&&a[xend][yend]!=0&&!status[xend][yend])
{
sum[xend][yend]=sum[x][y]+1;
if(a[xend][yend]!=-1)
{
b[a[posx][posy]][a[xend][yend]]=sum[xend][yend];
}
queue[top][0]=xend; queue[top++][1]=yend;
status[xend][yend]=1;
}
}
}
}