zoukankan      html  css  js  c++  java
  • POJ 3984 迷宫问题

    传送门:http://poj.org/problem?id=3984

    简单的搜索。

    实现代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    
    const int MAXN=5;
    struct Node{
        int x;
        int y;
        int pre;
        Node(){};
        Node(int x,int y,int pre):x(x),y(y),pre(pre){};
    };
    
    int maze[MAXN][MAXN];
    bool vis[MAXN][MAXN];
    
    int dx[]={1,-1,0,0};
    int dy[]={0,0,-1,1};
    
    Node que[MAXN*MAXN*MAXN];
    
    void print(int head){
        if(que[head].pre==-1){
            printf("(%d, %d)
    ",que[head].x,que[head].y);
            return;
        }
        print(que[head].pre);
        printf("(%d, %d)
    ",que[head].x,que[head].y);
    
    }
    
    int main(){
        for(int i=0;i<MAXN;i++)
            for(int j=0;j<MAXN;j++)
                scanf("%d",&maze[i][j]);
    
        memset(vis,false,sizeof(vis));
        Node st=Node(0,0,-1);
        int head=0,tail=0;
        que[tail++]=st;
        vis[0][0]=true;
    
        while(head<tail){
            Node tmp=que[head];
            int x=tmp.x;
            int y=tmp.y;
    
            if(x==4&&y==4)
                break;
            for(int i=0;i<4;i++){
                int tx=x+dx[i];
                int ty=y+dy[i];
    
                if(tx<0||tx>=MAXN||ty<0||ty>=MAXN)
                    continue;
    
                if(vis[tx][ty]||maze[tx][ty]==1)
                    continue;
                vis[tx][ty]=true;
                que[tail].x=tx;
                que[tail].y=ty;
                que[tail].pre=head;
                tail++;
            }
            head++;
        }
        print(head);
        return 0;
    
    }
  • 相关阅读:
    数组
    课堂验证性实验总结
    《大道至简》第二章读后感
    大道至简第一章伪代码
    大道至简
    python学习笔记1
    19maven依赖冲突
    18SSM资源整合2
    18SSM资源整合
    17mybatis注解开发
  • 原文地址:https://www.cnblogs.com/IKnowYou0/p/6661478.html
Copyright © 2011-2022 走看看