Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // 7562-02
- //
- // Created by dhpark on 7/20/17.
- // Copyright © 2017 dhpark. All rights reserved.
- //
- // 나이트의 이동
- #include <cstdio>
- #include <cstring>
- #include <queue>
- using namespace std;
- struct coord {
- int x, y, depth;
- } tmp;
- int t, l;
- int cnt;
- int mx[8] = { -2, -2, -1, -1, 1, 1, 2, 2 };
- int my[8] = { -1, 1, -2, 2, -2, 2, -1, 1 };
- int nx1, ny1, nx2, ny2;
- int map[300][300];
- int visited[300][300];
- queue<coord> q;
- void bfs(int, int);
- bool valid(int, int);
- int main(int argc, const char * argv[]) {
- scanf("%d", &t);
- while (t--) {
- cnt = 0;
- memset(visited, 0, sizeof(visited));
- while (!q.empty()) {
- q.pop();
- }
- scanf("%d", &l);
- scanf("%d%d%d%d", &nx1, &ny1, &nx2, &ny2);
- bfs(nx1, ny1);
- printf("%d\n", cnt);
- }
- return 0;
- }
- void bfs(int x, int y){
- visited[x][y] = 1;
- tmp.x = x;
- tmp.y = y;
- tmp.depth = 0;
- q.push(tmp);
- while (!q.empty()) {
- auto pos = q.front();
- q.pop();
- if(pos.x == nx2 && pos.y == ny2){
- cnt = pos.depth;
- return;
- }
- for(int i=0 ; i<8 ; i++){
- tmp.x = pos.x + mx[i];
- tmp.y = pos.y + my[i];
- tmp.depth = pos.depth + 1;
- if(valid(tmp.x, tmp.y) && !visited[tmp.x][tmp.y]){
- visited[tmp.x][tmp.y] = 1;
- q.push(tmp);
- }
- }
- }
- }
- bool valid(int x, int y){
- if(x>=0 && x<l && y>=0 && y<l)
- return true;
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement