성우리뷰

공 튕기기

두원공대88학번뚜뚜 2021. 11. 28. 00:45
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int b[10][10];
int d[10][10];

int solution(vector<vector<int>> board, vector<vector<int>> durability) {
    int answer = 0;

    for (int i = 0; i < board.size(); i++) {
        for (int j = 0; j < board.size(); j++) {
            b[i][j] = board[i][j];
            d[i][j] = durability[i][j];
        }
    }
    pair<int, int> p = make_pair(1, 0); //현재는 x만 1로 움직, y는 안움
    int x = -1; int y = 0; //시작포지션

    //맨 처음 시작포지션에서, 5 3 1일 경우엔 답은 0
    if (board[0][0] == 5) {
        return 0;
    }

    //x < board.size() && y < board.size()
    while (true) {
        x += p.first; y += p.second; //한칸 움직이고
        answer++;
        if (x >= board.size() || y >= board.size() || 
            x < 0 || y < 0) {
            return answer-1;
        }

        if (b[y][x] > 0) {
            //숫자판마다 표기 3번판
            if (b[y][x] == 3) {
                if (p.first == -1 && p.second == 0) {
                    p.first = 0; p.second = -1;
                }
                else if (p.first == 0 && p.second == 1) {
                    p.first = 1; p.second = 0;
                }
            }//2번판
            else if (b[y][x] == 2) {
                if (p.first == 1 && p.second == 0) {
                    p.first = 0; p.second = 1;
                }
                else if (p.first == 0 && p.second == -1) {
                    p.first = -1; p.second = 0;
                }
            }//1번판
            else if (b[y][x] == 1) {
                if (p.first == -1 && p.second == 0) {
                    p.first = 0; p.second = 1;
                }
                else if (p.first == 0 && p.second == -1) {
                    p.first = 1; p.second = 0;
                }
            }//4번판
            else if (b[y][x] == 4) {
                if (p.first == 1 && p.second == 0) {
                    p.first = 0; p.second = -1;
                }
                else if (p.first == 0 && p.second == 1) {
                    p.first = -1; p.second = 0;
                }
            } //5번판
            else if (b[y][x] == 5) {
                p.first = -p.first; p.second = -p.second;
                answer--; //5번판에서는 움직임이 없다
            }

            d[y][x]--;
            if (d[y][x] <= 0) {
                b[y][x] = 0;
            }
        }
        
    }

    return answer;
}

int main() {
    int ans = solution({ {0,2,0}, {1,0,5}, {3,4,0 } }, { {0,2,0}, {2,0,1}, {2,1,0} });
    cout << ans << endl;
    ans = solution({ {0,2,0} , {0,5,0}, {0,0,0} }, { {0,2,0}, {0,1,0}, {0,0,0} });
    cout << ans << endl;
    ans = solution({ {0,0,2,0} , {1,0,3,2}, {0,0,0,0}, {3,0,0,4} }, 
        { {0, 0,2,0}, {5,0,2,5}, {0,0,0,0}, {5,0,0,5} });
    cout << ans << endl;
    ans = solution({ {5,5,5}, {5,5,5}, {5,5,5} }, { {1,1,1}, {2,3,1}, {2,1,6} });
    cout << ans << endl;
}