HDU 4740 模拟题意
九野的博客,转载请注明出处:http://blog.csdn.net/acmmmm/article/details/11711743
题意:驴和老虎在方格中跑,跑的方式:径直跑,若遇到边界或之前走过的点则转向,驴向右转,虎向左转,若转向后还不能跑则一直呆着不动,
问:他们是否会相遇,会输出相遇坐标,不会输出-1
#include <iostream> #include <algorithm> #include <cstdlib> #include <cstring> #include <string> #include <vector> #include <cstdio> #include <queue> #include <cmath> #include <string.h> #include <assert.h> #include <stack> #include <sstream> #include <map> #include <set> #define M 1020 #define LL __int64 using namespace std; bool vis1[M][M]; bool vis2[M][M]; int dx[4]={0,1,0,-1}; int dy[4]={1,0,-1,0}; int main() { int i,j,x1,y1,z1,x2,y2,z2,xx1,yy1,xx2,yy2,n; bool flag,ok1,ok2; while(scanf("%d",&n),n) { scanf("%d%d%d",&x1,&y1,&z1); scanf("%d%d%d",&x2,&y2,&z2); memset(vis1,false,sizeof(vis1)); memset(vis2,false,sizeof(vis2)); ok1=true; ok2=true; flag=false; while(1) { if(x1==x2&&y1==y2) { flag=true; break; } if(!ok1&&!ok2) break; vis1[x1][y1]=true; vis2[x2][y2]=true; if(ok1) { xx1=x1+dx[z1]; yy1=y1+dy[z1]; if(xx1>=0&&xx1<n&&yy1>=0&&yy1<n&&!vis1[xx1][yy1]) { x1=xx1; y1=yy1; z1=z1; } else { xx1=x1+dx[(z1+1)%4]; yy1=y1+dy[(z1+1)%4]; if(xx1>=0&&xx1<n&&yy1>=0&&yy1<n&&!vis1[xx1][yy1]) { x1=xx1; y1=yy1; z1=(z1+1)%4; } else { x1=x1; y1=y1; z1=z1; ok1=false; } } } if(ok2) { xx2=x2+dx[z2]; yy2=y2+dy[z2]; if(xx2>=0&&xx2<n&&yy2>=0&&yy2<n&&!vis2[xx2][yy2]) { x2=xx2; y2=yy2; z2=z2; } else { xx2=x2+dx[((z2-1)%4+4)%4]; yy2=y2+dy[((z2-1)%4+4)%4]; if(xx2>=0&&xx2<n&&yy2>=0&&yy2<n&&!vis2[xx2][yy2]) { x2=xx2; y2=yy2; z2=((z2-1)%4+4)%4; } else { x2=x2; y2=y2; z2=z2; ok2=false; } } } } if(flag) { printf("%d %d\n",x1,y1); } else { printf("-1\n"); } } return 0; }