알고리즘

[알고리즘] 백준 17070 - 파이프 옮기기 1

blueberrysoda 2024. 7. 14. 23:59
#include <iostream>
#include <queue>
using namespace std;

int N;
int Arr[17][17];
queue<pair<int, pair<int, int>>> Q;

int main(){
    cin >> N;
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){
            cin >> Arr[i][j];
        }
    }

    Q.push({0, {0, 1}});
    int Ans = 0;

    while(Q.empty() == false){
        int d = Q.front().first;
        int y = Q.front().second.first;
        int x = Q.front().second.second;
        Q.pop();

        if(y == N - 1 && x == N - 1){
            Ans++;
            continue;
        }

        if(d == 0){
            if(x < N - 1 && Arr[y][x + 1] == 0){
                Q.push({0, {y, x + 1}});
            }
            if(x < N - 1 && y < N - 1 && Arr[y][x+1] == 0 && Arr[y+1][x] == 0 && Arr[y+1][x+1] == 0){
                Q.push({2, {y + 1, x + 1}});
            }
        }
        else if(d == 1){
            if(y < N - 1 && Arr[y + 1][x] == 0){
                Q.push({1, {y + 1, x}});
            }
            if(x < N - 1 && y < N - 1 && Arr[y][x+1] == 0 && Arr[y+1][x] == 0 && Arr[y+1][x+1] == 0){
                Q.push({2, {y + 1, x + 1}});
            }
        }
        else if(d == 2){
            if(x < N - 1 && Arr[y][x + 1] == 0){
                Q.push({0, {y, x + 1}});
            }
            if(y < N - 1 && Arr[y + 1][x] == 0){
                Q.push({1, {y + 1, x}});
            }
            if(x < N - 1 && y < N - 1 && Arr[y][x+1] == 0 && Arr[y+1][x] == 0 && Arr[y+1][x+1] == 0){
                Q.push({2, {y + 1, x + 1}});
            }
        }
    }
    cout << Ans << "\n";
    return 0;
}