zoukankan      html  css  js  c++  java
  • uva 439 Knight Moves

    题意:问你国际象棋中马从某一坐标走到另一坐标最少需要多少步

    解题思路:简单广搜!

    解题代码:

    // File Name: uva439.c
    // Author: darkdream
    // Created Time: 2013年05月25日 星期六 14时24分17秒
    
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<time.h>
    #include<math.h>
    struct node 
    {
       int x,y,step;
    };
    
    struct node list[10000];
    int xadd[] = {1,1,-1,-1,2,2,-2,-2};
    int yadd[] = {2,-2,2,-2,1,-1,1,-1};
    int is(int x)
    {
      if( x >= 1 && x <= 8 )
          return 1;
       return 0 ;
    }
    int visit[10][10];
    int main(){
    
       //freopen("/home/plac/problem/input.txt","r",stdin);
       //freopen("/home/plac/problem/output.txt","w",stdout);
       int a, b;
       char ca,cb;
       while(scanf("%c%d %c%d",&ca,&a,&cb,&b) != EOF)
       {
        getchar();
         memset(list,0,sizeof(list));
         memset(visit,0,sizeof(visit));
         int dx = cb -'a' +1;
         int dy = b ;
         list[1].x = ca - 'a' +1;
         list[1].y = a;
         list[1].step = 0;
         visit[list[1].x][list[1].y] = 1;
         int low = 1 , high = 1;
         while(low <= high)
         {
             if(list[low].x == dx && list[low].y == dy)
             {
                break;
             }
            for(int i = 0 ; i <= 7 ;i ++)
            {
              int tx = list[low].x + xadd[i];
              int ty = list[low].y + yadd[i];
              if(!visit[tx][ty] && is(tx)&&is(ty))
              {
                  visit[tx][ty] = 1;
                  high ++;
                  list[high].x = tx;
                  list[high].y = ty;
                  list[high].step = list[low].step+1;
              
              }
            }
            low++;
         }
         printf("To get from %c%d to %c%d takes %d knight moves.\n",ca,a,cb,b,list[low].step);
       }
    return 0 ;
    }
    View Code
    没有梦想,何谈远方
  • 相关阅读:
    攻击方法
    Paillier 同态加密方案
    $EL Gamal$ 密码方案的椭圆曲线形式
    【hbase】hbase的基本使用
    【linux】创建用户,查看用户
    【linux】删除命令
    【linux】上传文件也可以直接拖动文件到xshell中
    【linux】安装pip时报错
    【Linux】界面快捷键
    【linux】Ubuntu无法下载mysql
  • 原文地址:https://www.cnblogs.com/zyue/p/3098773.html
Copyright © 2011-2022 走看看