zoj 1091 Knight Moves

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=91

解题思路:BFS搜索

  1 ///////////////////////////////////////////////////////////////////////////
  2 //problem_id: zoj 1091
  3 //user_id: SCNU20102200088
  4 ///////////////////////////////////////////////////////////////////////////
  5 
  6 #include <algorithm>
  7 #include <iostream>
  8 #include <iterator>
  9 #include <iomanip>
 10 #include <cstring>
 11 #include <cstdlib>
 12 #include <string>
 13 #include <vector>
 14 #include <cstdio>
 15 #include <cctype>
 16 #include <cmath>
 17 #include <queue>
 18 #include <stack>
 19 #include <list>
 20 #include <set>
 21 #include <map>
 22 using namespace std;
 23 
 24 ///////////////////////////////////////////////////////////////////////////
 25 typedef long long LL;
 26 const double PI=acos(-1.0);
 27 
 28 const int x4[]={-1,0,1,0};
 29 const int y4[]={0,1,0,-1};
 30 const int x8[]={-1,-1,0,1,1,1,0,-1};
 31 const int y8[]={0,1,1,1,0,-1,-1,-1};
 32 
 33 typedef int T;
 34 T max(T a,T b){ return a>b? a:b; }
 35 T min(T a,T b){ return a<b? a:b; }
 36 ///////////////////////////////////////////////////////////////////////////
 37 
 38 ///////////////////////////////////////////////////////////////////////////
 39 //Add Code:
 40 int Ex,Ey,Min;
 41 bool flag[10][10];
 42 const int xx[]={-2,-1,1,2,2,1,-1,-2};
 43 const int yy[]={1,2,2,1,-1,-2,-2,-1};
 44 
 45 struct Node{
 46     int x,y,step;
 47     Node(int i,int j,int s):x(i),y(j),step(s){}
 48 };
 49 
 50 queue<Node> q;
 51 
 52 void BFS(int i,int j){
 53     if(i==Ex && j==Ey){
 54         Min=0;
 55         return ;
 56     }
 57     memset(flag,0,sizeof(flag));
 58     flag[i][j]=1;
 59     Node node(i,j,0);
 60     q.push(node);
 61     while(!q.empty()){
 62         Node temp=q.front();
 63         q.pop();
 64         for(int p=0;p<8;p++){
 65             int x=temp.x+xx[p],y=temp.y+yy[p];
 66             if(x<1 || x>8 || y<1 || y>8 || flag[x][y]) continue;
 67             int step=temp.step+1;
 68             flag[x][y]=1;
 69             if(x==Ex && y==Ey){
 70                 Min=step;
 71                 while(!q.empty()) q.pop();
 72                 return ;
 73             }
 74             else{
 75                 Node res(x,y,step);
 76                 q.push(res);
 77             }
 78         }
 79     }
 80 }
 81 ///////////////////////////////////////////////////////////////////////////
 82 
 83 int main(){
 84     ///////////////////////////////////////////////////////////////////////
 85     //Add code:
 86     char s[5],e[5];
 87     while(scanf("%s%s",s,e)!=EOF){
 88         int Sx=s[0]-'a'+1,Sy=s[1]-'0';
 89         Ex=e[0]-'a'+1,Ey=e[1]-'0';
 90         BFS(Sx,Sy);
 91         printf("To get from %s to %s takes %d knight moves.\n",s,e,Min);
 92     }
 93     ///////////////////////////////////////////////////////////////////////
 94     return 0;
 95 }
 96 
 97 ///////////////////////////////////////////////////////////////////////////
 98 /*
 99 Testcase:
100 Input:
101 e2 e4
102 a1 b2
103 b2 c3
104 a1 h8
105 a1 h7
106 h8 a1
107 b1 c3
108 f6 f6
109 Output:
110 To get from e2 to e4 takes 2 knight moves.
111 To get from a1 to b2 takes 4 knight moves.
112 To get from b2 to c3 takes 2 knight moves.
113 To get from a1 to h8 takes 6 knight moves.
114 To get from a1 to h7 takes 5 knight moves.
115 To get from h8 to a1 takes 6 knight moves.
116 To get from b1 to c3 takes 1 knight moves.
117 To get from f6 to f6 takes 0 knight moves.
118 */
119 ///////////////////////////////////////////////////////////////////////////

posted on 2013-08-22 10:59  SCNU20102200088  阅读(174)  评论(0编辑  收藏  举报

导航