[kuangbin带你飞]专题一 简单搜索- K - 迷宫问题

#include 
#include 
#include 
#include 
#include 
using namespace std;
#define INF 0x3f3f3f

int s[10][10];

int vis[10][10];
int xx[] = {1,-1,0,0};
int yy[] = {0,0,1,-1};

struct node
{
    int x, y; 
}jud[10][10];

int main()
{
    
    for(int i = 0; i < 5; i++)
    {
        for(int j = 0; j < 5; j++) scanf("%d", &s[i][j]);
    }
    queue<node>q;
    node a;
    a.x = 0;
    a.y = 0;
    q.push(a);
    vis[0][0] = 1;
    while(!q.empty())
    {   
        node p = q.front(); q.pop();
        if(p.x==4&&p.y==4) break;
        for(int i = 0; i < 4; i++)
        {
            int tx = p.x+xx[i];
            int ty = p.y+yy[i];
            if(tx<0||ty<0||tx>4||ty>4) continue;
            if(!vis[tx][ty]&&!s[tx][ty]){
                vis[tx][ty] = 1;
                jud[tx][ty].x = p.x;
                jud[tx][ty].y = p.y;
                a.x = tx;
                a.y = ty;
                q.push(a);
            }
        }
    }
    vector<node>ans;
    a.x = 4;
    a.y = 4;
    ans.push_back(a);
    int x = jud[4][4].x;
    int y = jud[4][4].y;
    while(x!=0||y!=0)
    {
        a.x = x;
        a.y = y;
        ans.push_back(a);
        int nx = jud[x][y].x;
        int ny = jud[x][y].y;
        x = nx; y = ny;
    }
    a.x = 0;
    a.y = 0;
    ans.push_back(a);
    int cnt = ans.size();
    for(int i = cnt-1; i >= 0; i--)
    {
        node p = ans[i];
        printf("(%d, %d)\n", p.x, p.y);
    }
    return 0;
}

你可能感兴趣的:(kuangbin)