[내 풀이]
문제에서 주어진 위치가 coordinate형식이 아니어서 그것만 변환해주는 식 만들기만 하면 문제는 쉽게 풀리는 것 같다.
[코드]
#include <iostream>
#include <map>
#include <vector>
#include <string>
#define N_MAX 51
using namespace std;
string king_pos, stone_pos;
pair<int, int> pos_king, pos_stone;
int N;
string order[N_MAX];
map<string, int> move_dir = { {"R", 0}, {"L", 1}, {"B", 2 },{"T", 3}, {"RT", 4}, {"LT", 5},{"RB", 6}, {"LB", 7}};
int dir_y[8] = { 0,0,1,-1,-1,-1,1,1 };
int dir_x[8] = { 1,-1,0,0,1,-1,1,-1 };
pair<int, int> convert_str_to_pair_pos(string str) {
//주어진 킹과 돌의 위치를 coordinate형식으로 바꾼다
int row = str[0] - 'A';
int column = 8-(str[1] - '0');
return make_pair(column, row);
}
string convert_pair_to_str_pos(pair<int, int> pos) {
//coordinate 형식의 좌표를 문제에서 정의한 위치 방식으로 바꾼다
char row = pos.second + 'A';
char column = (-(pos.first - 8))+'0';
string result = "";
result.push_back(row);
result.push_back(column);
return result;
}
bool check_range(int y, int x) {
//range가 8x8안에 있는지 확인하는 함수
if (y < 0 || y >= 8 || x < 0 || x >= 8) return false;
return true;
}
void solve() {
for (int i = 0; i < N; i++) {
string each_order = order[i];
int dir = move_dir[each_order];
int new_king_y = pos_king.first + dir_y[dir];
int new_king_x = pos_king.second + dir_x[dir];
//king 위치를 명령에 맡게 옮긴 후 range안에 없을시 다음 명령으로 갈 것
if (!check_range(new_king_y, new_king_x)) continue;
//옮긴 king의 위치와 stone의 위치가 같아 질시 stone도 옮겨야 함
if (new_king_y == pos_stone.first && new_king_x == pos_stone.second) {
int new_stone_y = pos_stone.first + dir_y[dir];
int new_stone_x = pos_stone.second + dir_x[dir];
//stone이 range안에 없을 시 다음 명령으로 갈것
if (!check_range(new_stone_y, new_stone_x)) continue;
pos_stone = make_pair(new_stone_y, new_stone_x); // 바뀐 stone위치로 옮기기
}
pos_king = make_pair(new_king_y, new_king_x); //바뀐 king위치로 옮기기
}
}
int main() {
cin >> king_pos >> stone_pos >> N;
pos_king = convert_str_to_pair_pos(king_pos);
pos_stone = convert_str_to_pair_pos(stone_pos);
for (int n = 0; n < N; n++) {
cin >> order[n];
}
solve();
king_pos = convert_pair_to_str_pos(pos_king);
stone_pos = convert_pair_to_str_pos(pos_stone);
cout << king_pos << "\n";
cout << stone_pos << "\n";
}
[총평]
주어진 위치를 coordinate으로 바꿔서 했는데 생각보다 복잡했는데 다른 방식이 분명 있을 것 같다.
https://yabmoons.tistory.com/130