HDU 1372

题意:模拟国际象棋马的走棋方式,和中国象棋一样马走日,8X8的棋盘,问从起点到终点的最短步数,国际象棋中数字代表行row,字母代表列column,

思路:记忆化深搜、

 

 1 #include<cstdio>
 2 #include<cstring>
 3 const int qq=20+5,no=1e7;
 4 int tx,ty,minx;
 5 char map[qq][qq];
 6 int dis[qq][qq];
 7 void dfs(int x,int y,int cnt)
 8 {
 9     if(x<=0||y<=0||x>8||y>8)    return;
10     if(cnt>=minx)    return;
11     if(cnt>=dis[x][y])    return;
12     if(x==tx&&y==ty)    if(cnt<minx)    minx=cnt;
13     dis[x][y]=cnt;
14     dfs(x+2,y+1,cnt+1);
15     dfs(x+2,y-1,cnt+1);
16     dfs(x-2,y+1,cnt+1);
17     dfs(x-2,y-1,cnt+1);
18     dfs(x+1,y+2,cnt+1);
19     dfs(x+1,y-2,cnt+1);
20     dfs(x-1,y+2,cnt+1);
21     dfs(x-1,y-2,cnt+1);
22     return;
23 }
24 int main()
25 {
26     char str[10];
27     while(scanf("%c%c%*c%c%c",&str[0],&str[1],&str[3],&str[4])!=EOF){
28         getchar();
29         int sx,sy;
30         sx=str[1]-'0';sy=str[0]-'a'+1;    //自己开始就没有+1,然后又调试了十几分钟 
31         tx=str[4]-'0';ty=str[3]-'a'+1;
32         for(int j,i=1;i<=8;++i)
33             for(j=1;j<=8;++j)
34                 dis[i][j]=no;
35         minx=no;
36         dfs(sx,sy,0);
37         printf("To get from %c%c to %c%c takes %d knight moves.\n",str[0],str[1],str[3],str[4],minx);
38     }
39 } 

 

posted @ 2016-01-20 10:09  我不萌、我要高冷  阅读(291)  评论(0编辑  收藏  举报