九度 1365 贝多芬第九交响曲

http://ac.jobdu.com/problem.php?id=1365

基本BFS,好像一个月没写题了。。。

写完这题继续看linux源码去。一定要坚持

 1 #include <stdio.h>
2 #include <string.h>
3 #include <queue>
4 using namespace std;
5 int N;
6 int num[102][102];
7 queue<int> Q;
8 int start_x,start_y,end_x,end_y;
9 int x_dire[]={-2,-2,-1,-1,1,1,2,2};
10 int y_dire[]={-1,1,-2,2,-2,2,-1,1};
11 bool legal_pos(int x,int y)
12 {
13 if(x>=1&&x<=N&&y>=1&&y<=N)
14 return true;
15 return false;
16 }
17 int calac()
18 {
19 while(!Q.empty()){
20 int x,y;
21 x=Q.front();
22 Q.pop();
23 y=Q.front();
24 Q.pop();
25 if(x==end_x&&y==end_y){
26 return num[x][y];
27 }
28 int i;
29 for(i=0;i<8;i++){
30 if(num[x+x_dire[i]][y+y_dire[i]]==-1&&legal_pos(x+x_dire[i],y+y_dire[i])){
31 Q.push(x+x_dire[i]);
32 Q.push(y+y_dire[i]);
33 num[x+x_dire[i]][y+y_dire[i]]=num[x][y]+1;
34 }
35 }
36 }
37 return -1;
38 }
39
40
41
42 int main()
43 {
44 while(scanf("%d",&N)!=EOF){
45 int i,j;
46 for(i=1;i<=N;i++)
47 for(j=1;j<=N;j++){
48 num[i][j]=-1;
49 }
50 scanf("%d%d%d%d",&start_x,&start_y,&end_x,&end_y);
51 while(!Q.empty())
52 Q.pop();
53 Q.push(start_x);
54 Q.push(start_y);
55 num[start_x][start_y]=0;
56 printf("%d\n",calac());
57 }
58 }



原文地址:https://www.cnblogs.com/yangce/p/2332011.html