poj2632

本题的图与其他题稍有不同,本题以左下角为(0,0)点,列号对应x,行号对应y。

完全模拟即可。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
 
const int maxa = 101;
 
struct Robot
{
    int x, y, d;
} robot[maxa];
 
int dir[4][2] =
{
{ 0, 1 },//e
{ -1, 0 }, //s
{ 0, -1 }, //w
{ 1, 0 } }; //n
 
 
int n, m, cx, cy;
int map[maxa][maxa];
 
void init()
{
    memset(map, 0, sizeof(map));
    cin >> cy >> cx >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> robot[i].y >> robot[i].x;
        getchar();
        switch (getchar())
        {
        case 'E':
            robot[i].d = 0;
            break;
        case 'S':
            robot[i].d = 1;
            break;
        case 'W':
            robot[i].d = 2;
            break;
        case 'N':
            robot[i].d = 3;
            break;
        }
        map[robot[i].x][robot[i].y] = i;
    }
}
 
bool forward(int a)
{
    map[robot[a].x][robot[a].y] = 0;
    robot[a].x += dir[robot[a].d][0];
    robot[a].y += dir[robot[a].d][1];
    int x = robot[a].x;
    int y = robot[a].y;
    if (x == 0 || x > cx || y == 0 || y > cy)
    {
        printf("Robot %d crashes into the wall\n", a);
        return false;
    }
    if (map[x][y] != 0)
    {
        printf("Robot %d crashes into robot %d\n", a, map[x][y]);
        return false;
    }
    map[x][y] = a;
    return true;
}
 
bool operate(int a, char op, int times)
{
    if (op == 'F')
    {
        for (int i = 0; i < times; i++)
            if (!forward(a))
                return false;
        return true;
    }
    if (op == 'L')
        robot[a].d -= times;
    else
        robot[a].d += times;
    robot[a].d %= 4;
    if (robot[a].d < 0)
        robot[a].d += 4;
    return true;
}
 
void finish(int times)
{
    string st;
    while (times--)
        getline(cin, st);
}
 
void work()
{
    for (int i = 0; i < m; i++)
    {
        int num, time;
        char op;
        cin >> num;
        getchar();
        cin >> op >> time;
        getchar();
        if (!operate(num, op, time))
        {
            finish(m - i - 1);
            return;
        }
    }
    cout << "OK\n";
}
 
int main()
{
    //freopen("D:\\t.txt", "r", stdin);
    int t;
    cin >> t;
    while (t--)
    {
        init();
        work();
    }
    return 0;
}
posted @   undefined2024  阅读(349)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示