POJ2632-Crashing Robots(模拟)

题目详情点此查看
花了近一个小时,总算是AC了,
其实这题比较简单,没啥算法,题目详情点击链接;
主要由以下事项:
1.我用的是二维数组,然后其(0, 0)点的位置与直角坐标系的远点位置恰好相反,因此处理数据时要特别注意。
2.最后提交时还出现了了WA, 然后一检查,我的天呐,我尽然把OK写成了小写的。。。。。。。

code:

//***************
//memory  time
// 288K    16MS

#include <iostream>
#include <fstream>
using namespace std;

class Pos{
    public:
        int x, y, d;    //robot的位置信息, d为方向,0, 1, 2, 3对应东北西南 
};

int direction[4][2] = {0, 1, -1, 0, 0, -1, 1, 0};    //此数组记录方向,方便运算 

Pos pos[102];

int main() {
    //ifstream in("data2632.in");
    int k;
    cin >> k;
    while(k --)  { 
        int map[101][101] = {0};
        int A, B, N, M;
        cin >> B >> A >> N >> M;
        for (int i = 1; i <= N; i ++)  {
            int x, y;
            char d;
            cin >> y >> x >> d;
            map[A- x + 1][y] = i;
            pos[i].x = A - x + 1;
            pos[i].y = y;
            switch(d)  {
                case 'E':
                    pos[i].d = 0;
                    break;
                case 'N':
                    pos[i].d = 1;
                    break;
                case 'W':
                    pos[i].d = 2;
                    break;
                case 'S':
                    pos[i].d = 3;
                    break;
            }
        }
        bool ok = true;

        while (M --)  {
            int num, times;
            char action;
            cin >> num >> action >> times;
            if(action == 'F' && ok) {
                while(times --) {
                    int x = pos[num].x;
                    int y = pos[num].y;
                    pos[num].x += direction[pos[num].d][0];
                    pos[num].y += direction[pos[num].d][1];     
                    map[x][y] = 0;
                    x = pos[num].x;
                    y = pos[num].y;
                    if(x < 1 || y < 1 || x > A || y > B) {
                        cout << "Robot " << num << " crashes into the wall" << endl; 
                        //M = 0;
                        ok = false;
                        break;
                    }
                    else if(map[x][y] != 0) {
                        cout << "Robot " << num << " crashes into robot " << map[x][y] << endl;
                        //M = 0;
                        ok = false;
                        break;
                    }
                    else  {
                        map[x][y] = num;
                    }
                }       
            }
            else {
                times %= 4;
                pos[num].d += 4;
                if(action == 'L') {
                    pos[num].d += times;
                }   
                else {
                    pos[num].d -= times;
                }
                pos[num].d %= 4;
            }
        }
        if(ok) {
            cout << "OK" << endl;           
        }
    }
    return 0;
}

  

附加测试数据:
Sample Input
8
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
9 5
5 19
2 4 E
4 3 N
6 2 E
9 5 S
9 1 W
4 F 1
4 R 1
4 F 6
4 L 5
4 F 3
4 L 1
5 R 1
5 F 3
5 L 1
5 F 2
5 L 1
5 F 3
5 R 5
5 F 2
5 R 1
5 F 2
4 F 2
4 L 1
4 F 3
9 5
2 6
9 5 S
9 1 W
1 F 1
1 R 1
1 F 2
2 F 2
2 R 1
2 F 3
5 4
2 2
1 1 E
5 4 W
1 R 1
1 F 2
5 4
2 2
1 1 E
5 4 W
1 L 1
1 F 2

Sample Output
Robot 1 crashes into the wall
Robot 1 crashes into robot 2
OK
Robot 1 crashes into robot 2
Robot 4 crashes into robot 5
Robot 2 crashes into robot 1
Robot 1 crashes into the wall
OK