POJ2243-Knight Moves

http://poj.org/problem?id=2243

最简单的bfs输出下路径

#include<stdio.h>
#include<string.h>
struct
{
    int x,y,step;
} queue[10000],e;
bool mark[10][10];
int dir[8][2]= {2,1,1,2,-1,2,-2,1,-2,-1,-1,-2,1,-2,2,-1},x1,y1,x2,y2,i;;
char y11,y22;
int bfs()
{
    int start,tail,x,y,step;
    start=tail=0;
    e.x=x1;
    e.y=y1;
    e.step=0;
    queue[start]=e;
    while(start<=tail)
    {
        x=queue[start].x;
        y=queue[start].y;
        step=queue[start].step;
        start++;
        for(i=0;i<8;i++)
        {
            e.x=dir[i][0]+x;
            e.y=dir[i][1]+y;
            e.step=step+1;
            if(e.x<=0||e.y<=0||e.x>8||e.y>8||mark[e.x][e.y])
                continue;
            if((e.x==x2)&&(e.y==y2))
            {
                printf("To get from %c%d to %c%d takes %d knight moves.\n",y11,x1,y22,x2,e.step);
                return 0;
            }
            ++tail;
            queue[tail]=e;
            mark[e.x][e.y]=true;
        }
    }
}
int main(void)
{
    while(scanf("%c%d %c%d",&y11,&x1,&y22,&x2)!=EOF)
    {
        getchar();
        y1=y11-'a'+1;
        y2=y22-'a'+1;
        if((x1==x2)&&(y1==y2))
        {
            printf("To get from %c%d to %c%d takes 0 knight moves.\n",y11,x1,y22,x2);
            continue;
        }
        memset(mark,false,sizeof(mark));
        bfs();
    }
    return 0;
}

转载于:https://www.cnblogs.com/YogurtShen/archive/2012/08/29/2662174.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值