Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 자료구조
- 그래프
- 북리뷰
- 정석학술정보관
- 너비우선탐색
- 컴공과
- cs
- 개발
- Operating System
- c++
- 컴공
- bfs
- 알고리즘
- 스택
- coding
- 정석
- 컴퓨터공학과
- 문제풀이
- 오퍼레이팅시스템
- OS
- 백준
- 코테
- 브루트포스
- Stack
- vector
- 코딩
- DP
- 오에스
- Computer science
- 구현
Archives
- Today
- Total
Little Jay
[C++] 백준 13565번 - 침투 본문
입력받는것을 계속 int로 받다가 실패했다.
띄어쓰기가 없는 것은 char이나 string으로 받아야 하는 것에 유의하자.
n-1일때 return true 해야하는데 m-1로 계속 써서 실패했다.
항상 조건을 잘 보도록 하자.
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
int n, m;
int board[1005][1005];
bool visited[1005][1005];
const int dx[4] = { 1, -1, 0, 0 };
const int dy[4] = { 0, 0, 1, -1 };
bool bfs(int y, int x) {
visited[y][x] = true;
queue<pair<int, int>> q;
q.push({ y, x });
while (!q.empty()) {
auto current = q.front(); q.pop();
if (current.first == n - 1)
return true;
for (int dir = 0; dir < 4; dir++) {
int ny = current.first + dy[dir];
int nx = current.second + dx[dir];
if (nx < 0 || nx >= m || ny < 0 || ny >= n) continue;
if (!visited[ny][nx] && board[ny][nx] == 0) {
visited[ny][nx] = true;
q.push({ ny, nx });
}
}
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char x; cin >> x;
board[i][j] = x - '0';
}
}
bool ans = false;
for (int i = 0; i < m; i++) {
if (board[0][i] == 0)
ans = bfs(0, i);
if (ans == true) break;
}
if (ans) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 17127번 - 벚꽃이 정보섬에 피어난 이유 (0) | 2022.05.09 |
---|---|
[C++] 백준 10552번 - DOM (0) | 2022.05.03 |
[C++] 백준 14464번 - 소가 길을 건너간 이유 4 (0) | 2022.04.27 |
[C++] 백준 6679번 - 싱기한 네자리 숫자 (0) | 2022.03.20 |
[C++] 백준 1655번 - 가운데를 말해요 (0) | 2022.03.14 |
Comments