poj 3984 迷宫问题(bfs)

刚才碰到一个搜索题,写错了,好伤心,不过那题下边就是这个,刷个水题

#include <iostream>
using std::cin;
using std::cout;
using std::endl;

struct node
{
    int x,y;
    int par;
};
int next[4][2] = {{0,1},{1,0},{0,-1},{-1,0}};

int G[10][10];
node que[500];

void Print(int index)
{
    if(que[index].par == -1)
    {
        cout << "(" << que[index].x << ", " << que[index].y << ")" << endl;
        return;
    }
    Print(que[index].par);
    cout << "(" << que[index].x << ", " << que[index].y << ")" << endl;
}

int main()
{
    for(int i = 0; i < 5; ++i)
        for(int j = 0; j < 5; ++j)
            cin >> G[i][j];
    int head = 0,tail = 0;
    que[tail].x = que[tail].y = 0;
    G[0][0] = 1;
    que[tail].par = -1;
    ++tail;
    int tx,ty;
    node cur;
    while(head < tail)
    {
        node cur = que[head++];
        if(cur.x == 4 && cur.y == 4)
            break;
        for(int k = 0; k < 4; ++k)
        {
            tx = cur.x + next[k][0];
            ty = cur.y + next[k][1];
            if(tx < 0 || ty < 0 || tx > 4 || ty > 4)
                continue;
            if(!G[tx][ty])
            {
                G[tx][ty] = 1;
                que[tail].x = tx;
                que[tail].y = ty;
                que[tail].par = head-1;
                ++tail;
            }
        }
    }
    Print(head-1);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值