• toc {:toc}

문제 ν™•μΈν•˜κΈ°

풀이

outer side μ—μ„œ inner side 둜 μ΄λ™ν•˜λŠ” 상황을 κ°€μ •ν•˜κΈ° μœ„ν•΄μ„œ 1 ν–‰μ˜ μ „κΈ°κ°€ ν†΅ν•˜λŠ” λΆ€λΆ„λ§Œ 큐에 μ§‘μ–΄ λ„£λŠ”λ‹€. BFS λ₯Ό μ΄μš©ν•΄μ„œ νƒμƒ‰ν•˜κ³  νƒμƒ‰ν•˜λŠ” λ™μ•ˆ λ§ˆμ§€λ§‰ 행에 λ„λ‹¬ν•˜λ©΄ inner side 둜 λ‚˜κ°ˆ 수 있기 λ•Œλ¬Έμ— λ§ˆμ§€λ§‰ 행에 도달할 경우 YES λ₯Ό 좜λ ₯ν•˜κ³  ν”„λ‘œκ·Έλž¨μ„ μ’…λ£Œν•œλ‹€.
while 문이 λλ‚˜λŠ” λ™μ•ˆμ—λ„ ν”„λ‘œκ·Έλž¨μ΄ μ’…λ£Œλ˜μ§€ μ•ŠλŠ” κ²½μš°λŠ” λ§ˆμ§€λ§‰ 행에 λ„λ‹¬ν•˜μ§€ μ•Šμ€ 것이기 λ•Œλ¬Έμ— NO λ₯Ό 좜λ ₯ν•˜κ³  ν”„λ‘œκ·Έλž¨μ„ μ’…λ£Œν•œλ‹€.

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define X first
#define Y second
 
int board[1003][1003];
int dx[4] = {0, 0, -1, 1};
int dy[4] = {1, -1, 0, 0};
queue <pair<int, int>> q;
 
int main(){
    ios::sync_with_stdio(0); cin.tie(0);
    
    int M, N;
 
    cin >> M >> N;
    
    for(int i = 0; i < M; i++){
        for(int j = 0; j < N; j++){
            char ch;
            cin >> ch;
            if(ch == '0') board[i][j] = 0;
            else board[i][j] = 1;
 
            if(i == 0 && board[i][j] == 0){
                board[i][j] = -1;
                q.push({i, j});
            }
        }
    }
 
    while(!q.empty()){
        auto cur = q.front(); q.pop();
        for(auto dir = 0; dir < 4; dir++){
            int nx = cur.X + dx[dir];
            int ny = cur.Y + dy[dir];
            if(nx < 0 || nx >= M || ny < 0 || ny >= N) continue;
            if(board[nx][ny] == -1 || board[nx][ny] == 1) continue;
            if(nx == M-1){
                cout << "YES" << endl;
                return 0;
            }
            q.push({nx, ny});
            board[nx][ny] = -1;
        }
    }
 
    cout << "NO" << endl;
 
    return 0;
}