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
- 시뮬레이션
- 문자열
- 그리디 알고리즘
- 재귀
- DFS
- 유니온 파인드
- 백준
- 정렬
- 투 포인터
- 수학
- ue5
- VR
- 다익스트라
- 알고리즘
- BFS
- 구현
- 유니티
- 그래프
- 우선순위 큐
- c++
- Team Fortress 2
- 자료구조
- 스택
- Unreal Engine 5
- 브루트포스
- 누적 합
- XR Interaction Toolkit
- 트리
- 다이나믹 프로그래밍
- 백트래킹
Archives
- Today
- Total
1일1알
백준 14923번 미로 탈출 C++ 본문
https://www.acmicpc.net/problem/14923
14923번: 미로 탈출
홍익이는 사악한 마법사의 꾐에 속아 N x M 미로 (Hx, Hy) 위치에 떨어졌다. 다행히도 홍익이는 마법사가 만든 미로의 탈출 위치(Ex, Ey)를 알고 있다. 하지만 미로에는 곳곳에 마법사가 설치한 벽이
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;
struct Info {
int row;
int col;
int moveCnt;
int useMagic;
};
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
int n, m;
int hr, hc, er, ec;
vector<vector<int>> board;
vector<vector<vector<bool>>> found;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> hr >> hc >> er >> ec;
board = vector<vector<int>>(n + 1, vector<int>(m + 1));
found = vector<vector<vector<bool>>>(n + 1, vector<vector<bool>>(m + 1, vector<bool>(2, false)));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> board[i][j];
}
}
queue<Info> q;
q.push({ hr,hc,0,0 });
found[hr][hc][0] = true;
int ans = -1;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (curr.row == er && curr.col == ec) {
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] == 0) {
if (found[nextRow][nextCol][curr.useMagic]) continue;
found[nextRow][nextCol][curr.useMagic] = true;
q.push({ nextRow,nextCol,curr.moveCnt + 1,curr.useMagic });
}
else {
if (curr.useMagic == 1) continue;
int nextUseMagic = 1;
if (found[nextRow][nextCol][nextUseMagic]) continue;
found[nextRow][nextCol][nextUseMagic] = true;
q.push({ nextRow,nextCol,curr.moveCnt + 1,nextUseMagic });
}
}
}
cout << ans;
}
'알고리즘' 카테고리의 다른 글
백준 3005번 크로스워드 퍼즐 쳐다보기 C++ (0) | 2023.01.07 |
---|---|
백준 16437번 양 구출 작전 C++ (0) | 2023.01.06 |
백준 13903번 출근 C++ (0) | 2023.01.04 |
백준 3980번 선발 명단 C++ (0) | 2023.01.03 |
백준 5567번 결혼식 C++ (0) | 2022.12.30 |