알고리즘
백준 1063번 킹 C++
영춘권의달인
2022. 4. 11. 10:59
명령에 따라 직접 움직여보면서 문제를 해결하였다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
using namespace std;
using ll = long long;
int dRow[8] = { -1,-1, 0, 1, 1, 1, 0,-1 };
int dCol[8] = { 0, 1, 1, 1, 0,-1,-1,-1 };
string GetNextPos(const string &str, const string &moveStr) {
string nextPos = "";
if (moveStr == "T") {
nextPos += str[0];
nextPos += str[1] + 1;
}
else if (moveStr == "RT") {
nextPos += str[0] + 1;
nextPos += str[1] + 1;
}
else if (moveStr == "R") {
nextPos += str[0] + 1;
nextPos += str[1];
}
else if (moveStr == "RB") {
nextPos += str[0] + 1;
nextPos += str[1] - 1;
}
else if (moveStr == "B") {
nextPos += str[0];
nextPos += str[1] - 1;
}
else if (moveStr == "LB") {
nextPos += str[0] - 1;
nextPos += str[1] - 1;
}
else if (moveStr == "L") {
nextPos += str[0] - 1;
nextPos += str[1];
}
else {
nextPos += str[0] - 1;
nextPos += str[1] + 1;
}
return nextPos;
}
bool isOutOfBoard(string str) {
if ((str[0] < 'A' || str[0]>'H') ||
(str[1] < '1' || str[1]>'8')) {
return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string king, stone;
int n;
cin >> king >> stone >> n;
for (int i = 0; i < n; i++) {
string moveStr;
cin >> moveStr;
string nextKingPos = GetNextPos(king, moveStr);
if (isOutOfBoard(nextKingPos)) continue;
if (nextKingPos == stone) {
string nextStonePos = GetNextPos(stone, moveStr);
if (isOutOfBoard(nextStonePos)) continue;
king = nextKingPos;
stone = nextStonePos;
}
else king = nextKingPos;
}
cout << king << "\n" << stone;
};