HDUKnight Moves

点击打开链接

题目链接

这题和石油那题特别像  

按照象棋中马走日的方法从一个点到另外一个点  求最短的步数

比较水的题~~~~~

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
int go[8][2] = {{-2,-1},{-2,1},{-1,-2},{1,-2},{-1,2},{1,2},{2,1},{2,-1}};
char n, N;
int m, M, sx, sy, ey, ex, sum = 0;
int vis[10][10],ans;
struct point
{
    int x,y;
    int step;
}p;
void bfs(int x1,int y1,int x2,int y2)
{
    p.x = x1,p.y = y1,p.step = 0;
    queue<point>q;
    q.push(p);
    point current, next;
    vis[x1][y1] = 1;
    while(!q.empty())
    {
        current = q.front();
        q.pop();
       for(int i = 0; i < 8; i++)
       {
            next.x = current.x + go[i][0];
            next.y = current.y + go[i][1];
            next.step = current.step + 1;
            if(next.x>=1&&next.x<=8&&next.y>=1&&next.y<=8&&vis[next.x][next.y]==0)
            {
                    if(next.x==x2&&next.y==y2)
                        {
                            sum = current.step + 1;
                            return ;
                        }
                   vis[next.x][next.y] = 1;
                   q.push(next);
            }
       }


    }
}
int main()
{
    while(cin >> n >> m >> N >> M)
    {
       sum = 0;
       memset(vis,0,sizeof(vis));
       sx = n - 96,sy = m;
       ex = N - 96,ey = M;
        bfs(sx,sy,ex,ey);
       printf("To get from %c%d to %c%d takes %d knight moves.\n",n,m,N,M,sum);
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值