poj 2632 Crashing Robots 模擬題 測試數據+AC代碼

據說 這是一道簡單的模擬題,
據說 這道題並沒有涉及算法。
但是很考驗耐心因爲需要考慮很多情況,我一直WA,所以不停的debug找出之前忽略的情況。

尤其需要注意的是,座標是變換的,注意座標的變換規律。

這道題目大體需要注意這幾點:
1.座標變換
將座標系用二維矩陣的形式表示,但需要注意,“向北走”就是二維矩陣中該點的行數加1,“向南走”就是在二維矩陣中該點的行數減1,“向東走”就是列數加1,“向西走”就是列數減1。
2.取餘出現負數的情況
座標變換的過程中,取餘可能出現負數的情況,注意將其轉換成整數

附上在網上看到的測試數據:
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

AC代碼:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;


int w[105][105];//二維矩陣表示warehouse中的各個位置的狀態,狀態爲-1表示該位置無機器人,爲i表示該位置有機器人i
int dr[4] = { 1,0,-1,0};    //北,東,南,西
int dc[4] = { 0,1,0,-1};
map<char,int> directionMap;

typedef struct robot
{
    int number;
    int r;
    int c;
    int d;
};
robot rbt[110];
void initDirectionMap()
{
    directionMap['N'] = 0;  
    directionMap['E'] = 1;  
    directionMap['S'] = 2;  
    directionMap['W'] = 3; 
}

//機器人的移動動作
void action(char c,int i)
{
    switch(c)
    {
    case 'L':
        {
            rbt[i].d = (rbt[i].d-1+4)%4;
            break;
        }
    case 'R':
        {
            rbt[i].d = (rbt[i].d+1)%4;
            break;
        }
    case 'F':
        {
            w[rbt[i].r][rbt[i].c] = -1;
            rbt[i].r+=dr[rbt[i].d];
            rbt[i].c+=dc[rbt[i].d];
        }
    }
}

int main()
{
    freopen("in.txt","r",stdin);
    int ncase;
    int a,b;
    int n,m;
    char direction;
    int flag;
    cin >> ncase;
    initDirectionMap();
    for(int nc=0;nc<ncase;nc++)
    {
        for(int i=0;i<105;i++)
            for(int j=0;j<105;j++)
                w[i][j] = -1;
        cin >> a >> b;
        cin >> n >> m;
        //記錄warehouse的狀態,即各機器人的位置及移動方向
        for(int i=1;i<=n;i++)
        {
            rbt[i].number = i;
            cin >> rbt[i].c >> rbt[i].r;
            cin >> direction;
            rbt[i].d = directionMap[direction];
            w[rbt[i].r][rbt[i].c] = i;
        }
        flag = 1;
        int num;
        char ac;
        int step;
        for(int i = 0;i<m;i++)
        {
            cin >> num;
            cin >> ac;
            cin >> step;
            flag = 1;
            for(int j=0;j<step;j++)
            {
                action(ac,num);
                if(w[rbt[num].r][rbt[num].c]==-1||w[rbt[num].r][rbt[num].c]==num)
                {
                    w[rbt[num].r][rbt[num].c] = num;
                }
                else
                {
                    flag = 2;
                    break;
                }
                if(rbt[num].c>a||rbt[num].r>b||rbt[num].c<=0||rbt[num].r<=0)
                {
                    flag = 3;
                    break;
                }
            }
            //如果撞牆或機器人相撞,退出循環,將剩下的m-i-1組數據存入臨時變量numx,acx,stepx中
            if(flag!=1)
            {
                int numx,stepx;
                char acx;
                for(int k=0;k<m-i-1;k++)
                    cin >> numx >> acx >> stepx;
                break;
            }
        }
        if(flag==1)
            cout << "OK" << endl;
        else if(flag==2)
            cout << "Robot " << num << " crashes into robot " << w[rbt[num].r][rbt[num].c] << endl;
        else if(flag==3)
            cout << "Robot " << num << " crashes into the wall" << endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章