Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // 7562-02
  4. //
  5. // Created by dhpark on 7/20/17.
  6. // Copyright © 2017 dhpark. All rights reserved.
  7. //
  8. // 나이트의 이동
  9.  
  10. #include <cstdio>
  11. #include <cstring>
  12. #include <queue>
  13. using namespace std;
  14.  
  15. struct coord {
  16. int x, y, depth;
  17. } tmp;
  18.  
  19. int t, l;
  20. int cnt;
  21. int mx[8] = { -2, -2, -1, -1, 1, 1, 2, 2 };
  22. int my[8] = { -1, 1, -2, 2, -2, 2, -1, 1 };
  23. int nx1, ny1, nx2, ny2;
  24. int map[300][300];
  25. int visited[300][300];
  26. queue<coord> q;
  27.  
  28. void bfs(int, int);
  29. bool valid(int, int);
  30.  
  31. int main(int argc, const char * argv[]) {
  32. scanf("%d", &t);
  33. while (t--) {
  34. cnt = 0;
  35. memset(visited, 0, sizeof(visited));
  36. while (!q.empty()) {
  37. q.pop();
  38. }
  39.  
  40. scanf("%d", &l);
  41. scanf("%d%d%d%d", &nx1, &ny1, &nx2, &ny2);
  42.  
  43. bfs(nx1, ny1);
  44. printf("%d\n", cnt);
  45. }
  46. return 0;
  47. }
  48.  
  49. void bfs(int x, int y){
  50. visited[x][y] = 1;
  51. tmp.x = x;
  52. tmp.y = y;
  53. tmp.depth = 0;
  54. q.push(tmp);
  55.  
  56. while (!q.empty()) {
  57. auto pos = q.front();
  58. q.pop();
  59.  
  60. if(pos.x == nx2 && pos.y == ny2){
  61. cnt = pos.depth;
  62. return;
  63. }
  64.  
  65. for(int i=0 ; i<8 ; i++){
  66. tmp.x = pos.x + mx[i];
  67. tmp.y = pos.y + my[i];
  68. tmp.depth = pos.depth + 1;
  69. if(valid(tmp.x, tmp.y) && !visited[tmp.x][tmp.y]){
  70. visited[tmp.x][tmp.y] = 1;
  71. q.push(tmp);
  72. }
  73. }
  74. }
  75. }
  76.  
  77. bool valid(int x, int y){
  78. if(x>=0 && x<l && y>=0 && y<l)
  79. return true;
  80. return false;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement