zoukankan      html  css  js  c++  java
  • 跳马(Knight Moves), ZOJ1091, POJ2243

    跳马(Knight Moves), ZOJ1091, POJ2243

    题目描述:

    给定象棋棋盘上两个位置 a 和 b,编写程序,计算马从位置 a 跳到位置 b 所需步数的最小值。

    输入描述:

    输入文件包含多个测试数据。每个测试数据占一行,为棋盘中的两个位置,用空格隔开。棋盘位置为两个字符组成的串,第 1 个字符为字母 a~h,代表棋盘中的列;第 2 个字符为数字字符1~8,代表棋盘中的行。

    输出描述:

    对输入文件中的每个测试数据,输出一行"To get from xx to yy takes n knight moves.", xx 和yy 分别为输入数据中的两个位置, n 为求得的最少步数。

    样例输入:

    样例输出:

    e2 e4

    a1 b2

    b2 c3

    a1 h8

    a1 h7

    h8 a1

    b1 c3

    f6 f6

    To get from e2 to e4 takes 2 knight moves.
    To get from a1 to b2 takes 4 knight moves.
    To get from b2 to c3 takes 2 knight moves.
    To get from a1 to h8 takes 6 knight moves.
    To get from a1 to h7 takes 5 knight moves.
    To get from h8 to a1 takes 6 knight moves.
    To get from b1 to c3 takes 1 knight moves.
    To get from f6 to f6 takes 0 knight moves.

    代码:

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<cstring>
     5 using namespace std;
     6 
     7 char s[8];
     8 int ex,ey,sx,sy;
     9 int dx[8]={1,2,2,1,-1,-2,-2,-1};
    10 int dy[8]={2,1,-1,-2,-2,-1,1,2};
    11 bool visit[10][10];
    12 struct node{
    13     int x,y,step;
    14 }cur,nxt;
    15 queue<node>q;
    16 
    17 void bfs(){
    18     if(ex==sx&&ey==sy){//特判,如果起点等于终点,输出0,结束 
    19         cout<<"To get from "<<char(ex+'a'-1)<<ey<<" to ";
    20         cout<<char(sx+'a'-1)<<sy<<" takes "<<"0"<<" knight moves."<<endl;
    21         return ;
    22     }
    23     while(!q.empty()) q.pop();//清空 
    24     memset(visit,0,sizeof(visit));
    25     cur.x=ex,cur.y=ey,cur.step=0;
    26     visit[ex][ey]=true;//标记 
    27     q.push(cur); //入队 
    28     while(!q.empty()){
    29         cur=q.front();q.pop();//取头元素 
    30         for(int i=0;i<8;i++){//解空间为8 
    31             int xx=cur.x+dx[i],yy=cur.y+dy[i];
    32             if(xx>0&&xx<=8&&yy>0&&yy<=8&&!visit[xx][yy]){//边界,是否搜过 
    33                 if(xx==sx&&yy==sy){//当找的终点的时,输出 
    34                     cout<<"To get from "<<char(ex+'a'-1)<<ey<<" to ";
    35                     cout<<char(sx+'a'-1)<<sy<<" takes "<<cur.step+1<<" knight moves."<<endl;
    36                     return ;
    37                 }
    38                 nxt.x=xx,nxt.y=yy,nxt.step=cur.step+1;
    39                 //如果没找到终点,就记录点的信息,让其入队,继续寻找 
    40                 visit[nxt.x][nxt.y]=true;
    41                 q.push(nxt); 
    42             }
    43         }
    44     }
    45 }
    46 
    47 int main(){
    48     while(scanf("%s",s)!=EOF){
    49         ex=s[0]-'a'+1,ey=s[1]-'0';//字符与数字间的转换 
    50         cin>>s;
    51         sx=s[0]-'a'+1,sy=s[1]-'0';
    52         bfs();
    53     }
    54 }

    思路:

       Bfs,字符输入一般转换成数字,有八种方向,解空间为8,特别注意字符与数字间的转化

  • 相关阅读:
    spring与springmvc父子容器
    spring容器BeanFactory简单例子
    spring整体架构
    css中".",",",“~”和“>”符号的意义
    CSS中的块级元素与行级元素
    java反射和动态代理
    thymeleaf的fragment例子
    编写一个简单的 JDBC 程序
    http://localhost/ 或 http://127.0.0.1/ 报错:HTTP 404 的解决办法
    教你如何清除 MyEclipse/Eclipse 中 Web Browser 和 Switch Workspace 的历史记录
  • 原文地址:https://www.cnblogs.com/wsdestdq/p/6764316.html
Copyright © 2011-2022 走看看