🔗 문제
https://www.acmicpc.net/problem/1063
✏️ 풀이
시키는 방향대로 이동하면 된다.
킹이나 돌맹이의 이동은 간단한 BFS
로 풀 수 있다.
이 문제가 실버3 인 이유는 위치가 x, y가 아니라 문자열로 주어진다는 점이다.
문자열 변환은 그냥 아래처럼 배열을 만들어줬다.
const char strCol[8] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
이렇게 하면 실제 열과 문자열의 순서가 맞는다.
행은 그냥 역순으로 계산해주면 되어서 7-row로 구했다.
이동해야할 것을 dir와 strDir를 순서를 맞춰 보관한다.
방향이 주어지면 for문으로 맞는 방향으로 이동해주었다.
const int dir[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
const string strDir[8] = {"LT", "T", "RT", "L", "R", "LB", "B", "RB"};
💾 소스
#include <iostream>
#include <queue>
#include <string>
using namespace std;
typedef pair<int, int> pii;
const int dir[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}};
const string strDir[8] = {"LT", "T", "RT", "L", "R", "LB", "B", "RB"};
const char strCol[8] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'};
bool isValid(const int x, const int y)
{
if(x<0 || y<0 || x>=8 || y>= 8)
return false;
return true;
}
pii convert(char col, int row)
{
for(int i=0; i<8; ++i)
{
if(col == strCol[i])
{
return {8-row, i};
}
}
}
string revert(const pii& pos)
{
return strCol[pos.second] + to_string(8-pos.first);
}
int main()
{
char ch;
int n, T;
string str;
pii king, stone;
cin >> ch >> n;
king = convert(ch, n);
cin >> ch >> n;
stone = convert(ch, n);
cin >> T;
while(T--)
{
cin >> str;
for(int d=0; d<8; ++d)
{
if(str == strDir[d])
{
int x = king.first + dir[d][0];
int y = king.second + dir[d][1];
if(!isValid(x, y)) break;
if(stone.first == x && stone.second == y)
{
int sx = stone.first + dir[d][0];
int sy = stone.second + dir[d][1];
if(!isValid(sx, sy)) break;
stone = {sx, sy};
}
king = {x, y};
break;
}
}
}
cout << revert(king) << "\n";
cout << revert(stone) << "\n";
return 0;
}