• 走迷宫


    #include<iostream>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    const int maxsize=16;
    int a[maxsize][maxsize]= {0};
    bool b[maxsize][maxsize]= {false};
    int flag=0;
    struct Point {
        int x,y;
        Point(int c,int d):x(c),y(d){}
    
    };
    int nextto[4][2]= {
    
        {0,-1},{-1,0},{0,1},{1,0}
    };
    vector<Point> path;
    int n,m,start_x,start_y,end_x,end_y;
    
    void search(int x,int y) {
    
        if(x==end_x&&y==end_y) {
            flag=1;
            for(int i=0; i<path.size()-1; i++) {
                printf("(%d,%d)->",path[i].x,path[i].y);
            }
            printf("(%d,%d)
    ",path[path.size()-1].x,path[path.size()-1].y);
            return;
        }
        for(int i=0; i<4; i++) {
            int newx=x+nextto[i][0];
            int newy=y+nextto[i][1];
            
            if(newx<1||newx>m||newy<1||newy>n||b[newx][newy]==true)continue;
            if(a[newx][newy]==1&&b[newx][newy]==false) {
                path.push_back(Point(newx,newy));
                b[newx][newy]=true;
                search(newx,newy);
                path.pop_back();
                b[newx][newy]=false;
            }
        }
    }
    int main() {
        while(scanf("%d %d",&m,&n)!=EOF) {
            flag=0;
            path.clear();
            memset(a,0,sizeof(a));
            memset(b,false,sizeof(b));
            for(int i=1; i<=m; i++) {
                for(int j=1; j<=n; j++) {
                    scanf("%d",&a[i][j]);
                }
            }
            scanf("%d %d",&start_x,&start_y);
            path.push_back(Point(start_x,start_y));
            b[start_x][start_y]=true;
            scanf("%d %d",&end_x,&end_y);
            search(start_x,start_y);
            if(flag==0) {
                printf("-1
    ");
            }
        }
    
    
        return 0;
    }
  • 相关阅读:
    hdoj 2544 最短路径
    树状数组 hdoj1166
    并查集学习
    1402大数相乘 FFT算法学习!
    hdu1014
    动态规划 简单题dp
    迷宫路径
    简单的动态规划dp
    poj 3282 (bFS)
    背包问题,看不懂,啊!!!!!!!!dp
  • 原文地址:https://www.cnblogs.com/tianyudizhua/p/13476345.html
Copyright © 2020-2023  润新知