HDU 1372(骑士周游问题)

  1 //仍然bfs+判重 

  2 #include <iostream>

  3 #include <stdio.h>

  4 #include <cstring>

  5 #include <queue>

  6 using namespace std;

  7 typedef struct Node

  8 {

  9     int x,y;

 10     int step;

 11 }Node;

 12 Node ch[1000];

 13 bool vis[10][10];

 14 int bfs(int r1,int c1,int r2,int c2)

 15 {

 16     int i,j,k,t;

 17     memset(ch,0,sizeof(ch));

 18     memset(vis,false,sizeof(vis));

 19     ch[1].x = r1,ch[1].y = c1;

 20     ch[1].step = 0;

 21     queue <Node > q;

 22     while(!q.empty())

 23         q.pop();

 24     q.push(ch[1]);

 25     Node temp;

 26     while(!q.empty())

 27     {

 28         Node head = q.front();

 29         q.pop();

 30         //non-lvalue in assignment :等号写成了赋值号 

 31         if(head.x == r2&&head.y == c2)

 32             return head.step;

 33         else

 34         {//向八个方向搜索

 35         //忘加上界啦 

 36             int col = head.y,row = head.x;

 37             int ans = head.step;

 38             temp.step = 0;

 39             if(!vis[row-2][col-1]&&row>=3&&col>=2&&row<=8&&col<=8)

 40             {

 41                 vis[row-2][col-1] = true;

 42                 temp.x=row-2,temp.y=col-1,temp.step+=ans+1;

 43                 q.push(temp);

 44                 temp.step=0;

 45             } 

 46             if(!vis[row-1][col-2]&&row>=2&&col>=3&&row<=8&&col<=8)

 47             {

 48                 vis[row-1][col-2] = true;

 49                 temp.x=row-1,temp.y=col-2,temp.step+=ans+1;

 50                 q.push(temp);

 51                 temp.step=0;

 52             }

 53             if(!vis[row+1][col-2]&&row>=1&&col>=3&&row<=7&&col<=8)

 54             {

 55                 vis[row+1][col-2] = true;

 56                 temp.x=row+1,temp.y=col-2,temp.step+=ans+1;

 57                 q.push(temp);

 58                 temp.step=0;

 59             }

 60             if(!vis[row+2][col-1]&&row>=1&&col>=2&&row<=6&&col<=8)

 61             {

 62                 vis[row+2][col-1] = true;

 63                 temp.x=row+2,temp.y=col-1,temp.step+=ans+1;

 64                 q.push(temp);

 65                 temp.step=0;

 66             }

 67             if(!vis[row+2][col+1]&&row>=1&&col>=1&&row<=6&&col<=7)

 68             {

 69                 vis[row+2][col+1] = true;

 70                 temp.x=row+2,temp.y=col+1,temp.step+=ans+1;

 71                 q.push(temp);

 72                 temp.step=0;

 73             }

 74             if(!vis[row+1][col+2]&&row>=1&&col>=1&&row<=7&&col<=6)

 75             {

 76                 vis[row+1][col+2] = true;

 77                 temp.x=row+1,temp.y=col+2,temp.step+=ans+1;

 78                 q.push(temp);

 79                 temp.step=0;

 80             }

 81             if(!vis[row-1][col+2]&&row>=2&&col>=1&&row<=8&&col<=6)

 82             {

 83                 vis[row-1][col+2] = true;

 84                 temp.x=row-1,temp.y=col+2,temp.step+=ans+1;

 85                 q.push(temp);

 86                 temp.step=0;

 87             }

 88             if(!vis[row-2][col+1]&&row>=3&&col>=1&&row<=8&&col<=7)

 89             {

 90                 vis[row-2][col+1] = true;

 91                 temp.x=row-2,temp.y=col+1,temp.step+=ans+1;

 92                 q.push(temp);

 93                 temp.step=0;

 94             }

 95         }

 96     }

 97 }

 98 int main()

 99 {

100     int i,j,k,t;

101     char c11,c22;

102     int r1,r2;

103     while(scanf("%c%d %c%d",&c11,&r1,&c22,&r2)!=EOF)

104     {

105         int c1 = c11-'a'+1;//下标0不用

106         int c2 = c22-'a'+1;//下标0不用

107         int ans = bfs(r1,c1,r2,c2);

108         cout<<"To get from "<<c11<<r1<<" to "<<c22<<r2<<" takes "<<ans<<" knight moves."<<endl;

109         getchar();

110     }

111     return 0;

112 }

113 

114 

115          

116         

117         
 1 //网上的 

 2 #include <iostream>

 3 #include <queue>

 4 using namespace std;

 5 struct Node

 6 {

 7     int c;

 8     int r;

 9     int lev;

10 };

11 queue <Node> Q;

12 int a[] = {0,-2,-2,-1,-1,1,1,2,2};

13 int b[] = {0,-1,1,-2,2,-2,2,-1,1};

14 int GetRe(Node s,Node e)

15 {

16     int n = 0,i;

17     while(!Q.empty())

18         Q.pop();

19     if(e.c == s.c && e.r == s.r)

20         return 0;

21     Q.push(s);

22     Node front;

23     Node tmp;

24     while(!Q.empty())

25     {

26         front = Q.front();

27         Q.pop();

28         for(i = 1; i <= 8; i ++)

29         {

30             tmp.c = b[i] + front.c;

31             tmp.r = a[i] + front.r;

32             tmp.lev = front.lev + 1;

33             if(e.c == tmp.c && e.r == tmp.r)

34                 return tmp.lev;

35             if(tmp.c >= 1 && tmp.c <= 8 && tmp.r >= 1 && tmp.r <= 8)

36                 Q.push(tmp);

37         }

38     }

39 

40 }

41 int main()

42 {

43     char s[3],e[3];

44     Node start,end;

45     while(cin >> s >> e)

46     {

47         start.c = s[0] - 'a' + 1;

48         start.r = s[1] - '0';

49         start.lev = 0;

50         end.c = e[0] - 'a' + 1;

51         end.r = e[1] - '0';

52         cout<<"To get from "<<s<<" to "<<e<<" takes "<<GetRe(start,end)<<" knight moves."<<endl;

53     }

54     return 0;

55 }

 

你可能感兴趣的:(HDU)