poj1101 The Game

来源:互联网 发布:彩虹六号对局数据查询 编辑:程序博客网 时间:2024/04/30 03:55

 #include<iostream>
using namespace std;
#define MAX 2*60000

int turn[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct Point
{
 int x,y;
}queue[MAX],pre[6000];
bool mk[110][110];
int map[110][110];
int counts[110][110];
int n,m;

int BFS(int x,int y,int x2,int y2)
{
 int front=0,rear=0;
 int i;
 int a,b;
 memset(mk,false,sizeof(mk)); 
 memset(counts,0,sizeof(counts));
 mk[x][y]=true;
 queue[rear].x=x;
 queue[rear].y=y;
 rear++;
 while(front<rear)
 {
  x=queue[front].x;
  y=queue[front].y;
  front++;
  for(i=0;i<4;i++)
  {
   a=x+turn[i][0];
   b=y+turn[i][1];
   while(!mk[a][b] && map[a][b] && a>=0 && a<=m+1 && b>=0 && b<=n+1)
   {
    mk[a][b]=true;
    counts[a][b]=counts[x][y]+1;
    if(a==x2 && b==y2)
     return 1;
    queue[rear].x=a;
    queue[rear].y=b;
    rear++;
    a+=turn[i][0]; //别人的思想
    b+=turn[i][1];
   }
  }
 }
 return 0;
}

int main()
{
 int i,j,count=1;
 int x,y,x1,y1;
 int t1,t2,ant,cnt;
 bool flag;
 char ch;
 while(1)
 {
  scanf("%d%d",&n,&m);
  if(n==0 && m==0)
   break;
  for(i=0;i<=80;i++)
   for(j=0;j<=80;j++)
    map[i][j]=1;  
   for(i=1;i<=m;i++)
   {
    getchar();
    for(j=1;j<=n;j++)
    {
     while(scanf("%c",&ch)&& ch!='X' && ch!=' ');
     if(ch=='X')
      map[i][j]=0;
     
    }
   }
   i=1;
   flag=true;
   while(1)
   {
    scanf("%d%d%d%d",&x,&y,&x1,&y1);
    if(x==0 && y==0 && x1==0 && y1==0)
     break;
    cnt=0;
    t1=map[y][x];
    t2=map[y1][x1];
    map[y][x]=1;
    map[y1][x1]=1;
    if(flag)
    {
     printf("Board #%d:/n",count);
     flag=false;
    }
    ant=BFS(y,x,y1,x1);     
    if(ant)
    {

     printf("Pair %d: %d segments./n",i,counts[y1][x1]);
    }
    else
     printf("Pair %d: impossible./n",i);
    map[y][x]=t1;
    map[y1][x1]=t2;
    i++;
   }
   printf("/n");
   count++;
 }
 return 0;
}

 

 

 

原创粉丝点击