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
- Unreal Engine 5
- XR Interaction Toolkit
- DFS
- 투 포인터
- 우선순위 큐
- 유니온 파인드
- ue5
- 트리
- 알고리즘
- c++
- 재귀
- 브루트포스
- 그래프
- VR
- 유니티
- 다익스트라
- 자료구조
- 시뮬레이션
- 백트래킹
- 수학
- 다이나믹 프로그래밍
- 문자열
- 누적 합
- 백준
- 정렬
- 구현
- 그리디 알고리즘
- BFS
- Team Fortress 2
- 스택
Archives
- Today
- Total
1일1알
백준 2194번 유닛 이동시키기 C++ 본문
https://www.acmicpc.net/problem/2194
2194번: 유닛 이동시키기
첫째 줄에 다섯 개의 정수 N, M(1 ≤ N, M ≤ 500), A, B(1 ≤ A, B ≤ 10), K(0 ≤ K ≤ 100,000)가 주어진다. 다음 K개의 줄에는 장애물이 설치된 위치(행 번호, 열 번호)가 주어진다. 그 다음 줄에는 시작점의
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 dRow[4] = { -1,0,1,0 };
int dCol[4] = { 0,1,0,-1 };
int n, m, a, b, k;
vector<vector<int>> board;
vector<vector<bool>> found;
pair<int, int> targetPos;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> a >> b >> k;
board = vector<vector<int>>(n + 1, vector<int>(m + 1, 0));
found = vector<vector<bool>>(n + 1, vector<bool>(m + 1, false));
for (int i = 0; i < k; i++) {
int r, c;
cin >> r >> c;
board[r][c] = -1;
}
int sr, sc;
int er, ec;
cin >> sr >> sc >> er >> ec;
queue<Info> q;
q.push({ sr,sc,0 });
found[sr][sc] = true;
targetPos = { er,ec };
int ans = -1;
while (!q.empty()) {
auto curr = q.front();
q.pop();
if (curr.row == targetPos.first && curr.col == targetPos.second) {
ans = curr.moveCnt;
break;
}
for (int i = 0; i < 4; i++) {
bool canGo = true;
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 (found[nextRow][nextCol]) continue;
if (board[nextRow][nextCol] == -1) continue;
int nnextRow = nextRow + a - 1;
int nnextCol = nextCol + b - 1;
if (nnextRow <= 0 || nnextRow > n) continue;
if (nnextCol <= 0 || nnextCol > m) continue;
for (int j = nextRow; j <= nnextRow; j++) {
for (int k = nextCol; k <= nnextCol; k++) {
if (board[j][k] == -1) canGo = false;
}
}
if (canGo == false) continue;
q.push({ nextRow,nextCol,curr.moveCnt + 1 });
found[nextRow][nextCol] = true;
}
}
cout << ans;
}
'알고리즘' 카테고리의 다른 글
백준 19638번 센티와 마법의 뿅망치 C++ (0) | 2023.02.12 |
---|---|
백준 21921번 블로그 C++ (0) | 2023.02.11 |
백준 1911번 흙길 보수하기 C++ (0) | 2023.02.08 |
백준 2258번 정육점 C++ (0) | 2023.02.07 |
백준 1735번 분수 합 C++ (0) | 2023.02.06 |