POJ 1915(Knight Moves)
简单BFS
思路很简单,就是从起点始,有八个选择可走(如果每个都合法的话)
一直到终点为止,我做麻烦了,其实可以用二维数组来表示下一步要走
的方向
用G++须加头文件#include <stdio.h>
#include<string.h>
1 #include <stdio.h>
2 #include<string.h>
3 #include <queue>
4 #include<iostream>
5 #include<queue>
6 using namespace std;
7 #define maxn 301
8 int T,n,x0,y0,xn,yn;
9 bool flag[maxn][maxn];
10 struct node
11 {
12 int x,y,step;
13 };
14 void BFS(int x0,int y0,int xn,int yn)
15 {
16 queue<node> q;
17 node first;
18 memset(flag,false,sizeof(flag));
19 first.x = x0,first.y = y0,first.step = 0;
20 q.push(first);
21 while(!q.empty())
22 {
23 node temp = q.front();
24 q.pop();
25 node next;
26 if(temp.x+1==xn && temp.y+2 == yn) //x+1,y+2
27 {
28 printf("%d\n",temp.step+1);
29 return;
30 }
31 if(temp.x+1 < n && temp.y+2 < n && !flag[temp.x+1][temp.y+2])
32 {
33 next.x = temp.x+1,next.y = temp.y+2,next.step = temp.step+1;
34 q.push(next);
35 flag[temp.x+1][temp.y+2] = true;
36 }
37 if(temp.x+2==xn && temp.y+1 == yn) //x+2,y+1
38 {
39 printf("%d\n",temp.step+1);
40 return;
41 }
42 if(temp.x+2 < n && temp.y+1 < n && !flag[temp.x+2][temp.y+1])
43 {
44 next.x = temp.x+2,next.y = temp.y+1,next.step = temp.step+1;
45 q.push(next);
46 flag[temp.x+2][temp.y+1] = true;
47 }
48 if(temp.x+1==xn && temp.y-2 == yn) //x+1,y-2
49 {
50 printf("%d\n",temp.step+1);
51 return;
52 }
53 if(temp.x+1 < n && temp.y-2 >= 0 && !flag[temp.x+1][temp.y-2])
54 {
55 next.x = temp.x+1,next.y = temp.y-2,next.step = temp.step+1;
56 q.push(next);
57 flag[temp.x+1][temp.y-2] = true;
58 }
59 if(temp.x+2==xn && temp.y-1 == yn) //x+2,y-1
60 {
61 printf("%d\n",temp.step+1);
62 return;
63 }
64 if(temp.x+2 < n && temp.y-1 >= 0 && !flag[temp.x+2][temp.y-1])
65 {
66 next.x = temp.x+2,next.y = temp.y-1,next.step = temp.step+1;
67 q.push(next);
68 flag[temp.x+2][temp.y-1] = true;
69 }
70 if(temp.x-2 ==xn && temp.y+1 == yn) //x-2,y+1
71 {
72 printf("%d\n",temp.step+1);
73 return;
74 }
75 if(temp.x-2 >= 0 && temp.y+1 < n && !flag[temp.x-2][temp.y+1])
76 {
77 next.x = temp.x-2,next.y = temp.y+1,next.step = temp.step+1;
78 q.push(next);
79 flag[temp.x-2][temp.y+1] = true;
80 }
81 if(temp.x-1==xn && temp.y+2 == yn) //x-1,y+2
82 {
83 printf("%d\n",temp.step+1);
84 return;
85 }
86 if(temp.x-1 >= 0 && temp.y+2 < n && !flag[temp.x-1][temp.y+2])
87 {
88 next.x = temp.x-1,next.y = temp.y+2,next.step = temp.step+1;
89 q.push(next);
90 flag[temp.x-1][temp.y+2] = true;
91 }
92 if(temp.x-1==xn && temp.y-2 == yn) //x-1,y-2
93 {
94 printf("%d\n",temp.step+1);
95 return;
96 }
97 if(temp.x-1 >= 0 && temp.y-2 >= 0 && !flag[temp.x-1][temp.y-2])
98 {
99 next.x = temp.x-1,next.y = temp.y-2,next.step = temp.step+1;
100 q.push(next);
101 flag[temp.x-1][temp.y-2] = true;
102 }
103 if(temp.x-2==xn && temp.y-1 == yn) //x-2,y-1
104 {
105 printf("%d\n",temp.step+1);
106 return;
107 }
108 if(temp.x-2 >= 0 && temp.y-1 >=0 && !flag[temp.x-2][temp.y-1])
109 {
110 next.x = temp.x-2,next.y = temp.y-1,next.step = temp.step+1;
111 q.push(next);
112 flag[temp.x-2][temp.y-1] = true;
113 }
114 }
115 }
116 int main()
117 {
118 //freopen("1915.txt","r",stdin);
119 scanf("%d",&T);
120 while(T--)
121 {
122 scanf("%d",&n);
123 scanf("%d%d%d%d",&x0,&y0,&xn,&yn);
124 if(x0 == xn && y0 == yn)
125 {
126 printf("0\n");
127 continue;
128 }
129 BFS(x0,y0,xn,yn);
130 }
131 return 0;
132 }