POJ 1915

1. 简单的bfs,有用双向bfs的,但是为了赶进度,以后用双向bfs实现

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>

using namespace std;
int dir[8][2] = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}};
int flag[301][301], step[301][301];
int n, len, i;
struct Node
{
    int x, y;
} s, t;
void bfs()
{
    queue<Node> q;
    q.push(s);
    while (!q.empty())
    {
        Node temp = q.front(), nex;
        flag[temp.x][temp.y] = 1;
        if (temp.x == t.x && temp.y == t.y)
        {
            cout << step[temp.x][temp.y] << endl;
            return;
        }
        q.pop();
        for (i = 0; i < 8; i++)
        {
            nex.x = temp.x + dir[i][0];
            nex.y = temp.y + dir[i][1];
            if (nex.x < len && nex.x >= 0 && nex.y < len && nex.y >= 0 && !flag[nex.x][nex.y])
            {
                q.push(nex);
                step[nex.x][nex.y] = step[temp.x][temp.y] + 1;
                flag[nex.x][nex.y] = 1;
            }
        }
    }
}
int main()
{
    cin >> n;
    while (n--)
    {
        cin >> len >> s.x >> s.y >> t.x >> t.y;
        memset(flag, 0, sizeof(flag));
        memset(step, 0, sizeof(step));
        step[s.x][s.y] = 0;
        bfs();
    }
    return 0;
}


posted on 2012-07-07 15:13  赵乐ACM 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/dollarzhaole/archive/2012/07/07/3188904.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值