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
- 백준
- 2565번
- directx12 그리기 연산2
- dx12
- 백준 1260 c++
- Unreal
- DirectX12
- 4134번
- 11286번
- 2d 박스충돌
- lis응용
- 루트서명
- 드래곤플라이트 모작
- Perforce
- 그리기 연산
- WinAPI
- 다음소수
- 뱀과사다리게임
- 애니메이션
- C++
- 2075번
- 백준 c++ 24479번
- 24779
- unrealengine
- 백준 24444 c++
- 다익스트라
- dx12 정리
- 랜더링 파이프라인
Archives
- Today
- Total
game-1 님의 블로그
[백준] 7562번 C++ 나이트의 이동 본문



나이트의 이동 문제로 대표적인 그래프 탐색 문제이다.
bfs를 이용하여 풀었으며, 정답률이 높기때문에 이 문제를 입문으로 그래프문제를 풀면 좋을 것 같다.
풀이)
#include<iostream>
#include<queue>
#include<vector>
#include<array>
using namespace std;
const int MAX = 301;
int len;
int cnt{};
int dx[8] = { 1,2,2,1,-1,-2,-2,-1 };
int dy[8] = { 2,1,-1,-2,-2,-1,1,2 };
/* 방문했는지 확인할 visited
* bfs 돌릴 queue
* 가중치 계산할 map
*/
int visited[MAX][MAX]{ {} };
int map[MAX][MAX]{ {} };
void bfs(int x, int y) {
queue<pair<int, int>> q;
q.push({ x,y });
visited[y][x] = 1;
while (not q.empty()) {
int a = q.front().first;
int b = q.front().second;
q.pop();
for (int i = 0; i < 8; ++i) {
int nx = a + dx[i];
int ny = b + dy[i];
if (0 <= nx && nx < len && 0 <= ny && ny < len && not visited[ny][nx] && map[ny][nx] == 1) {
q.push({nx,ny});
visited[ny][nx] = visited[b][a] + 1;
}
}
}
}
int main()
{
int t;
cin >> t;
while (t--) {
int x, y, x2, y2;
cin >> len;
cin >> x >> y >> x2 >> y2;
//배열 초기화
cnt = 0;
for (int i = 0; i < len; ++i) {
for (int j = 0; j < len; ++j) {
map[i][j] = 1;
visited[i][j] = 0;
}
}
bfs(x, y);
cout << visited[y2][x2] - 1 << '\n';
}
}

'백준 문제풀이' 카테고리의 다른 글
| [백준] C++ 11286번:절댓값 힙 (1) | 2025.01.09 |
|---|---|
| [백준] 1927번: 최소 힙 C++ (0) | 2025.01.08 |
| [백준] C++ 2178번: 미로 탐색 (0) | 2025.01.03 |
| [백준] C++ 1012번: 유기농 배추 BFS (1) | 2025.01.02 |
| [백준] C++ 2667번: 단지번호붙이기 BFS (0) | 2025.01.01 |