hdu1372 广搜大水

#include<cstdio>
#include<iostream>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
int a,b;
char s1,s2;
int step[20][120];
queue<pair<int,int> > q;
int dir[8][2]={-2,-1,-1,-2,2,-1,1,-2,1,2,2,1,-1,2,-2,1};
int visit[20][20];
void bfs(int m,int n){
    visit[m][n]=1;
    pair<int,int> tmp;
    while(!q.empty())
      q.pop();
    q.push(make_pair(m,n));
    while(!q.empty()){
        tmp=q.front();
        q.pop();
        if(tmp.first==s2-'a'&&tmp.second==b-1){
            //cout<<s1<<a<<s2<<b<<endl;
           printf("To get from %c%d to %c%d takes %d knight moves.\n",s1,a,s2,b,step[tmp.first][tmp.second]);
           return;
        }
        for(int k=0;k<8;k++){
          int ii=tmp.first+dir[k][0];
          int jj=tmp.second+dir[k][1];
          if(ii>=0&&jj>=0&&ii<8&&jj<8&&!visit[ii][jj]){
            visit[ii][jj]=1;
            step[ii][jj]=step[tmp.first][tmp.second]+1;
            q.push(make_pair(ii,jj));
          }
        }




    }




}
int main(){
    while(scanf("%c%d %c%d",&s1,&a,&s2,&b)!=EOF){
         //cout<<s1<<a<<s2<<b<<endl;
         memset(visit,0,sizeof(visit));
         memset(step,0,sizeof(step));
         bfs(s1-'a',a-1);




    }





}

你可能感兴趣的:(广搜)