본문 바로가기

Algorithm/Implementation

(C++) - 백준(BOJ) 1063번 : 킹 답

반응형

www.acmicpc.net/problem/1063

 

1063번: 킹

8*8크기의 체스판에 왕이 하나 있다. 킹의 현재 위치가 주어진다. 체스판에서 말의 위치는 다음과 같이 주어진다. 알파벳 하나와 숫자 하나로 이루어져 있는데, 알파벳은 열을 상징하고, 숫자는

www.acmicpc.net

꼼꼼하게 구현해야하는 문제였습니다.

 

풀이방법

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