티스토리 뷰
https://www.acmicpc.net/problem/7562
7562번: 나이트의 이동
문제 체스판 위에 한 나이트가 놓여져 있다. 나이트가 한 번에 이동할 수 있는 칸은 아래 그림에 나와있다. 나이트가 이동하려고 하는 칸이 주어진다. 나이트는 몇 번 움직이면 이 칸으로 이동할
www.acmicpc.net
백준 소스코드 [C++] 7562 나이트의 이동
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
bool check[304][304];
int arr[304][304];
int n, m, startx, starty, destx, desty;
pair<int, int>pp, p[8] = { {-1,2} ,{-2,1}, {-2,-1}, {-1,-2} ,{2,1},{1,2},{1,-2},{2,-1} };
queue <pair<int, int> > q;
void bfs() {
while (q.empty() != true) {
pp = q.front();
q.pop();
if (pp.first == desty + 2 && pp.second == destx + 2) {
while (q.empty() != true) q.pop();
return;
}
for (int a = 0;a < 8;a++)
if (check[pp.first + p[a].first][pp.second + p[a].second] == true)
{
check[pp.first + p[a].first][pp.second + p[a].second] = false;
q.push({ pp.first + p[a].first, pp.second + p[a].second });
arr[pp.first + p[a].first][pp.second + p[a].second]
= min(arr[pp.first + p[a].first][pp.second + p[a].second], arr[pp.first][pp.second] + 1);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for (int x = 0;x < n;x++) {
cin >> m;
cin >> startx >> starty;
cin >> destx >> desty;
for (int i = 0;i < 304;i++)for (int j = 0;j < 304;j++) check[i][j] = false;
for (int i = 2;i < m+2;i++)for (int j = 2;j < m+2;j++) {
check[i][j] = true;
arr[i][j] = 987654321;
}
check[starty + 2][startx + 2] = false;
arr[starty + 2][startx + 2] = 0;
q.push({ starty + 2,startx + 2 });
bfs();
cout << arr[desty + 2][destx + 2] << '\n';
}
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 4677 Oil Deposits (0) | 2020.08.23 |
---|---|
백준 소스코드 [C++] 11123 양 한마리 양 두마리 (0) | 2020.08.23 |
백준 소스코드 [C++] 4963 섬의 개수 (0) | 2020.08.22 |
백준 소스코드 [C++] 1012 유기농 배추 (0) | 2020.08.22 |
백준 소스코드 [C++] 1343 폴리오미노 (0) | 2020.08.21 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- State Restoration
- 벨만포드 알고리즘
- IOS
- WWDC19
- 최단경로문제
- 벨만포드 시간복잡도
- 네트워크 플로우
- rxswift
- WWDC16
- 코딩대회
- 포드 풀커슨 알고리즘
- 최대 매칭
- 부스트캠프 6기
- Testable
- observeOn
- HIG
- 최단경로 알고리즘
- 다익스트라 시간복잡도
- 에드몬드 카프 알고리즘
- CPU와 Memory
- 컴퓨터 추상화
- WWDC17
- mach-o
- 네트워크 유량
- 강한 순환 참조
- MeTal
- 최단경로 문제
- WWDC21
- test coverage
- CompositionalLayout
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함