• poj 3984 迷宫问题(bfs 打印路径)


    http://poj.org/problem?id=3984

    思路: 最基础的bfs  

              记录父节点坐标 并在最后打印路径

    stl queue: 

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #define mem(a,b) memset(a,b,sizeof(a))
    #define ll __int64
    using namespace std;
    struct M
    {
        int x,y;
        int fax,fay,val;
    };
    struct Road
    {
        int x,y;
    };
    int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
    M mat[10][10];
    Road r[50];
    void bfs()
    {
        int i,j;
        M now,next;
        queue<M> q;
        mat[1][1].val=1;
        q.push(mat[1][1]);
        while(!q.empty())
        {
            now=q.front();
            q.pop();
            if(now.x==5&&now.y==5)
            {
                int cnt=0;
                int fx=now.x,fy=now.y;
                while(1)
                {
    
                    if(fx==0&&fy==0) break;
                    r[cnt].x=mat[fx][fy].x;
                    r[cnt].y=mat[fx][fy].y;
                    cnt++;
                    int tx=fx,ty=fy;
                    fx=mat[tx][ty].fax;
                    fy=mat[tx][ty].fay;
                }
                for(int i=cnt-1; i>=0; i--)
                {
                    printf("(%d, %d)
    ",r[i].x-1,r[i].y-1);
                }
    
                return ;
            }
            for(i=0; i<4; i++)
            {
                int x=now.x+dir[i][0];
                int y=now.y+dir[i][1];
                if(mat[x][y].val==0)
                {
                    next.x=x;
                    next.y=y;
    
                    mat[x][y].fax=now.x;
                    mat[x][y].fay=now.y;
                    mat[x][y].val=1;
                    q.push(next);
                }
            }
        }
    }
    int main()
    {
        mem(mat,-1);
        int i,j;
        for(i=1; i<=5; i++)
        {
            for(j=1; j<=5; j++)
            {
                scanf("%d",&mat[i][j].val);
                mat[i][j].x=i;
                mat[i][j].y=j;
            }
        }
        mat[1][1].fax=0;
        mat[1][1].fay=0;
    
        bfs();
        return 0;
    }
    

      

    手写队列:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int mat[10][10];
    int vis[10][10];
    struct node
    {
        int x,y;
    };
    node que[100],print[100];
    int fa[100];
    int dir[4][2]= {0,1,0,-1,1,0,-1,0};
    bool ok(int x,int y)
    {
        if(x<0||x>4||y<0||y>4||vis[x][y]==1||mat[x][y]==1)
        {
            return false;
        }
        return true;
    }
    void bfs()
    {
        que[0].x=0;
        que[0].y=0;
        fa[0]=0;
        vis[0][0]=1;
        int x,y;
        int i,j,k;
        int head=0,rear=1;
        while(head<rear)
        {
            if(que[head].x==4&&que[head].y==4)
            {
                int cnt=0;
                while(head!=0)
                {
                    print[cnt].x=que[head].x;
                    print[cnt].y=que[head].y;
                    cnt++;
                    head=fa[head];
                }
                print[cnt].x=0;
                print[cnt].y=0;
                for(i=cnt; i>=0; i--)
                {
                    printf("(%d, %d)
    ",print[i].x,print[i].y);
                }
    
                return ;
            }
            for(i=0; i<4; i++)
            {
                x=que[head].x+dir[i][0];
                y=que[head].y+dir[i][1];
                if(ok(x,y))
                {
                    que[rear].x=x;
                    que[rear].y=y;
                    fa[rear]=head;
                    vis[x][y]=1;
                    rear++;
                }
            }
            head++;
        }
    }
    
    int main()
    {
        int i,j,k;
        memset(vis,0,sizeof(vis));
        for(i=0; i<5; i++)
        {
            for(j=0; j<5; j++)
            {
                scanf("%d",&mat[i][j]);
            }
        }
        bfs();
        return 0;
    }
    

      

      

  • 相关阅读:
    CSAcademy Or Problem
    BZOJ 4516 [Sdoi2016] 生成魔咒
    SPOJ7258 SUBLEX
    SPOJ1812 LCS2
    SPOJ1811 LCS
    SPOJ8222 NSUBSTR
    洛谷3804 【模板】后缀自动机
    SPOJ287 NETADMIN
    SPOJ1693 COCONUTS
    BZOJ5329 SDOI2018 战略游戏
  • 原文地址:https://www.cnblogs.com/sola1994/p/3915917.html
Copyright © 2020-2023  润新知