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 | 31 |
Tags
- BFS
- VR
- 자료구조
- 정렬
- 구현
- 투 포인터
- 백트래킹
- 누적 합
- 트리
- 그래프
- 백준
- DFS
- ue5
- Unreal Engine 5
- 다익스트라
- 시뮬레이션
- 알고리즘
- Team Fortress 2
- 유니온 파인드
- 브루트포스
- 유니티
- 다이나믹 프로그래밍
- 문자열
- 우선순위 큐
- c++
- 수학
- 그리디 알고리즘
- 스택
- 재귀
- XR Interaction Toolkit
Archives
- Today
- Total
1일1알
Judge - 1237 : 2보 전진을 위한 1보 후퇴 C++ 본문

두가지 방법으로 풀었다.
1. 실제로 2보 전진, 1보 후퇴하는식으로 bfs탐색으로 해결
2. 그냥 bfs탐색을 하면서 parent를 이용해서 경로를 저장한 뒤, 도착지점에서 출발지점으로 2보전진 1보후퇴
출발지점이 벽인 경우를 처음에 고려를 안해서 계속 틀리다보니 고민을 계속 하다가 두가지 방법이 나오게 되었다.
출발지점이 벽인 경우를 고려하면 둘 다 맞는 답이다.
#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 n;
struct location {
int row;
int col;
int cnt;
};
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
vector<vector<int>> board;
vector<vector<bool>> found;
pair<int, int> start = { 0,0 };
pair<int, int> target;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
board = vector<vector<int>>(n, vector<int>(n));
found = vector<vector<bool>>(n, vector<bool>(n, false));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> board[i][j];
}
}
target = { n - 1,n - 1 };
if (board[start.first][start.second] == 0 || board[target.first][target.second] == 0) {
cout << "-1" << "\n";
return 0;
}
queue<location> q;
q.push({ start.first,start.second,0 });
found[start.first][start.second] = true;
while (!q.empty()) {
auto curr = q.front();
q.pop();
bool isReached = false;
int ans = INT_MAX;
for (int i = 0; i < 4; i++) {
int nextRow = curr.row + dRow[i];
int nextCol = curr.col + dCol[i];
int nextCnt = curr.cnt + 1;
if (nextRow < 0 || nextRow >= n) continue;
if (nextCol < 0 || nextCol >= n) continue;
if (board[nextRow][nextCol] == 0) continue;
if (found[nextRow][nextCol]) continue;
if (nextRow == target.first && nextCol == target.second) {
isReached = true;
ans = min(ans, nextCnt);
}
for (int j = 0; j < 4; j++) {
int nnextRow = nextRow + dRow[j];
int nnextCol = nextCol + dCol[j];
int nnextCnt = nextCnt + 1;
if (nnextRow < 0 || nnextRow >= n) continue;
if (nnextCol < 0 || nnextCol >= n) continue;
if (board[nnextRow][nnextCol] == 0) continue;
if (found[nnextRow][nnextCol]) continue;
if (nnextRow == target.first && nnextCol == target.second) {
isReached = true;
ans = min(ans, nnextCnt);
}
q.push({ nextRow,nextCol,nnextCnt + 1 });
found[nextRow][nextCol] = true;
}
}
if (isReached) {
cout << ans << "\n";
return 0;
}
}
cout << -1 << "\n";
}
#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 n;
int dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
vector<vector<int>> board;
vector<vector<bool>> found;
vector<vector<pair<int, int>>> parent;
pair<int, int> start = { 0,0 };
pair<int, int> target;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
board = vector<vector<int>>(n, vector<int>(n));
found = vector<vector<bool>>(n, vector<bool>(n, false));
parent = vector<vector<pair<int, int>>>(n, vector<pair<int, int>>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> board[i][j];
}
}
target = { n - 1,n - 1 };
if (board[start.first][start.second] == 0 || board[target.first][target.second] == 0) {
cout << "-1" << "\n";
return 0;
}
queue<pair<int, int>> q;
q.push({ start.first,start.second});
found[start.first][start.second] = true;
parent[start.first][start.second] = start;
bool isPossible = false;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (curr.first == target.first && curr.second == target.second) {
isPossible = true;
break;
}
for (int i = 0; i < 4; i++) {
int nextRow = curr.first + dRow[i];
int nextCol = curr.second + dCol[i];
if (nextRow < 0 || nextRow >= n) continue;
if (nextCol < 0 || nextCol >= n) continue;
if (board[nextRow][nextCol] == 0) continue;
if (found[nextRow][nextCol]) continue;
q.push({ nextRow,nextCol });
found[nextRow][nextCol] = true;
parent[nextRow][nextCol] = { curr.first,curr.second };
}
}
if (!isPossible) {
cout << "-1" << "\n";
return 0;
}
pair<int, int> curr = target;
int cnt = 0;
while (true) {
if (curr == start) break;
cnt++;
pair<int, int> _parent = parent[curr.first][curr.second];
if (_parent == start) break;
cnt++;
pair<int, int> _pparent = parent[_parent.first][_parent.second];
if (_pparent == start) break;
cnt++;
curr = _parent;
}
cout << cnt << "\n";
}
'알고리즘' 카테고리의 다른 글
Judge - 1235 : Please In My Frontyard! C++ (0) | 2022.07.15 |
---|---|
Judge - 1170 : 크리스마스의 요정 C++ (0) | 2022.07.14 |
Judge - 1234 : 서로 다른 단어 하지만 우리는 하나 C++ (0) | 2022.07.13 |
백준 20040번 사이클 게임 C++ (0) | 2022.07.12 |
백준 4386번 별자리 만들기 C++ (0) | 2022.07.10 |