POJ 2632 Crashing Robots 模拟 难度:0
http://poj.org/problem?id=2632
#include<cstdio> #include <cstring> #include <algorithm> using namespace std; int A,B,n,m; int robot[101][3]; char rbuff[10]; int dir[255]; const int dx[4]={0,1,0,-1}; const int dy[4]={1,0,-1,0}; int action[101][3]; int tx,ty; int ansr; bool between(int aim,int gap){ int sx=robot[gap][0]; int sy=robot[gap][1]; int mingx=min(sx,tx); int maxgx=max(sx,tx); int mingy=min(sy,ty); int maxgy=max(sy,ty); int ax=robot[aim][0];int ay=robot[aim][1]; if(ax>=mingx&&ax<=maxgx&&ay>=mingy&&ay<=maxgy){ if(ansr==0)ansr=aim; else { if(abs(robot[ansr][0]-sx)>=abs(ax-sx)&&abs(robot[ansr][1]-sy)>=abs(ay-sy)){ ansr=aim; } } return true; } return false; } void solve(){ ansr=0; for(int i=0;i<m;i++){ int rob=action[i][0]; int rep=action[i][1]; if(action[i][2]==0){ robot[rob][2]=(robot[rob][2]+4-rep%4)%4; } else if(action[i][2]==1){ robot[rob][2]=(robot[rob][2]+rep%4)%4; } else{ bool fl=false; tx=robot[rob][0]+rep*dx[robot[rob][2]]; ty=robot[rob][1]+rep*dy[robot[rob][2]]; for(int j=1;j<=n;j++){ if(j==rob)continue; if(between(j,rob)){ fl=true; } } if(fl){ printf("Robot %d crashes into robot %d\n",rob,ansr);return ; } if(tx<1||tx>A||ty<1||ty>B){ printf("Robot %d crashes into the wall\n",rob);return ; } robot[rob][0]=tx; robot[rob][1]=ty; } } puts("OK"); } int main(){ dir['N']=0;dir['E']=1;dir['S']=2;dir['W']=3; dir['L']=0;dir['R']=1;dir['F']=2; int t; scanf("%d",&t); while(t--){ scanf("%d%d%d%d",&A,&B,&n,&m); for(int i=1;i<=n;++i){ scanf("%d%d%s",robot[i],robot[i]+1,rbuff); robot[i][2]=dir[rbuff[0]]; } for(int i=0;i<m;i++){ scanf("%d%s%d",action[i],rbuff,action[i]+1); action[i][2]=dir[rbuff[0]]; } solve(); } return 0; }