1일1알

백준 1063번 킹 C++ 본문

알고리즘

백준 1063번 킹 C++

영춘권의달인 2022. 4. 11. 10:59

출처 : https://www.acmicpc.net/problem/1063

 

명령에 따라 직접 움직여보면서 문제를 해결하였다.

 

#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;
};

'알고리즘' 카테고리의 다른 글

백준 1113번 수영장 만들기 C++  (0) 2022.04.14
백준 1195번 킥다운 C++  (0) 2022.04.12
백준 1205번 등수 구하기 C++  (0) 2022.04.10
백준 6603번 로또 C++  (0) 2022.04.09
백준 16236번 아기 상어 C++  (0) 2022.04.07