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 |
Tags
- 구현
- 스택
- BFS
- 수학
- 우선순위 큐
- Unreal Engine 5
- 자료구조
- Team Fortress 2
- 그래프
- 백트래킹
- 정렬
- 투 포인터
- c++
- 브루트포스
- 유니온 파인드
- 다익스트라
- 그리디 알고리즘
- 알고리즘
- XR Interaction Toolkit
- 누적 합
- 트리
- 문자열
- VR
- 재귀
- 유니티
- 다이나믹 프로그래밍
- ue5
- DFS
- 백준
- 시뮬레이션
Archives
- Today
- Total
1일1알
백준 17129번 윌리암슨수액빨이딱따구리가 정보섬에 올라온 이유 C++ 본문
https://www.acmicpc.net/problem/17129
17129번: 윌리암슨수액빨이딱따구리가 정보섬에 올라온 이유
첫째 줄에 정보섬 2층의 크기 n과 m이 주어진다. (1 ≤ n,m ≤ 3000, 4 ≤ n×m ≤ 9×106) 이후 n행 m열에 걸쳐 0, 1, 2, 3, 4, 5로만 구성된 Ai,j가 주어진다. Ai,j와 Ai,j+1사이에 공백은 주어지지 않는다. 2,
www.acmicpc.net
bfs
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
vector<vector<int>> board;
vector<vector<bool>> found;
int n, m;
struct Info {
int row;
int col;
int moveCnt;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
board = vector<vector<int>>(n, vector<int>(m));
found = vector<vector<bool>>(n, vector<bool>(m, false));
queue<Info> q;
for (int i = 0; i < n; i++) {
string str;
cin >> str;
for (int j = 0; j < m; j++) {
board[i][j] = str[j] - '0';
if (board[i][j] == 2) {
q.push({ i,j,0 });
found[i][j] = true;
}
}
}
int ans = -1;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (board[curr.row][curr.col] >= 3 && board[curr.row][curr.col] <= 5) {
ans = curr.moveCnt;
break;
}
for (int i = 0; i < 4; i++) {
int nextRow = curr.row + dRow[i];
int nextCol = curr.col + dCol[i];
if (nextRow < 0 || nextRow >= n) continue;
if (nextCol < 0 || nextCol >= m) continue;
if (board[nextRow][nextCol] == 1) continue;
if (found[nextRow][nextCol]) continue;
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol,curr.moveCnt + 1 });
}
}
if (ans == -1) cout << "NIE";
else cout << "TAK\n" << ans;
}
'알고리즘' 카테고리의 다른 글
백준 14426번 접두사 찾기 C++ (0) | 2022.10.25 |
---|---|
백준 15724번 주지수 C++ (0) | 2022.10.24 |
백준 12869번 뮤탈리스크 C++ (0) | 2022.10.22 |
백준 14271번 그리드 게임 C++ (0) | 2022.10.14 |
백준 17352번 여러분의 다리가 되어 드리겠습니다! C++ (0) | 2022.10.12 |