[kuangbin带你飞]专题一 简单搜索 K POJ 3984

来源:互联网 发布:h5 手机页面模版源码 编辑:程序博客网 时间:2024/06/04 11:03

题目地址:https://vjudge.net/contest/65959#problem/K

思路:保存路径的迷宫问题。用pre数组保存上一位置。

AC代码:

#include<iostream>#include<cstring>#include<cstdio>#include<stack>#include<queue>using namespace std;int map[5][5];bool vis[5][5];int xx[4]={-1,0,1,0};int yy[4]={0,-1,0,1};struct point{int x,y;};point pre[5][5];int main(){    for(int i=0;i<5;i++)        for(int j=0;j<5;j++)    {        scanf("%d",&map[i][j]);    }    memset(vis,false,sizeof(vis));    queue<point>q;    q.push(point{0,0});    while(!q.empty())    {        point now=q.front();        q.pop();        int x=now.x;        int y=now.y;        if(x==4 && y==4)        {        int i=4,j=4;        stack<point>k;        while(i!=0 || j!=0)        {        k.push(point{i,j});        int ii=pre[i][j].x;        int jj=pre[i][j].y;        i=ii;        j=jj;        }        k.push(point{0,0});        while(!k.empty())        {            point now=k.top();            k.pop();            printf("(%d, %d)\n",now.x,now.y);        }        break;        }        for(int i=0;i<4;i++)        {            int tempx=x+xx[i];            int tempy=y+yy[i];            if(tempx>0 &&tempx<5 &&tempy>=0 &&tempy<5 && !vis[tempx][tempy] && !map[tempx][tempy])            {                pre[tempx][tempy].x=x;                pre[tempx][tempy].y=y;                vis[tempx][tempy]=true;                q.push(point{tempx,tempy});            }        }    }}


0 0
原创粉丝点击