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
- 구현
- 스택
- 누적 합
- 그리디 알고리즘
- 다이나믹 프로그래밍
- c++
- 유니온 파인드
- 그래프
- 백준
- 시뮬레이션
- Team Fortress 2
- ue5
- VR
- 자료구조
- 브루트포스
- 백트래킹
- 정렬
- 투 포인터
- DFS
- XR Interaction Toolkit
- BFS
- 트리
- 다익스트라
- 재귀
- Unreal Engine 5
- 알고리즘
- 유니티
- 우선순위 큐
- 수학
- 문자열
Archives
- Today
- Total
1일1알
백준 16509번 장군 C++ 본문
https://www.acmicpc.net/problem/16509
16509번: 장군
오랜만에 휴가를 나온 호근이는 문득 동아리방에 있는 장기가 하고 싶어졌다. 하지만 장기를 오랫동안 하지 않은 탓인지 예전에는 잘 쓰던 상을 제대로 쓰는 것이 너무 힘들었다. 호근이를 위해
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[8] = { -1,-1, 0, 1, 1, 1, 0,-1 };
int dCol[8] = { 0, 1, 1, 1, 0,-1,-1,-1 };
int seq[8][3] = { {0,1,1},{0,7,7},{2,1,1},{2,3,3},{4,3,3},{4,5,5},{6,7,7},{6,5,5} };
struct Info {
int row;
int col;
int moveCnt;
};
vector<vector<int>> board(10, vector<int>(9, -1));
vector<vector<bool>> found(10, vector<bool>(9, false));
pair<int, int> start;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> start.first >> start.second;
int rowK, colK;
cin >> rowK >> colK;
board[rowK][colK] = 1;
queue<Info> q;
q.push({ start.first,start.second,0 });
found[start.first][start.second] = true;
int ans = -1;
while (!q.empty()) {
Info curr = q.front();
q.pop();
if (board[curr.row][curr.col] == 1) {
ans = curr.moveCnt;
break;
}
for (int i = 0; i < 8; i++) {
int nextRow = curr.row;
int nextCol = curr.col;
bool possible = true;
for (int j = 0; j < 3; j++) {
nextRow += dRow[seq[i][j]];
nextCol += dCol[seq[i][j]];
if (nextRow < 0 || nextRow >= 10 || nextCol < 0 || nextCol >= 9) {
possible = false;
break;
}
if (j!=2 && board[nextRow][nextCol]==1) {
possible = false;
break;
}
}
if (possible == false) continue;
if (found[nextRow][nextCol]) continue;
found[nextRow][nextCol] = true;
q.push({ nextRow,nextCol,curr.moveCnt + 1 });
}
}
cout << ans;
}
'알고리즘' 카테고리의 다른 글
백준 5002번 도어맨 C++ (0) | 2023.05.20 |
---|---|
백준 11637번 인기 투표 C++ (1) | 2023.05.19 |
백준 17413번 단어 뒤집기 C++ (0) | 2023.05.15 |
백준 17085번 십자가 2개 놓기 C++ (1) | 2023.05.14 |
백준 11536번 줄 세우기 C++ (0) | 2023.05.13 |