poj 2632 Crashing Robots

http://poj.org/problem?id=2632

题意:判断机器人在行走的过程中的状况;

思路:简单的模拟题,就是有点麻烦;

代码:

View Code
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
using namespace std;
struct node
{
int x;
int y;
int ford;
}link[110];
int x = 0;
int y = 0;
int visit[110][110] = {0};
bool biao = 1;
void out(int x,int y,char c,int i)
{
link[i].x = x;
link[i].y = y;
switch(c)
{
case 'N':link[i].ford = 1;break;
case 'W':link[i].ford = 2;break;
case 'S':link[i].ford = 3;break;
case 'E':link[i].ford = 4;break;
default :break;
}
}
void printcrash(int a,int b)
{
printf("Robot %d crashes into robot %d\n",b,a);
}
void printout(int a)
{
printf("Robot %d crashes into the wall\n",a);

}
void move1(int a,int b)
{
for(int i = link[a].y +1; i <= link[a].y+b; ++i)
{
if(i > y)
{
printout(a);
biao = 0;
break;
}
if(visit[link[a].x][i])
{
printcrash(visit[link[a].x][i],a);
biao = 0;
break;
}
}
visit[link[a].x][link[a].y] = 0;
visit[link[a].x][link[a].y+b] = a;
link[a].y = link[a].y + b;
}
void move2(int a,int b)
{
for(int i = link[a].x -1; i >= link[a].x-b; --i)
{
if(i < 1)
{
printout(a);
biao = 0;
break;
}
if(visit[i][link[a].y])
{
printcrash(visit[i][link[a].y],a);
biao = 0;
break;
}
}
visit[link[a].x][link[a].y] = 0;
visit[link[a].x - b][link[a].y] = a;
link[a].x = link[a].x - b;
}
void move3(int a,int b)
{
for(int i = link[a].y -1; i >= link[a].y - b; --i)
{
if(i < 1)
{
printout(a);
biao = 0;
break;
}
if(visit[link[a].x][i])
{
printcrash(visit[link[a].x][i],a);
biao = 0;
break;
}
}
visit[link[a].x][link[a].y] = 0;
visit[link[a].x][link[a].y-b] = a;
link[a].y = link[a].y - b;
}
void move4(int a,int b)
{
for(int i = link[a].x +1; i <= link[a].x+b; ++i)
{
if(i > x)
{
printout(a);
biao = 0;
break;
}
if(visit[i][link[a].y])
{
printcrash(visit[i][link[a].y],a);
biao = 0;
break;
}
}
visit[link[a].x][link[a].y] = 0;
visit[link[a].x + b][link[a].y] = a;
link[a].x = link[a].x +b;
}
void move(int a,char c,int b)
{
switch(c)
{
case 'F':
{
switch(link[a].ford)
{
case 1:move1(a,b);break;
case 2:move2(a,b);break;
case 3:move3(a,b);break;
case 4:move4(a,b);break;
default :break;
}
}break;
case 'L':{link[a].ford +=b,link[a].ford = link[a].ford % 4;if(link[a].ford == 0) link[a].ford = 4;}break;
case 'R':{link[a].ford = link[a].ford - b%4;if(link[a].ford < 1) link[a].ford +=4;}break;
default :break;
}
}
int main()
{
int nn = 0;
scanf("%d",&nn);
while(nn--)
{
memset(visit,0,sizeof(visit));
biao = 1;
int x1 = 0;
int y1 = 0;
char c = '\0';
int num = 0;
int n = 0;
int a = 0;
int b = 0;
scanf("%d%d",&x,&y);
scanf("%d%d",&num,&n);
for(int i = 1; i <= num; ++i)
{
scanf("%d %d",&x1,&y1);
getchar();
scanf("%c",&c);
out(x1,y1,c,i);
visit[x1][y1] = i;
}
for(int i = 1; i <= n; ++i)
{
scanf("%d",&a);
getchar();
scanf("%c",&c);
scanf("%d",&b);
if(biao)
move(a,c,b);
}
if(biao)
printf("OK\n");
}
return 0;
}



posted @ 2012-02-26 11:04  LT-blogs  阅读(160)  评论(0编辑  收藏  举报