标签:
输入仓库大小,和机器人的位置,朝向。根据指令进行操作,如果两个机器人相撞或者机器人撞墙则输出,结束。
输入:
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
模拟水题。。没有算法,细心就行。
#include <iostream>
using namespace std;
const int MAX = 110;
int A, B;
int N, M;
struct RB {
int x, y;
char f;
};
struct ML {
int r;
char a;
int c;
};
RB bot[MAX];
ML m[MAX];
int map[MAX][MAX];
void outmap() {
for (int i = 1; i <= A; i++) {
for (int j = 1; j <= B; j++) {
cout << map[i][j];
}
cout << endl;
}
}
void solve() {
for (int i = 1; i <= M; i++) {
ML t = m[i];
if (t.a == ‘L‘) {
t.c %= 4;
while (t.c --) {
if (bot[t.r].f == ‘N‘) bot[t.r].f = ‘W‘;
else if (bot[t.r].f == ‘W‘) bot[t.r].f = ‘S‘;
else if (bot[t.r].f == ‘E‘) bot[t.r].f = ‘N‘;
else if (bot[t.r].f == ‘S‘) bot[t.r].f = ‘E‘;
}
}
else if (t.a == ‘R‘) {
t.c %= 4;
while (t.c --) {
if (bot[t.r].f == ‘N‘) bot[t.r].f = ‘E‘;
else if (bot[t.r].f == ‘W‘) bot[t.r].f = ‘N‘;
else if (bot[t.r].f == ‘E‘) bot[t.r].f = ‘S‘;
else if (bot[t.r].f == ‘S‘) bot[t.r].f = ‘W‘;
}
}
else if (t.a == ‘F‘) {
if (bot[t.r].f == ‘N‘) {
while (t.c--) {
bot[t.r].y++;
if (map[bot[t.r].x][bot[t.r].y]) {
cout << "Robot " << t.r << " crashes into robot " << map[bot[t.r].x][bot[t.r].y] << endl;
return;
}
else {
map[bot[t.r].x][bot[t.r].y] = t.r;
map[bot[t.r].x][bot[t.r].y-1] = 0;
}
if (bot[t.r].y >= B+1) {
cout << "Robot " << t.r << " crashes into the wall" << endl;
return;
}
}
}
else if (bot[t.r].f == ‘S‘) {
while (t.c--) {
bot[t.r].y--;
if (map[bot[t.r].x][bot[t.r].y]) {
cout << "Robot " << t.r << " crashes into robot " << map[bot[t.r].x][bot[t.r].y] << endl;
return;
}
else {
map[bot[t.r].x][bot[t.r].y] = t.r;
map[bot[t.r].x][bot[t.r].y+1] = 0;
}
if (bot[t.r].y <= 0) {
cout << "Robot " << t.r << " crashes into the wall" << endl;
return;
}
}
}
else if (bot[t.r].f == ‘W‘) {
while (t.c--) {
bot[t.r].x--;
if (map[bot[t.r].x][bot[t.r].y]) {
cout << "Robot " << t.r << " crashes into robot " << map[bot[t.r].x][bot[t.r].y] << endl;
return;
}
else {
map[bot[t.r].x][bot[t.r].y] = t.r;
map[bot[t.r].x+1][bot[t.r].y] = 0;
}
if (bot[t.r].x <= 0) {
cout << "Robot " << t.r << " crashes into the wall" << endl;
return;
}
}
}
else if (bot[t.r].f == ‘E‘) {
while (t.c--) {
bot[t.r].x++;
if (map[bot[t.r].x][bot[t.r].y]) {
cout << "Robot " << t.r << " crashes into robot " << map[bot[t.r].x][bot[t.r].y] << endl;
return;
}
else {
map[bot[t.r].x][bot[t.r].y] = t.r;
map[bot[t.r].x-1][bot[t.r].y] = 0;
}
if (bot[t.r].x >= A+1) {
cout << "Robot " << t.r << " crashes into the wall" << endl;
return;
}
}
}
}
}
cout << "OK" << endl;
}
int main() {
int TT;
cin >> TT;
while (TT--) {
for (int i = 1; i <= 100; i++) {
for (int j = 1; j <= 100; j++) {
map[i][j] = 0;
}
}
cin >> A >> B;
cin >> N >> M;
for (int i = 1; i <= N; i++) {
cin >> bot[i].x >> bot[i].y >> bot[i].f;
map[bot[i].x][bot[i].y] = i;
}
for (int i = 1; i <= M; i++) {
cin >> m[i].r >> m[i].a >> m[i].c;
}
solve();
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/songkuo/article/details/51334036