zoj1091(广度搜索

码出来了,不过不知道这种写法标准不,回去看看数据结构。

PS:又修改了一下。

看下时间,最长的是深度,不过内存少。然后第一次写的广度,最后是这个广度的版本。


#include <iostream>
#include <cstring>
using namespace std;


int step1[8] = {1,1,2,2,-1,-1,-2,-2};
int step2[8] = {2,-2,1,-1,2,-2,1,-1};
int knight[8][8];
int Queuex[10000],Queuey[10000];
int rear = 0,fron = -1;
void bfsFind(int st1,int st2,int moves)//广度搜索
{
    knight[st1][st2] = moves;
    while(rear != fron)
    {
        fron += 1;
        for(int m = 0;m < 8;m++)
        {
            if(st1 + step1[m]< 0 || st1 + step1[m]> 7 || st2 + step2[m]< 0 || st2 + step2[m]> 7 || moves+1 >= knight[st1 + step1[m]][st2 + step2[m]])
                continue;
            Queuex[rear] = st1 + step1[m];
            Queuey[rear] = st2 + step2[m];
            rear++;
            knight[st1 + step1[m]][st2 + step2[m]] = moves+1;
        }
        st1 = Queuex[fron];
        st2 = Queuey[fron];
        moves = knight[Queuex[fron]][Queuey[fron]];
    }
}
int main()
{
    string a,b;
    while(cin >> a >> b)
    {
        rear = 0,fron = -1;
        memset(knight,100,sizeof(knight));
        bfsFind(a[0]-'a',a[1]-'1',0);
        cout << "To get from "<< a << " to " << b << " takes " << knight[b[0] - 'a'][b[1] - '1'] << " knight moves." << endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值