POJ - 3984 迷宫问题

这道题应该算是 水题  搜索里面算是简单的了 唯一要想的就是 怎么让遍历的点重复出来 其实可以 在结构体里面再定义一个队列 对的 没错就是再定义一个 队列 然后 我们  当有一个 到了 重点 直接输出就行  话不多说 估计直接看代码 就行了  如果要是没有点特别的想法 直接暴力  直接就凉凉了。。。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <string>
using namespace std;
int p[10][10];
int hh[5]= {1,0,-1,0};
int kk[5]= {0,1,0,-1};
bool vis[5][5];
struct ppx
{
    int x,y;
} ag;
struct pp
{
    int xx,yy;
    queue<ppx>q;
} as,ad;
bool pd(int i,int j)
{
    if(i>=0&&i<5&&j>=0&&j<5&&!vis[i][j]&&!p[i][j])
        return 1;
    return 0;
}
void slove()
{
    memset(vis,0,sizeof(vis));
    queue<pp>qq;
    as.xx=0,as.yy=0;
    while(!as.q.empty())
        as.q.pop();
    ag.x=0,ag.y=0;
    as.q.push(ag);
       qq.push(as);
    while(!qq.empty())
    {
        ad=qq.front();
        qq.pop();
        if(ad.xx==4&&ad.yy==4)
        {
            while(!ad.q.empty())
            {
                ag=ad.q.front();
                ad.q.pop();
                printf("(%d, %d)\n",ag.x,ag.y);
            }
            return;
        }
        for(int i=0; i<4; i++)
        {
            as=ad;
            as.xx=ad.xx+kk[i];
            as.yy=ad.yy+hh[i];
            if(pd(as.xx,as.yy))
            {
                ag.x=as.xx,ag.y=as.yy;
                as.q.push(ag);
                qq.push(as);
                vis[as.xx][as.yy]=1;
            }
        }
    }
}
int main()
{
    for(int i=0; i<5; i++)
    {
        for(int j=0; j<5; j++)
        {
            scanf("%d",&p[i][j]);

        }
    }
    slove();
    return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_41071646/article/details/80210789