您的位置:首页 > 其它

poj-2632-Crashing Robots

2016-03-28 14:54 316 查看
传送门

大意:一个仓库里面有几个机器人,给你机器人的初始位置及方向,然后让机器人执行一些指令,输出指令执行结果

如:

Robot i crashes into the wall, if robot i crashes into a wall. (A robot crashes into a wall if Xi = 0, Xi = A + 1, Yi = 0 or Yi = B + 1.)

Robot i crashes into robot j, if robots i and j crash, and i is the moving robot.

OK, if no crashing occurs.

直接模拟机器人就行了,

tip:用map把方向转换成数字,这样每次转方向的时候只要取模就行了

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <algorithm>
#define N 110
#define ll long long
using namespace std;
struct robot{
int x,y;
int dir;
}r
;
int Map

, a, b, n, m;
map<char, int> mm;
bool fun(int num, int rep){
int i, x = r[num].x, y = r[num].y;
if (r[num].dir == 0){
for (i = 1; i <= rep; i++){
if (x+i > a){
printf("Robot %d crashes into the wall\n", num);
return true;
}
if (Map[x+i][y]){
printf("Robot %d crashes into robot %d\n", num, Map[x+i][y]);
return true;
}else{
Map[x+i-1][y] = 0;
Map[x+i][y] = num;
r[num].x = x+i;
}
}
}else if (r[num].dir == 1){
for (i = 1; i <= rep; i++){
if (y-i < 1){
printf("Robot %d crashes into the wall\n", num);
return true;
}
if (Map[x][y-i]){
printf("Robot %d crashes into robot %d\n", num, Map[x][y-i]);
return true;
}else{
Map[x][y-i+1] = 0;
Map[x][y-i] = num;
r[num].y = y-i;
}
}
}else if (r[num].dir == 2){
for (i = 1; i <= rep; i++){
if (x-i < 1){
printf("Robot %d crashes into the wall\n", num);
return true;
}
if (Map[x-i][y]){
printf("Robot %d crashes into robot %d\n", num, Map[x-i][y]);
return true;
}else{
Map[x-i+1][y] = 0;
Map[x-i][y] = num;
r[num].x = x-i;
}
}
}else{
for (i = 1; i <= rep; i++){
if (y+i > b){
printf("Robot %d crashes into the wall\n", num);
return true;
}
if (Map[x][y+i]){
printf("Robot %d crashes into robot %d\n", num, Map[x][y+i]);
return true;
}else{
Map[x][y+i-1] = 0;
Map[x][y+i] = num;
r[num].y = y+i;
}
}
}

return false;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("1.txt", "r", stdin);
#endif
int i, j, T;
int num, rep;
char act;
bool flag;
mm.insert(pair<char, int>('E', 0));
mm.insert(pair<char, int>('S', 1));
mm.insert(pair<char, int>('W', 2));
mm.insert(pair<char, int>('N', 3));
scanf("%d", &T) ;
while(T--){
flag = false;
memset(Map, 0, sizeof(Map));
scanf("%d%d", &a, &b);
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++){
cin >> r[i].x >> r[i].y >> act;
r[i].dir = mm[act];
Map[r[i].x][r[i].y] = i;
}
for (i = 0; i < m; i++){
cin >> num >> act >> rep;
if (flag)   continue;
switch(act){
case 'L': r[num].dir = ((r[num].dir-rep)%4+4)%4; break;
case 'R': r[num].dir = (r[num].dir+rep)%4; break;
case 'F': flag = fun(num, rep); break;
}
}
if (!flag){
printf("OK\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: