반응형
꼼꼼하게 구현해야하는 문제였습니다.
풀이방법
1. 먼저 움직였을 때 예상되는 킹의 row와 column을 먼저 구해줍니다. 그 후 킹을 이동해도 체스판 안인지, 유효한 이동인지 확인합니다. 아니라면 continue;
2. 다음으로는 돌과 킹을 움직인 후의 위치가 같은지를 확인합니다. 만약 같다면 해당 돌을 이동 시켰을 때 유효한지 확인합니다. 유효하지 않다면 conitnue; 유효하다면 돌을 이동 시킨 후 해당 위치로 돌의 위치를 갱신합니다.
3. 킹을 이동시킨 후 위치를 갱신해줍니다.
Code
#include <bits/stdc++.h>
using namespace std;
int chessboard[8][8];
int main(){
string king,stone;
int moveCount;
cin >> king >> stone >> moveCount;
string currentKing = king;
string currentStone = stone;
while(moveCount--){
string moveDirection;
char kingRow = currentKing[1];
char kingColumn = currentKing[0];
char stoneRow = currentStone[1];
char stoneColumn = currentStone[0];
cin >> moveDirection;
if(moveDirection == "R"){
int nextKingColumn = kingColumn - 'A' + 1;
if(nextKingColumn >= 8) continue;
if(nextKingColumn + 'A' == stoneColumn && kingRow == stoneRow){
if(nextKingColumn + 1 >= 8) continue;
stoneColumn = nextKingColumn + 1 + 'A';
}
kingColumn = nextKingColumn + 'A';
}
else if(moveDirection=="L"){
int nextKingColumn = kingColumn - 'A' - 1;
if(nextKingColumn < 0) continue;
if(nextKingColumn + 'A' == stoneColumn && kingRow == stoneRow){
if(nextKingColumn - 1 < 0) continue;
stoneColumn = nextKingColumn - 1 + 'A';
}
kingColumn = nextKingColumn + 'A';
}
else if(moveDirection=="B"){
int nextKingRow = kingRow - '0' - 1;
if(nextKingRow < 1) continue;
if(nextKingRow + '0' == stoneRow && kingColumn == stoneColumn){
if(nextKingRow - 1 < 1) continue;
stoneRow = nextKingRow - 1 + '0';
}
kingRow = nextKingRow + '0';
}
else if(moveDirection=="T"){
int nextKingRow = kingRow - '0' + 1;
if(nextKingRow > 8) continue;
if(nextKingRow + '0' == stoneRow && kingColumn == stoneColumn){
if(nextKingRow + 1 > 8) continue;
stoneRow = nextKingRow + 1 + '0';
}
kingRow = nextKingRow + '0';
}
else if(moveDirection=="RT"){
int nextKingRow = kingRow - '0' + 1;
int nextKingColumn = kingColumn - 'A' + 1;
if(nextKingRow > 8 || nextKingColumn >= 8) continue;
if(nextKingRow + '0' == stoneRow && nextKingColumn + 'A' == stoneColumn){
if(nextKingRow + 1 > 8 || nextKingColumn + 1 >= 8) continue;
stoneRow = nextKingRow + 1 + '0';
stoneColumn = nextKingColumn + 1 + 'A';
}
kingRow = nextKingRow + '0';
kingColumn = nextKingColumn + 'A';
}
else if(moveDirection=="LT"){
int nextKingRow = kingRow - '0' + 1;
int nextKingColumn = kingColumn - 'A' - 1;
if(nextKingRow > 8 || nextKingColumn < 0) continue;
if(nextKingRow + '0' == stoneRow && nextKingColumn + 'A' == stoneColumn){
if(nextKingRow + 1 > 8 || nextKingColumn - 1 < 0) continue;
stoneRow = nextKingRow + 1 + '0';
stoneColumn = nextKingColumn -1 + 'A';
}
kingRow = nextKingRow + '0';
kingColumn = nextKingColumn + 'A';
}
else if(moveDirection == "RB"){
int nextKingRow = kingRow - '0' - 1;
int nextKingColumn = kingColumn - 'A' + 1;
if(nextKingRow < 1 || nextKingColumn >= 8) continue;
if(nextKingRow + '0' == stoneRow && nextKingColumn + 'A' == stoneColumn){
if(nextKingRow - 1 < 1 || nextKingColumn + 1 >= 8) continue;
stoneRow = nextKingRow - 1 + '0';
stoneColumn = nextKingColumn + 1 + 'A';
}
kingRow = nextKingRow + '0';
kingColumn = nextKingColumn + 'A';
}
else if(moveDirection == "LB"){
int nextKingRow = kingRow - '0' - 1;
int nextKingColumn = kingColumn - 'A' - 1;
if(nextKingRow < 1 || nextKingColumn < 0) continue;
if(nextKingRow + '0' == stoneRow && nextKingColumn + 'A' == stoneColumn){
if(nextKingRow - 1 < 1 || nextKingColumn - 1 < 0) continue;
stoneRow = nextKingRow - 1 + '0';
stoneColumn = nextKingColumn - 1 + 'A';
}
kingRow = nextKingRow + '0';
kingColumn = nextKingColumn + 'A';
}
string tmp = "";
tmp += kingColumn;
tmp += kingRow;
currentKing = tmp;
tmp = "";
tmp += stoneColumn;
tmp += stoneRow;
currentStone = tmp;
}
cout << currentKing << '\n' << currentStone << '\n';
}
'Algorithm > Implementation' 카테고리의 다른 글
(C++) - 백준(BOJ) 19944번 : 뉴비의 기준은 뭘까? 답 (0) | 2021.02.07 |
---|---|
(C++) - 백준(BOJ) 15726번 : 이칙연산 답 (0) | 2021.02.07 |
(C++) - 프로그래머스(연습문제) : 야근 지수 답 (0) | 2021.02.02 |
(C++) - 프로그래머스(고득점 kit - 스택/큐) : 주식가격 답 (0) | 2021.02.01 |
(C++) - 프로그래머스(월간 코드 챌린지 시즌 1) : 삼각 달팽이 답 (0) | 2021.02.01 |