zoukankan      html  css  js  c++  java
  • 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.
    ",str[0],str[1],str[3],str[4],minx);
    38     }
    39 } 
  • 相关阅读:
    TLE: poj 1011 Sticks
    UVa 116 Unidirectional TSP
    csuoj 1215 稳定排序
    UVa 103 Stacking Boxes
    UVa 147 Dollars
    UVa 111 History Grading
    怎么在ASP.NET 2.0中使用Membership
    asp.net中如何删除cookie?
    ASP.NET中的HTTP模块和处理程序[收藏]
    NET开发中的一些小技巧
  • 原文地址:https://www.cnblogs.com/sasuke-/p/5144176.html
Copyright © 2011-2022 走看看