티스토리 뷰
문제 출처: 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
가장 윗줄이 0인 경우 큐에 넣어 BFS를 돌려주고 BFS 도중 y가 M-1에 도착할 경우 YES를 출력하고 종료한다.
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<cstring>
#include<math.h>
#include<queue>
#include<vector>
using namespace std;
typedef pair<int, int>P;
int M, N;
int arr[1001][1001];
int visited[1001][1001];
int movepos[4][2] = { {0,1},{0,-1},{1,0},{-1,0} };
bool canMove(int x, int y) {
if (x < 0 || x >= M)return false;
if (y < 0 || y >= N)return false;
return true;
}
int main(void)
{
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> N >> M;
for (int i = 0; i < N; i++) {
string temp;
cin >> temp;
for (int j = 0; j < M; j++) {
arr[i][j] = temp[j] - '0';
}
}
queue<P>q;
for (int i = 0; i < M; i++) {
if (arr[0][i] == 0) {
q.push(P(0, i));
visited[0][i] = 1;
}
}
while (!q.empty()) {
P curr = q.front(); q.pop();
int x = curr.second;
int y = curr.first;
if (y == N - 1) {
cout << "YES";
return 0;
}
for (int i = 0; i < 4; i++) {
int gox, goy;
gox = movepos[i][0] + x;
goy = movepos[i][1] + y;
if (canMove(gox, goy) == false)continue;
if (visited[goy][gox] == 1)continue;
if (arr[goy][gox] == 1)continue;
visited[goy][gox] = 1;
q.push(P(goy, gox));
}
}
cout << "NO";
}'PS' 카테고리의 다른 글
| [백준] 1759번 암호 만들기 C++ (0) | 2022.07.19 |
|---|---|
| [백준] 1182번 부분수열의 합 C++ (0) | 2022.07.18 |
| [백준] 2644번 촌수계산 (0) | 2022.07.14 |
| [백준] 9322번 철벽 보안 알고리즘 C++ (0) | 2022.07.13 |
| [백준] 14402번 야근 C++ (0) | 2022.07.12 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 골목길
- 백준
- 중위 표기식 후위 표기식으로 변환
- 소셜네트워킹어플리케이션
- 6539
- 1918
- 타임머신
- 1738
- C++
- 벨만포드
- 스택
- 7511
- 상범빌딩
- 후위 표기식
- 벨만-포드
- 어린왕자 C++
- 6018
- 골목길C++
- 1004
- 11657
- tea time
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
글 보관함
