ACM篇:POJ3984--迷宫问题

水水

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

const int N = 5;
struct Point
{
    int x, y;
    int last;
    Point(int x=0, int y=0,int last=-1)
    {
        this->x = x; 
        this->y = y;
        this->last = last;
    }
};

const int ROW[] = {1, -1, 0, 0};
const int COL[] = {0, 0, 1, -1};

bool in_maze(const Point next)
{
    return (next.x < N && next.x >= 0 && next.y < N && next.y >= 0 );
}

void _print(const Point q[], const Point cur)
{
    if (cur.last != -1)_print(q, q[cur.last]);
    printf("(%d, %d)\n", cur.x, cur.y);
}
int main()
{
    bool maze[N][N];
    //  read    maze
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            scanf("%d", &maze[i][j]);

    //  bfs
    bool visit[N][N];
    memset(visit, 0, sizeof(visit));

    Point q[N*N+2];
    int head = 1;
    int tail = 2;
    while (head < tail)
    {
        Point cur = q[head];
        for (int i = 0; i < 4; i++)
        {
            Point next(cur.x+ROW[i], cur.y+COL[i], head);
            if (next.x == 4 && next.y == 4)
            {
                _print(q, next);
                return 0;
            }
            else if (in_maze(next) && !maze[next.x][next.y] && !visit[next.x][next.y])
            {
                visit[next.x][next.y] = true;
                q[tail++] = next;
            }
        }
        head++;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值