Skip to content

Commit

Permalink
7562 나이트의 이동 / BFS
Browse files Browse the repository at this point in the history
  • Loading branch information
cjy8922 authored Mar 28, 2021
1 parent 1b6df80 commit cddaedf
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions 2021_03/BJ_7562.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// https://www.acmicpc.net/problem/7562

#include <vector>
#include <string>
#include <iostream>
#include <queue>
using namespace std;

int dx[8] = { 1, 2, 2, 1, -1, -2, -2, -1 };
int dy[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);

int T;
cin >> T;

for (int test_case = 1; test_case <= T; test_case++) {
int size, start_x, start_y, end_x, end_y;
cin >> size >> start_x >> start_y >> end_x >> end_y;

vector<vector<int>> res(size, vector<int>(size));
queue<pair<int, int>> q;
q.push(make_pair(start_x, start_y));
res[start_x][start_y] = 0;

while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();

if (x == end_x && y == end_y) {
cout << res[end_x][end_y] << endl;
break;
}

for (int i = 0; i < 8; i++) {
int rx = x + dx[i];
int ry = y + dy[i];

if (rx >= 0 && ry >= 0 && rx < size && ry < size) {
if (res[rx][ry] == 0) {
q.push(make_pair(rx, ry));
res[rx][ry] = res[x][y] + 1;
}
}
}
}
}

return 0;
}

0 comments on commit cddaedf

Please sign in to comment.