Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9345 | Accepted: 3997 |
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题目大意:模拟机器人的运动,给定地图,机器人的初始位置和方向,以及一些指令,判断机器人在运动时是否会撞墙或者撞到其他机器人。
思路:用一个二维数组模拟地图,定义-1为墙壁,0为空地,大于0的数组为此时该地有机器人,数字为机器人编号,运动时若不为0则记录该值,并记录当前运动的机器人编号然后读完数据即可输出。没有什么算法可言,只是需要耐心。
实现代码:
//************************************************************************// //*Author : Handsome How *// //************************************************************************// //#pragma comment(linker, "/STA CK:1024000000,1024000000") #pragma warning(disable:4996) #include <vector> #include <list> #include <map> #include <set> #include <deque> #include <queue> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <complex> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <ctime> #include <cassert> #define fur(i,a,b) for(int i=(a);i<=(b);i++) #define furr(i,a,b) for(int i=(a);i>=(b);i--) using namespace std; typedef long long LL; const int dirx[] = { 0,1,0,-1 }; const int diry[] = { 1,0,-1,0 }; int fig[105][105]; int X, Y; struct Robot { int x; int y; int dir; }robot[105]; void init(int x,int y) { memset(fig, 0, sizeof(fig)); fur(i, 1, y+1)fig[x + 1][i] = -1; fur(i, 1, x)fig[i][y + 1] = -1; fur(i, 0, 104)fig[0][i] = -1; fur(i, 1, 104)fig[i][0] = -1; //对地图初始化,边界为-1,其他为0 } void turnl(int n) { robot[n].dir = (robot[n].dir + 3) % 4; } void turnr(int n) { robot[n].dir = (robot[n].dir + 1) % 4; } int go(int n, int t) { int dir = robot[n].dir; fig[robot[n].x][robot[n].y] = 0; while (t--) { robot[n].x += dirx[dir]; robot[n].y += diry[dir]; if (fig[robot[n].x][robot[n].y]!=0)return fig[robot[n].x][robot[n].y]; //若不是空地立刻返回 } fig[robot[n].x][robot[n].y] = n; return 0; } int main() { //freopen("E:\\data.in", "r", stdin); //freopen("E:\\data.out", "w", stdout); int T; scanf("%d", &T); while (T--) { scanf("%d%d", &X, &Y); init(X, Y); int crasha = 0, crashb = 0; int numb, q; char str[5]; scanf("%d%d", &numb, &q); fur(i, 1, numb) { scanf("%d%d%s", &robot[i].x, &robot[i].y, str); fig[robot[i].x][robot[i].y] = i; switch (str[0]) { case 'N': robot[i].dir = 0; break; case 'E': robot[i].dir = 1; break; case 'S': robot[i].dir = 2; break; case 'W': robot[i].dir = 3; break; } } int n, t; fur(i, 1, q) { scanf("%d%s%d", &n,str,&t); if (crashb == 0) //若以有结果则只需要读完数据 { if (str[0] == 'L') { t = t % 4; while (t--) turnl(n); continue; } if (str[0] == 'R') { t = t % 4; while (t--) turnr(n); continue; } crashb = go(n, t); if (crashb)crasha = n; } } if (crashb == 0)printf("OK\n"); else if (crashb == -1)printf("Robot %d crashes into the wall\n",crasha); else printf("Robot %d crashes into robot %d\n",crasha,crashb); } return 0; }