POJ2632

模拟robot,用一个二维数组来存放各个robot的位置和方向,但是注意二维数组的方向和实际的相差90°,理解的时候需转换

 

代码:

#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef struct p{
int x;
int y;
int c;
}P;
int main()
{
freopen("F://学习//算法//codeblock//11//in.txt","r",stdin);
//freopen("F://学习//算法//codeblock//11//in.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--){
int a,b,n,m;
scanf("%d%d%d%d",&a,&b,&n,&m);
int f[101][101];
memset(f,0,sizeof(f));
int i;
char c1;
P r[101];
for(i=1;i<=n;i++){
scanf("%d%d %c",&r[i].x,&r[i].y,&c1);
if(c1=='N'){
r[i].c=0;
}else if(c1=='W'){
r[i].c=1;
}else if(c1=='S'){
r[i].c=2;
}else{
r[i].c=3;
}
f[r[i].x][r[i].y]=i;
}
int j,cnt;
bool flag=false,flag1=false;
for(i=1;i<=m;i++){
scanf("%d %c%d",&j,&c1,&cnt);
if(flag||flag1){
continue;
}
if(c1=='L'){
cnt=cnt%4;
r[j].c=(r[j].c+cnt)%4;
}else if(c1=='R'){
cnt=cnt%4;
r[j].c=(r[j].c-cnt+4)%4;
}else{
int k;
if(r[j].c==0){
for(k=r[j].y+1;k<=b&&k<=r[j].y+cnt;k++){
if(f[r[j].x][k]!=0){
printf("Robot %d crashes into robot %d\n",f[r[j].x][r[j].y],f[r[j].x][k]);
flag=true;
break;
}
}
if(!flag){
if(r[j].y+cnt>b){
printf("Robot %d crashes into the wall\n",f[r[j].x][r[j].y]);
flag1=true;
continue;
}
f[r[j].x][r[j].y+cnt]=j;
f[r[j].x][r[j].y]=0;
r[j].y+=cnt;
}
}else if(r[j].c==1){
for(k=r[j].x-1;k>=1&&k>=r[j].x-cnt;k--){
if(f[k][r[j].y]!=0){
printf("Robot %d crashes into robot %d\n",f[r[j].x][r[j].y],f[k][r[j].y]);
flag=true;
break;
}
}
if(!flag){
if(r[j].x-cnt<1){
printf("Robot %d crashes into the wall\n",f[r[j].x][r[j].y]);
flag1=true;
continue;
}
f[r[j].x-cnt][r[j].y]=j;
f[r[j].x][r[j].y]=0;
r[j].x-=cnt;
}
}else if(r[j].c==2){
for(k=r[j].y-1;k>=1&&k>=r[j].y-cnt;k--){
if(f[r[j].x][k]!=0){
printf("Robot %d crashes into robot %d\n",f[r[j].x][r[j].y],f[r[j].x][k]);
flag=true;
break;
}
}
if(!flag){
if(r[j].y-cnt<1){
printf("Robot %d crashes into the wall\n",f[r[j].x][r[j].y]);
flag1=true;
continue;
}
f[r[j].x][r[j].y-cnt]=j;
f[r[j].x][r[j].y]=0;
r[j].y-=cnt;
}
}else{
for(k=r[j].x+1;k<=a&&k<=r[j].x+cnt;k++){
if(f[k][r[j].y]!=0){
printf("Robot %d crashes into robot %d\n",f[r[j].x][r[j].y],f[k][r[j].y]);
flag=true;
break;
}
}
if(!flag){
if(r[j].x+cnt>a){
printf("Robot %d crashes into the wall\n",f[r[j].x][r[j].y]);
flag1=true;
continue;
}
f[r[j].x+cnt][r[j].y]=j;
f[r[j].x][r[j].y]=0;
r[j].x+=cnt;
}
}
}
}
if(!flag&&!flag1){
printf("OK\n");
}
}
return 0;
}


 

posted on 2012-03-08 16:48  Kahuna  阅读(339)  评论(0编辑  收藏  举报

导航