模拟题,考验一些细节的东西,但我真的是太笨了,细节方面总是出错。
这一题最关键的是如何用数组表示出这个二维坐标平面,我个人觉得把题目给的图顺时针旋转90度的话,就比较方便。
以下是代码(虽然是从网上看的):
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <map>
#include <cstring>
using namespace std;
const int MAX = 1e3;
struct node
{
int x, y;
int sign;
};
int run[4][2] = {
{0,1},{-1,0},{0,-1},{1,0}
};
map<char, int> m;
node pos[MAX];
int maze[MAX][MAX] = { {0} };
int A, B, N, M;
int main()
{
m['N'] = 0, m['W'] = 1, m['S'] = 2, m['E'] = 3;
int t; cin >> t;
while (t--)
{
memset(maze, 0, sizeof(maze));
cin >> A >> B >> N >> M; int a, b; char ch; //cout << A << B << N << M;
for (int i = 1; i <= N; i++)
{
//cout << i << endl;
cin >> pos[i].x >> pos[i].y >> ch;
maze[pos[i].x][pos[i].y] = i;
pos[i].sign = m[ch];
}
bool f = false;
while (M--)
{
cin >> a >> ch >> b; //cout << a << ch << b << endl;
while (b--) {
if (f) break;
if (ch == 'L') { pos[a].sign = (pos[a].sign + 1) % 4; }
else if (ch == 'R') { pos[a].sign = (pos[a].sign + 3) % 4; }
else {
//cout << a << endl;
maze[pos[a].x][pos[a].y] = 0;
int x = pos[a].x+= run[pos[a].sign][0], y = pos[a].y+= run[pos[a].sign][1]; //cout << "first " << x << " " << y << endl;
//x , y ;
//cout << "second " << x << " " << y << " " << B << " " << A << endl;
if (x < 1 || y < 1 || x > A || y > B)
{
f = true;
printf("Robot %d crashes into the wall\n", a); break;
}
else
{
if (maze[x][y] && maze[x][y] != a) {
f = true, printf("Robot %d crashes into robot %d\n", a, maze[x][y]); break;
}
else maze[x][y] = a;
//{ swap(maze[pos[a].x][pos[a].y], maze[x][y]), pos[a].x = x, pos[a].y = y; }
}
}
}
}
if (!f)
cout << "OK" << endl;
}
return 0;
}