poj.2632模拟

来源:互联网 发布:网络配音cv大神 编辑:程序博客网 时间:2024/05/19 02:44

本题的意思是:给定一个a*b的矩阵,矩阵中的n个机器在矩阵中的位置和方位,m个步骤,每个步骤分为机器的序号+步骤的种类+步骤执行的次数,要求刚开始时机器不能重合,即给定的位置不能相等,并且每个指令时依次完成的,不能同时执行两个指令,而指令种类分L:向左转90度,R:向右转90度;F:向前行进一步,即一个方格。现要求判断执行所有步骤的时候是否有相撞或撞墙,如果有则输出结束,否则就输出“OK",这道题目其实思路很简单,就是一步一步的执行所给的步骤,麻烦就在于方向和指令各不同,那么每执行一个步骤就组合出4(方向)*3(指令的种类)种可能,另外就是判断在执行前进步骤过程中是否碰到了其他机器,这也是难点,至于其他两种指令只需要对4取余,然后相应改成转向后的方向即可可,这道题目的核心就是模拟题目中的步骤。

下面是代码:

#include <stdio.h>#include <stdlib.h>#include <string.h>#define Max 100struct Pos{int x,y;char dir;};struct Req{int number;int time;char step;};Pos pos[Max];Req req[Max];int k;int a,b;int n,m;bool trag;bool record[Max+1][Max+1];int main(){scanf("%d",&k);while(k--){scanf("%d%d%d%d",&a,&b,&n,&m);int i,j,index;memset(record,false,sizeof(record));for(i=0;i<n;i++){scanf("%d%d %c",&pos[i].x , &pos[i].y , &pos[i].dir);record[pos[i].x][pos[i].y]=true;}for(i=0;i<m;i++)scanf("%d %c%d",&req[i].number,&req[i].step,&req[i].time);trag=true;for(i=0;i<m;i++){if(!trag)break;int num=req[i].number;int t=req[i].time;switch(pos[num-1].dir){//==================================case 'N':switch(req[i].step){case 'L':switch(t%4){case 1:pos[num-1].dir='W';break;case 2:pos[num-1].dir='S';break;case 3:pos[num-1].dir='E';break;}break;case 'R':switch(t%4){case 1:pos[num-1].dir='E';break;case 2:pos[num-1].dir='S';break;case 3:pos[num-1].dir='W';break;}break;case 'F':if(pos[num-1].x<=0 || pos[num-1].x>=a+1 || pos[num-1].y+t<=0 || pos[num-1].y+t>=b+1){for(j=pos[num-1].y+1;j<=b;j++)if(record[pos[num-1].x][j])break;if(j<=b){for(index=0;index<n;index++)if(pos[index].x==pos[num-1].x && pos[index].y==j){printf("Robot %d crashes into robot %d\n",num,index+1);        trag=false;}}else{printf("Robot %d crashes into the wall\n",num);    trag=false;}}else{for(j=pos[num-1].y+1;j<=pos[num-1].y+t;j++)if(record[pos[num-1].x][j])break;     if(j<=pos[num-1].y+t){  for(index=0;index<n;index++)    if(pos[index].x==pos[num-1].x && pos[index].y==j){printf("Robot %d crashes into robot %d\n",num,index+1);        trag=false;} }     else{ record[pos[num-1].x][pos[num-1].y]=false;         record[pos[num-1].x][pos[num-1].y+t]=true;         pos[num-1].y+=t; }}break;}break;//==========================================case 'S':switch(req[i].step){case 'L':switch(t%4){case 1:pos[num-1].dir='E';break;case 2:pos[num-1].dir='N';break;case 3:pos[num-1].dir='W';break;}break;case 'R':switch(t%4){case 1:pos[num-1].dir='W';break;case 2:pos[num-1].dir='N';break;case 3:pos[num-1].dir='E';break;}break;case 'F':/*if(pos[num-1].x==0 || pos[num-1].x==a+1 || pos[num-1].y-t==0 || pos[num-1].y-t==b+1){printf("Robot %d crashes into the wall\n",num);trag=false;}else if(record[pos[num-1].x][pos[num-1].y-t]==true){for(j=0;j<n;j++)if(pos[j].x==pos[num-1].x && pos[j].y==pos[num-1].y-t)break;printf("Robot %d crashes into robot %d\n",num,j+1);trag=false;}else{record[pos[num-1].x][pos[num-1].y]=false;record[pos[num-1].x][pos[num-1].y-t]=true;pos[num-1].y-=t;}*/if(pos[num-1].x<=0 || pos[num-1].x>=a+1 || pos[num-1].y-t<=0 || pos[num-1].y-t>=b+1){for(j=pos[num-1].y-1;j>=1;j--)if(record[pos[num-1].x][j])break;if(j>=1){for(index=0;index<n;index++)if(pos[index].x==pos[num-1].x && pos[index].y==j){printf("Robot %d crashes into robot %d\n",num,index+1);        trag=false;}}else{printf("Robot %d crashes into the wall\n",num);    trag=false;}}else{for(j=pos[num-1].y-1;j>=pos[num-1].y-t;j--)if(record[pos[num-1].x][j])break;     if(j>=pos[num-1].y-t){  for(index=0;index<n;index++)    if(pos[index].x==pos[num-1].x && pos[index].y==j){printf("Robot %d crashes into robot %d\n",num,index+1);        trag=false;} }     else{ record[pos[num-1].x][pos[num-1].y]=false;         record[pos[num-1].x][pos[num-1].y-t]=true;         pos[num-1].y-=t; }}break;}break;//====================================case 'W':switch(req[i].step){case 'L':switch(t%4){case 1:pos[num-1].dir='S';break;case 2:pos[num-1].dir='E';break;case 3:pos[num-1].dir='N';break;}break;case 'R':switch(t%4){case 1:pos[num-1].dir='N';break;case 2:pos[num-1].dir='E';break;case 3:pos[num-1].dir='S';break;}break;case 'F':if(pos[num-1].x-t<=0 || pos[num-1].x-t>=a+1 || pos[num-1].y<=0 || pos[num-1].y>=b+1){for(j=pos[num-1].x-1;j>=1;j--)if(record[j][pos[num-1].y])break;if(j>=1){for(index=0;index<n;index++)if(pos[index].x==j && pos[index].y==pos[num-1].y){printf("Robot %d crashes into robot %d\n",num,index+1);        trag=false;}}else{printf("Robot %d crashes into the wall\n",num);    trag=false;}}else{for(j=pos[num-1].x-1;j>=pos[num-1].x-t;j--)if(record[j][pos[num-1].y])break;     if(j>=pos[num-1].x-t){  for(index=0;index<n;index++)    if(pos[index].x==j && pos[index].y==pos[num-1].y){printf("Robot %d crashes into robot %d\n",num,index+1);        trag=false;} }     else{ record[pos[num-1].x][pos[num-1].y]=false;         record[pos[num-1].x-t][pos[num-1].y]=true;         pos[num-1].x-=t; }}break;}break;//==================================case 'E':switch(req[i].step){case 'L':switch(t%4){case 1:pos[num-1].dir='N';break;case 2:pos[num-1].dir='W';break;case 3:pos[num-1].dir='S';break;}break;case 'R':switch(t%4){case 1:pos[num-1].dir='S';break;case 2:pos[num-1].dir='W';break;case 3:pos[num-1].dir='N';break;}break;case 'F':if(pos[num-1].x+t<=0 || pos[num-1].x+t>=a+1 || pos[num-1].y<=0 || pos[num-1].y>=b+1){for(j=pos[num-1].x+1;j<=a;j++)if(record[j][pos[num-1].y])break;if(j<=a){for(index=0;index<n;index++)if(pos[index].x==j && pos[index].y==pos[num-1].y){printf("Robot %d crashes into robot %d\n",num,index+1);        trag=false;}}else{printf("Robot %d crashes into the wall\n",num);    trag=false;}}else{for(j=pos[num-1].x+1;j<=pos[num-1].x+t;j++)if(record[j][pos[num-1].y])break;     if(j<=pos[num-1].x+t){  for(index=0;index<n;index++)    if(pos[index].x==j && pos[index].y==pos[num-1].y){printf("Robot %d crashes into robot %d\n",num,index+1);        trag=false;} }     else{ record[pos[num-1].x][pos[num-1].y]=false;         record[pos[num-1].x+t][pos[num-1].y]=true;         pos[num-1].x+=t; }}break;}break;}}if(trag)printf("OK\n");}return 0;}


 

原创粉丝点击