POJ2632

来源:互联网 发布:程序员该怎么建立博客 编辑:程序博客网 时间:2024/05/01 16:21

 摘要:模拟题,水

#include <iostream>
using namespace std;


const int size = 100;
typedef struct Position{
    int machine;
    int dir;
}Position;

typedef struct Machine{
    int x;
    int y;
}Machine;

Machine robot[size];
Position graph[size+1][size+1];

int getDir(char d)
{
    int dir = 0;   
    switch(d){
        case 'N':
            dir = 1;
            break;
        case 'E':
            dir = 2;
            break;
        case 'S':
            dir = 3;
            break;
        case 'W':
            dir = 4;
            break;
        default:
            dir = 0;
            break;
    }

    return dir;
}

void getNewPosi(int x, int y, int & new_x, int & new_y)
{
    switch(graph[x][y].dir){
        case 1:
            new_x = x;
            new_y = y+1;   
            break;
        case 2:
            new_x = x+1;
            new_y = y;   
            break;
        case 3:
            new_x = x;
            new_y = y-1;
            break;
        case 4:
            new_x = x-1;
            new_y = y;
            break;
        default:
            break;
    }
}


bool checkNewPosi(int r, int new_x, int new_y, int ew, int ns, bool & crash)
{
    if( new_x>ew || new_x<1    || new_y>ns || new_y<1 ){
        crash = true;
        cout << "Robot " << r << " crashes into the wall" << endl;
        return false;
    }                   
    if( graph[new_x][new_y].machine != 0 ){
        crash = true;
        cout << "Robot " << r << " crashes into robot " << graph[new_x][new_y].machine << endl;
        return false;   
    }

    return true;
}

void move(int r, int new_x, int new_y)
{
    int temp_m = graph[robot[r].x][robot[r].y].machine;
    int temp_d = graph[robot[r].x][robot[r].y].dir;
   
    graph[robot[r].x][robot[r].y].machine = 0;
    graph[robot[r].x][robot[r].y].dir = 0;
    robot[r].x = new_x;
    robot[r].y = new_y;
    graph[new_x][new_y].machine = temp_m;
    graph[new_x][new_y].dir = temp_d;

}


int main()
{
    int n;
    cin >> n;
    for(int i=1; i<=n; i++){
        int ew, ns;
        cin >> ew >> ns;
        for(int j=1; j<=ew; j++){
            for(int k=1; k<=ns; k++){
                graph[j][k].machine = 0;
                graph[j][k].dir = 0;
            }
        }
        //cout << "graph init finish" << endl;
   
        int N, M;
        cin >> N >> M;
        for(int j=1; j<=N; j++){
            int x, y;
            char dir;
            cin >> x >> y >> dir;
            graph[x][y].machine = j;
            graph[x][y].dir = getDir(dir);                 
            robot[j].x = x;
            robot[j].y = y;
        }
        //cout << "graph, robot assignment finish" << endl;

        bool crash = false;
        for(int j=1; j<=M; j++){
            int r, steps;
            char op;
            cin >> r >> op >>steps;
            //if( crash ){
            //    continue;
            //}
           
            for(int k=1; k<=steps; k++){
                if( crash ){
                    continue;
                }
                if( op == 'F' ){
                    int new_x = 0;
                    int new_y = 0;
                    getNewPosi(robot[r].x, robot[r].y, new_x, new_y);
                    if( !checkNewPosi(r, new_x, new_y, ew, ns, crash) ){
                        //crash = true;
                        break;   
                    }   
                    move(r, new_x, new_y);   
                    continue;
                }
                if( op == 'L' ){
                    graph[robot[r].x][robot[r].y].dir--;
                    if( graph[robot[r].x][robot[r].y].dir ==0 ){
                        graph[robot[r].x][robot[r].y].dir = 4;
                    }
                    continue;
                }
                if( op == 'R' ){
                    graph[robot[r].x][robot[r].y].dir++;
                    if( graph[robot[r].x][robot[r].y].dir == 5){
                        graph[robot[r].x][robot[r].y].dir = 1;
                    }
                    continue;
                }
            }       
            //cout << "finish opration:" << j << endl;
        }
        if( !crash ){
            cout << "OK" << endl;
        }
    }

    return 0;
}