【題解】CPE 一顆星選集:47. Mutant Flatworld Explorers

題目

Moves off an edge is lost forever, leave scent, prohibit future robots drop at same point.
注意:如果 lost,輸出他掉落前的最後位置和方向。

UVa 連結
ZeroJudge 連結

解法

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main(){
    int m, n, x, y;
    char o;
    string s;
    cin >> m >> n;
    m++, n++;
    int a[m][n];
    memset(a, 0, sizeof(a));
    while(cin >> x >> y >> o){
        cin >> s;
        bool lost = false;
        for(char c : s){
            if(c == 'L'){
                if(o == 'N') o = 'W';
                else if(o == 'S') o = 'E';
                else if(o == 'E') o = 'N';
                else if(o == 'W') o = 'S';
            }
            else if(c == 'R'){
                if(o == 'N') o = 'E';
                else if(o == 'S') o = 'W';
                else if(o == 'E') o = 'S';
                else if(o == 'W') o = 'N';
            }
            else{
                int lastX = x, lastY = y;
                if(o == 'N') y += 1;
                else if(o == 'S') y -= 1;
                else if(o == 'E') x += 1;
                else if(o == 'W') x -= 1;
                if(!(x >= 0 && x < m && y >= 0 && y < n)){
                    x = lastX, y = lastY;
                    if(a[lastX][lastY] == 0){
                        a[lastX][lastY] = 1;
                        lost = true;
                        break;
                    }
                }
            }
        }
        cout << x << ' ' << y << ' ' << o;
        if(lost) cout << " LOST\n";
        else cout << '\n';
    }
}
👉 回到:【CPE大學程式能力檢定】目錄
發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *