# 題目: UVa 118 - Mutant Flatworld Explorers
# 題目說明
給一個x * y大小的棋盤與機器人的位置與朝向
再經過一連串的指令後,求機器人最後的位置與朝向
指令一共有3種
L: 左轉90度R: 右轉90度F: 前進一格
當機器人移動超出棋盤時,它就會永遠LOST
每當機器人在一個地方LOST後,它會在原地留下一個記號,避免未來的機器人在同樣的地方LOST
INPUT:
先輸入兩個整數x、y,代表棋盤的大小為x * y
每筆測資輸入兩個整數與一個字元,代表一開始的位置與朝向
接著輸入一個字串,代表移動
OUTPUT:
輸出機器人最後的位置與朝向
若機器人超出棋盤,則再輸出LOST
# 解題方法
使用變數紀錄每一步的位置與朝向
機器人每走一步,即時判斷它是否有超出棋盤的邊界,當超出邊界時,將原本的位置紀錄
# 參考程式碼
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
static auto fast_io = []
{
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
return 0;
}();
int main()
{
unordered_map<char, int> CI = { {'N', 0}, {'E', 1}, {'S', 2}, {'W', 3} };
unordered_map<int, char> IC = { {0, 'N'}, {1, 'E'}, {2, 'S'}, {3, 'W'} };
unordered_map<int, pair<int, int>> M = { {0, {0, 1}}, {1, {1, 0}}, {2, {0, -1}}, {3, {-1, 0}} };
vector<pair<int, int>> drop;
int x, y, a, b, pos;
char p;
string str;
cin >> x >> y;
while (cin >> a >> b >> p && cin >> str)
{
bool lost = false;
pos = CI[p];
for (auto& c : str)
{
if (c == 'R') pos = (pos + 1) % 4;
else if (c == 'L') pos = (pos + 3) % 4;
else
{
int new_a = a + M[pos].first;
int new_b = b + M[pos].second;
if (new_a < 0 || new_a > x || new_b < 0 || new_b > y)
{
lost = true;
for (auto& [i, j] : drop) if (a == i && b == j) lost = false;
if (lost) drop.push_back({ a, b });
}
else a = new_a, b = new_b;
}
if (lost) break;
}
cout << a << " " << b << " " << IC[pos] << (lost ? " LOST\n" : "\n");
}
}