안선생의 개발 블로그

C++ 13565 침투 본문

백준

C++ 13565 침투

안선생 2023. 2. 25. 21:38
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<math.h>
using namespace std;
int g[1001][1001= { 0 };
bool visit[1001][1001= { 0 };
int dx[4= { 0,1,-1,0 };
int dy[4= { 1,0,0,-1 };
int n, m, w = 0, b = 0;
bool wc = 0, bc = 0;
bool bfs(int x, int y)
{
    queue <pair<intint>> a;
    a.push({ x,y });
    visit[x][y] = 1;
    while (!a.empty())
    {
        int xx = a.front().first;
        int yy = a.front().second;
        if (xx == n - 1)
            return 1;
        a.pop();
        for (int i = 0; i < 4; i++)
        {
            int x1 = xx + dx[i];
            int y1 = yy + dy[i];
 
            if (x1 < 0 || x1 >= n || y1 < 0 || y1 >= m || visit[x1][y1]) continue;
 
            if (!g[x1][y1])
            {
                a.push({ x1,y1 });
                visit[x1][y1] = 1;
            }
        }
    }
 
    return 0;
}
 
int main()
{
    cin >> n >> m;
    string s;
    bool check = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> s;
        for (int j = 0; j < m; j++)
            g[i][j] = s[j] - '0';
    }
 
 
    for (int i = 0; i < 1; i++)
        for (int j = 0; j < m; j++)
            if (!g[i][j] && !visit[i][j])
            {
                if (bfs(i, j))
                {
                    cout << "YES";
                    return 0;
                }
            }
    cout << "NO";
    return 0;
}
 
cs

https://www.acmicpc.net/problem/13565

 

13565번: 침투

첫째 줄에는 격자의 크기를 나타내는  M (2 ≤ M ≤ 1,000) 과 N (2 ≤ N ≤ 1,000) 이 주어진다. M줄에 걸쳐서, N개의 0 또는 1 이 공백 없이 주어진다. 0은 전류가 잘 통하는 흰색, 1은 전류가 통하지 않

www.acmicpc.net

 

'백준' 카테고리의 다른 글

C++ 5014 스타트링크  (0) 2023.02.26
C++ 14940 쉬운 최단거리  (0) 2023.02.25
C++ 1303 전쟁 - 전투  (0) 2023.02.25
C++ 16953 A ->B  (0) 2023.02.25
C++ 11123 양 한마리... 양 두마리...  (0) 2023.02.24