标签:poj
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 8424 | Accepted: 3648 |
Description
Input
Output
Sample Input
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
Sample Output
Robot 1 crashes into the wall Robot 1 crashes into robot 2 OK Robot 1 crashes into robot 2
题意是给定一个坐标地图,给了很多个机器人的坐标和方向,有三种对机器人的操作方式,F是前进,L是向左转,R向右转。看哪一个机器人先撞墙或是撞到其他机器人。
自己需要注意两点:
1.不一定非得到操作机器人的时候才有冲突,可能在最开始的机器人拜访过程中机器人站的位置就已经重复,即冲突了。
2.R向右转,自己定义成为-1,原来自己以为负数除以数的余数会是正数,结果自己too naive了,要考虑将其变为正数。
这个题自己就注意这两点就够了,因为本身就是一个模拟的过程,跟算法也没什么关系。。。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;
struct R
{
int x;
int y;
int dir;
}Robot[110];
enum{ E,N,W,S };
enum{ L=1,R=-1};
int map_f[200][200];
int move_x[5]={1,0,-1,0};
int move_y[5]={0,1,0,-1};
int Test,X,Y,Robot_n,Q,i,flag;
string temp;
void solve()
{
int cal_n,cishu,k;
string manu;
cin>>cal_n>>manu>>cishu;
if(flag==0)
return;
if(manu=="F")
{
map_f[Robot[cal_n].x][Robot[cal_n].y]=0;
for(k=1;k<=cishu;k++)
{
Robot[cal_n].x = Robot[cal_n].x + move_x[Robot[cal_n].dir];
Robot[cal_n].y = Robot[cal_n].y + move_y[Robot[cal_n].dir];
if(Robot[cal_n].x <=0 || Robot[cal_n].y <=0 || Robot[cal_n].x >X || Robot[cal_n].y >Y)
{
flag=0;
cout<<"Robot "<<cal_n<<" crashes into the wall"<<endl;
break;
}
else if(map_f[Robot[cal_n].x][Robot[cal_n].y])
{
flag=0;
cout<<"Robot "<<cal_n<<" crashes into robot "<<map_f[Robot[cal_n].x][Robot[cal_n].y]<<endl;
break;
}
}
map_f[Robot[cal_n].x][Robot[cal_n].y]=cal_n;
}
else if(manu=="L")
{
Robot[cal_n].dir = (Robot[cal_n].dir + L*cishu)%4;
}
else if(manu=="R")
{
Robot[cal_n].dir = (Robot[cal_n].dir + (R*cishu)%4 + 4)%4;
}
}
int main()
{
cin>>Test;
while(Test--)
{
flag=1;
memset(map_f,0,sizeof(map_f));
cin>>X>>Y;
cin>>Robot_n>>Q;
for(i=1;i<=Robot_n;i++)
{
cin>>Robot[i].x>>Robot[i].y;
if(map_f[Robot[i].x][Robot[i].y])
{
flag=0;
cout<<"Robot "<<i<<" crashes into robot "<<map_f[Robot[i].x][Robot[i].y]<<endl;
}
else
{
map_f[Robot[i].x][Robot[i].y]=i;
}
cin>>temp;
if(temp=="E")
Robot[i].dir=E;
else if(temp=="N")
Robot[i].dir=N;
else if(temp=="W")
Robot[i].dir=W;
else if(temp=="S")
Robot[i].dir=S;
}
for(i=1;i<=Q;i++)
{
solve();
}
if(flag)
cout<<"OK"<<endl;
}
//system("pause");
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:poj
原文地址:http://blog.csdn.net/u010885899/article/details/47004823