| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
我也发下,毕竟做的很艰难//poj2632
//纯模拟,不解释
#include "iostream"
using namespace std;
#define W 0
#define N 1
#define E 2
#define S 3
#define R 1
#define L 3
struct robot{
int pos_x,pox_y;
int direction;
int num;
};
robot rob[101];
int direct[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
int total_case,map_l,map_w;
int map[101][101];
int turn(char c,int dir){
switch (c)
{
case 'F':
return dir;
break;
case 'R':
return (dir+1)%4;
case 'L':
return (dir+3)%4;
}
return 0;
}
int outwell(int x,int y){
if (x<1 || x>map_l || y<1 || y>map_w)
{
return 1;
}
return 0;
}
int run(int rob_n,char to_turn,int step){
int temp_x,temp_y;
int i=0;
for (i=1;i<=step;i++)
{
if(to_turn=='F'){
temp_x=rob[rob_n].pos_x+direct[rob[rob_n].direction][0];
temp_y=rob[rob_n].pox_y+direct[rob[rob_n].direction][1];
if (outwell(temp_x,temp_y))
{
cout<<"Robot "<<rob_n<<" crashes into the wall"<<endl;
return 1;
}
if (map[temp_x][temp_y])
{
cout<<"Robot "<<rob_n<<" crashes into robot "<<map[temp_x][temp_y]<<endl;
return 1;
}
map[rob[rob_n].pos_x][rob[rob_n].pox_y]=0;
rob[rob_n].pos_x=temp_x;
rob[rob_n].pox_y=temp_y;
map[rob[rob_n].pos_x][rob[rob_n].pox_y]=rob_n;
}
else rob[rob_n].direction=turn(to_turn,rob[rob_n].direction);
}
return 0;
}
int main(){
int robot_n,cmd_n,i,robot_num,step,flag;
char dir;
cin>>total_case;
while (total_case--)
{
cin>>map_l>>map_w;
cin>>robot_n>>cmd_n;
memset(map,0,sizeof(map));
for ( i=1;i<=robot_n;i++)
{
cin>>rob[i].pos_x>>rob[i].pox_y>>dir;
if (dir=='W')
{
rob[i].direction=W;
}
else if (dir=='E')
{
rob[i].direction=E;
}
else if(dir=='N')
{
rob[i].direction=N;
}
else if (dir=='S')
{
rob[i].direction=S;
}
rob[i].num=i;
map[rob[i].pos_x][rob[i].pox_y]=i;
}
for (i=0;i<cmd_n;i++)
{
cin>>robot_num>>dir>>step;
flag=run(robot_num,dir,step);
if (flag)
{
for (i++;i<cmd_n;i++)
cin>>robot_num>>dir>>step;
break;
}
}
if (!flag)
{
cout<<"OK"<<endl;
}
}
return 0;
}
/*
4
5 4
2 2
1 1 E
5 4 W
1 F 7
2 F 7
5 4
2 4
1 1 E
5 4 W
1 F 3
2 F 1
1 L 1
1 F 3
5 4
2 2
1 1 E
5 4 W
1 L 96
1 F 2
5 4
2 3
1 1 E
5 4 W
1 F 4
1 L 1
1 F 20
Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2
*/
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator