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 |
Tags
- cs
- 그래프
- 브루트포스
- OS
- 오퍼레이팅시스템
- 자료구조
- 컴공
- 북리뷰
- coding
- 코딩
- 컴공과
- Operating System
- vector
- 백준
- Computer science
- DP
- 정석학술정보관
- 문제풀이
- 오에스
- 코테
- 스택
- 너비우선탐색
- Stack
- 컴퓨터공학과
- 알고리즘
- c++
- bfs
- 정석
- 개발
- 구현
Archives
- Today
- Total
Little Jay
[C++] 백준 7562번 - 나이트의 이동 본문
간단한 BFS 문제이다.
오랜만에 무지성으로 풀고 한번에 통과했다
미로탐색이랑 유사한 문제이다
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
int t, l, sx, sy, fx, fy;
int cnt[300][300];
bool visited[300][300];
int table[300][300];
const int dx[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
const int dy[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int main() {
cin >> t;
while (t--) {
cin >> l;
memset(visited, 0, sizeof(visited));
for (int i = 0; i < l; i++) {
for (int j = 0; j < l; j++) {
table[i][j] = 0;
cnt[i][j] = 0;
}
}
cin >> sx >> sy;
cin >> fx >> fy;
queue<pair<int, int>> q;
q.push({ sx, sy });
visited[sx][sy] = true;
cnt[sx][sy] = 0;
while (!q.empty()) {
auto current = q.front();
q.pop();
for (int dir = 0; dir < 8; dir++) {
int nx = current.first + dx[dir];
int ny = current.second + dy[dir];
if (nx < 0 || ny < 0 || nx >= l || ny >= l)
continue;
if (visited[nx][ny] == true)
continue;
visited[nx][ny] = true;
q.push({ nx, ny });
cnt[nx][ny] = cnt[current.first][current.second] + 1;
}
}
cout << cnt[fx][fy] << '\n';
}
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 2468번 - 안전영역 (0) | 2021.11.19 |
---|---|
[C++] 백준 5076번 - Web Pages (0) | 2021.11.16 |
[C++] 백준 2178번 - 미로 탐색 (0) | 2021.11.12 |
[C++] 백준 17298번 - 오큰수 (0) | 2021.11.08 |
[C++] 백준 15652번 - N과 M (4) (0) | 2021.11.06 |
Comments