The Donkey of Gui Zhou(模拟题 hdu 4740)

题意:驴和老虎在方格中跑,跑的方式:径直跑,若遇到边界或之前走过的点则转向,驴向右转,虎向左转,若转向后还不能跑则一直呆着不动,

问:他们是否会相遇,会输出相遇坐标,不会输出-1

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define maxn 1005
#define inf 0x3f3f3f3f
#include <cstring>
#include <vector>
#define LL long long
#define MOD 100000007
using namespace std;
int N,R1,C1,D1,R2,C2,D2;
int dx[4]={0,1,0,-1};//东南西北四个方向
int dy[4]={1,0,-1,0};
bool book1[maxn][maxn];
bool book2[maxn][maxn];
bool check(int x,int y)//检查是否越界
{
   if(x<0||y<0||x>=N||y>=N)
      return false;
   return true;
}
bool check2(int x1,int y1,int x2,int y2)//检验两个坐标是否相同
{
   if(x1==x2&&y1==y2)
      return true;
   return false;
}
int main()
{
   ios::sync_with_stdio(false);
   while(cin>>N&&N)
   {
      cin>>R1>>C1>>D1;
      cin>>R2>>C2>>D2;
      int x1,y1,x2,y2,tx1,tx2,ty1,ty2;
      x1=R1;y1=C1;
      x2=R2;y2=C2;
      int flag1,flag2,flagz;
      flag1=flag2=flagz=0;
      memset(book1,false,sizeof(book1));
      memset(book2,false,sizeof(book2));
      while(1)
      {
         if(check2(x1,y1,x2,y2))
         {
            flagz=1;
            break;
         }
         if(flag1&&flag2)
            break;
         book1[x1][y1]=true;
         book2[x2][y2]=true;
         if(!flag1)
         {
            tx1=x1+dx[D1];
            ty1=y1+dy[D1];
            if(check(tx1,ty1)&&!book1[tx1][ty1])
            {
               x1=tx1;
               y1=ty1;
            }
            else
            {
               tx1=x1+dx[(D1+1)%4];
               ty1=y1+dy[(D1+1)%4];
               if(check(tx1,ty1)&&!book1[tx1][ty1])
               {
                  x1=tx1;
                  y1=ty1;
                  D1=(D1+1)%4;
               }
               else
               {
                  flag1=1;
               }
            }
         }
         if(!flag2)
         {
            tx2=x2+dx[D2];
            ty2=y2+dy[D2];
            if(check(tx2,ty2)&&!book2[tx2][ty2])
            {
               x2=tx2;
               y2=ty2;
            }
            else
            {
               tx2=x2+dx[(D2+3)%4];
               ty2=y2+dy[(D2+3)%4];
               if(check(tx2,ty2)&&!book2[tx2][ty2])
               {
                  x2=tx2;
                  y2=ty2;
                  D2=(D2+3)%4;
               }
               else
               {
                  flag2=1;
               }
            }
         }

          //cout << x1 << ' ' << y1 << endl;
           //cout << x2 << ' ' << y2 << endl;
      }
      if(flagz)
      {
         cout << x1 << ' ' << y1 << endl;
      }
      else
         cout << -1 << endl;
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/wwwlps/article/details/81197960