본 포스팅은 문제에 대한 접근에 문제가 없지만 코드를 구현함에 있어서 어려운 분들에게 도움이 되었으면 하고자하여 작성하게 되었습니다.
1063 킹
- 이 문제는 시뮬레이션 문제로, 주어진 지문의 조건을 '그대로' 구현하면 되는 문제이다.
- 주어진 킹과 돌의 위치를 기반으로 움직임에 대한 명령어 대로 킹과 돌을 움직여주면 된다.
- 자세한 내용은 코드의 주석을 참고하자.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//단순 시뮬레이션이라서 주석을 따로 남기지 않는다 | |
//그저 주어진 조건을 그대로 코딩하면 되는 문제 | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
#define KING 1 | |
#define STONE 2 | |
int map[8][8] = { 0 }; | |
string N, M; | |
int T = 0; | |
int king_row, king_col; | |
int stone_row, stone_col; | |
void move_king(string move_type) | |
{ | |
if (!move_type.compare("R")) | |
{ | |
if (king_col + 1 >= 8) | |
return; | |
king_col += 1; | |
if (king_col == stone_col && king_row == stone_row) | |
{ | |
if (stone_col + 1 >= 8) | |
{ | |
king_col -= 1; | |
return; | |
} | |
stone_col += 1; | |
} | |
} | |
else if (!move_type.compare("L")) | |
{ | |
if (king_col - 1 < 0) | |
return; | |
king_col -= 1; | |
if (king_col == stone_col && king_row == stone_row) | |
{ | |
if (stone_col - 1 < 0) | |
{ | |
king_col += 1; | |
return; | |
} | |
stone_col -= 1; | |
} | |
} | |
else if (!move_type.compare("B")) | |
{ | |
if (king_row + 1 >= 8) | |
return; | |
king_row += 1; | |
if (king_col == stone_col && king_row == stone_row) | |
{ | |
if (stone_row + 1 >= 8) | |
{ | |
king_row -= 1; | |
return; | |
} | |
stone_row += 1; | |
} | |
} | |
else if (!move_type.compare("T")) | |
{ | |
if (king_row - 1 < 0) | |
return; | |
king_row -= 1; | |
if (king_col == stone_col && king_row == stone_row) | |
{ | |
if (stone_row - 1 < 0) | |
{ | |
king_row += 1; | |
return; | |
} | |
stone_row -= 1; | |
} | |
} | |
else if (!move_type.compare("RB")) | |
{ | |
if (king_row + 1 >= 8 || king_col + 1 >= 8) | |
return; | |
king_row += 1; | |
king_col += 1; | |
if (king_col == stone_col && king_row == stone_row) | |
{ | |
if (stone_row + 1 >= 8 || stone_col + 1 >= 8) | |
{ | |
king_row -= 1; | |
king_col -= 1; | |
return; | |
} | |
stone_row += 1; | |
stone_col += 1; | |
} | |
} | |
else if (!move_type.compare("LB")) | |
{ | |
if (king_row + 1 >= 8 || king_col - 1 < 0) | |
return; | |
king_row += 1; | |
king_col -= 1; | |
if (king_col == stone_col && king_row == stone_row) | |
{ | |
if (stone_row + 1 >= 8 || stone_col - 1 < 0) | |
{ | |
king_row -= 1; | |
king_col += 1; | |
return; | |
} | |
stone_row += 1; | |
stone_col -= 1; | |
} | |
} | |
else if (!move_type.compare("RT")) | |
{ | |
if (king_row - 1 < 0 || king_col + 1 >= 8) | |
return; | |
king_row -= 1; | |
king_col += 1; | |
if (king_col == stone_col && king_row == stone_row) | |
{ | |
if (stone_row - 1 < 0 || stone_col + 1 >= 8) | |
{ | |
king_row += 1; | |
king_col -= 1; | |
return; | |
} | |
stone_row -= 1; | |
stone_col += 1; | |
} | |
} | |
else if (!move_type.compare("LT")) | |
{ | |
if (king_row - 1 < 0 || king_col - 1 < 0) | |
return; | |
king_row -= 1; | |
king_col -= 1; | |
if (king_col == stone_col && king_row == stone_row) | |
{ | |
if (stone_row - 1 < 0 || stone_col - 1 < 0) | |
{ | |
king_row += 1; | |
king_col += 1; | |
return; | |
} | |
stone_row -= 1; | |
stone_col -= 1; | |
} | |
} | |
} | |
int main(void) | |
{ | |
cin >> N >> M >> T; | |
king_row = 8 - (N.at(1) - '0'); | |
king_col = N.at(0) - 'A'; | |
stone_row = 8 - (M.at(1) - '0'); | |
stone_col = M.at(0) - 'A'; | |
map[king_row][king_col] = KING; | |
map[stone_row][stone_col] = STONE; | |
for (int i = 0; i < T; i++) | |
{ | |
map[king_row][king_col] = 0; | |
map[stone_row][stone_col] = 0; | |
string input; | |
cin >> input; | |
move_king(input); | |
map[king_row][king_col] = KING; | |
map[stone_row][stone_col] = STONE; | |
} | |
map[king_row][king_col] = KING; | |
map[stone_row][stone_col] = STONE; | |
king_col += 'A'; | |
king_row = (8 - king_row) + '0'; | |
stone_col += 'A'; | |
stone_row = (8 - stone_row) + '0'; | |
string res_king_col, res_king_row; | |
res_king_col = (char)king_col; | |
res_king_row = (char)king_row; | |
string res_stone_col, res_stone_row; | |
res_stone_col = (char)stone_col; | |
res_stone_row = (char)stone_row; | |
cout << res_king_col << res_king_row << endl; | |
cout << res_stone_col << res_stone_row << endl; | |
return 0; | |
} |
'Algorithm > Baekjoon_PS' 카테고리의 다른 글
17141 연구소2 ( BFS, BackTracking ) (0) | 2020.08.14 |
---|---|
16988_Baaaaaaaaaduk2_Easy ( BFS, BackTracking ) (0) | 2020.08.14 |
1987_알파벳 (DFS) (0) | 2020.08.05 |
17136_색종이 붙이기 ( BackTracking ) (0) | 2020.08.05 |
2210_숫자판점프 (DFS) (0) | 2020.08.05 |