• poj 2935 Basic Wall Maze bfs+路径记录


    Basic Wall Maze
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 2876   Accepted: 1301   Special Judge

    Description

    In this problem you have to solve a very simple maze consisting of:

    1. a 6 by 6 grid of unit squares
    2. 3 walls of length between 1 and 6 which are placed either horizontally or vertically to separate squares
    3. one start and one end marker

    A maze may look like this:

    You have to find a shortest path between the square with the start marker and the square with the end marker. Only moves between adjacent grid squares are allowed; adjacent means that the grid squares share an edge and are not separated by a wall. It is not allowed to leave the grid.

    Input

    The input consists of several test cases. Each test case consists of five lines: The first line contains the column and row number of the square with the start marker, the second line the column and row number of the square with the end marker. The third, fourth and fifth lines specify the locations of the three walls. The location of a wall is specified by either the position of its left end point followed by the position of its right end point (in case of a horizontal wall) or the position of its upper end point followed by the position of its lower end point (in case of a vertical wall). The position of a wall end point is given as the distance from the left side of the grid followed by the distance from the upper side of the grid.

    You may assume that the three walls don’t intersect with each other, although they may touch at some grid corner, and that the wall endpoints are on the grid. Moreover, there will always be a valid path from the start marker to the end marker. Note that the sample input specifies the maze from the picture above.

    The last test case is followed by a line containing two zeros.

    Output

    For each test case print a description of a shortest path from the start marker to the end marker. The description should specify the direction of every move (‘N’ for up, ‘E’ for right, ‘S’ for down and ‘W’ for left).

    There can be more than one shortest path, in this case you can print any of them.

    Sample Input

    1 6
    2 6
    0 0 1 0
    1 5 1 6
    1 5 3 5
    0 0

    Sample Output

    NEEESWW


    我的思路是先找到路径,将路径存起来,再输出方向。地图可以用map[x][y][4]来记录,第三个参数4用来记录这个点是够可以上下左右走。例如map[1][2][0]=1,说明可以在(1,2)这个点向上走,若map[1][2][1]=1,说明可以在(1,2)这个点向下走。那么在判断是否可以走这个点时,要注意

    int dir[][2]= {{-1,0},{1,0},{0,-1},{0,1}}这些方向是否与map[x][y][4]这个方向判断相对应,例如dir[1][]应该对应于map[][][1](在我的代码里代表相下走)



    #include<stdio.h>
    #include<iostream>
    #include<string>
    #include<string.h>
    #include<algorithm>
    #include<math.h>
    #include<map>
    #include<queue>
    #include<vector>
    using namespace std;
    struct point
    {
        int x,y,pre;
    } que[1000];//用来模拟队列
    int dir[][2]= {{-1,0},{1,0},{0,-1},{0,1}};//上下左右四个方向
    int mymap[100][100][4];//这个[4]用来记录这个点是够可以上下左右走
    int vis[100][100];//用来记录这个点是否走过
    int front,rear;//相当于队列的指针
    int cnt=0;//用来计数(最后路径个数)
    point ans[100];//用来保存最后路径
    void print(int s)//得到最后路径
    {
        if(s!=-1)
        {
            print(que[s].pre);
            ans[cnt].x=que[s].x;
            ans[cnt].y=que[s].y;
            cnt++;
        }
    
    }
    void bfs(int bx,int by,int ex,int ey)//bfs
    {
    
        int i,j,tmpx,tmpy;
        point tmp1,tmp2;
        tmp1.x=bx;
        tmp1.y=by;
        tmp1.pre=-1;
        front=rear=0;
        vis[bx][by]=1;
        que[rear++]=tmp1;
        while(rear>front)
        {
            tmp1=que[front];
            for(i=0; i<4; i++)
            {
                tmpx=tmp1.x+dir[i][0];
                tmpy=tmp1.y+dir[i][1];
    //           printf("1*%d %d %d
    ",tmpx,tmpy,i);
                if(tmpx>0&&tmpx<=6&&tmpy>0&&tmpy<=6&&!vis[tmpx][tmpy]&&mymap[tmp1.x][tmp1.y][i]==0)
                {
    //这里要注意,应该是判断tmp1这个点是够可以向四个方向走,而不是(tmpx,tmpy)这个点
                    vis[tmpx][tmpy]=1;
                    tmp2.x=tmpx;
                    tmp2.y=tmpy;
                    tmp2.pre=front;
                    que[rear++]=tmp2;
                }
                if(vis[ex][ey])
                {
                    print(front);
                    return;
                }
            }
            //  printf("*********
    ");
            front++;
        }
    }
    
    
    int main()
    {
        int i,j,a,b,c,d,ex,ey,bx,by;
        while(scanf("%d%d",&by,&bx)!=EOF)
        {
            if(by==0&&bx==0)break;
            scanf("%d%d",&ey,&ex);
            memset(vis,0,sizeof(vis));
            memset(mymap,0,sizeof(mymap));
            for(i=0; i<3; i++)
            {
                scanf("%d%d%d%d",&a,&b,&c,&d);
                if(a==c)
                {
                    if(b>d) swap(b,d);
                    for( j=b+1; j<=d; j++)
                    {
                        if(a) mymap[j][a][3]=1;
                        if(a!=6)mymap[j][a+1][2]=1;
                    }
                }
                if(b==d)
                {
                    if(a>c) swap(a,c);
                    for(int j=a+1; j<=c; j++)
                    {
                        if(b) mymap[b][j][1]=1;
                        if(b!=6) mymap[b+1][j][0]=1;
                    }
                }
            }
            bfs(bx,by,ex,ey);
    
            
            
            ans[cnt].x=ex;//这里要注意到达终点时应该记录终点的位置,bfs中并未记录
            ans[cnt].y=ey;
            cnt++;
            
            
            for(i=1; i<cnt; i++)
            {
                if(ans[i-1].x==ans[i].x&&ans[i-1].y<ans[i].y)printf("E");
                else if(ans[i-1].x==ans[i].x&&ans[i-1].y>ans[i].y)printf("W");
                else if(ans[i-1].y==ans[i].y&&ans[i-1].x>ans[i].x)printf("N");
                else printf("S");
            }
            printf("
    ");
    
    //下面这段代码用来测试地图录入情况        
    //        for(i=1; i<=6; i++)
    //        {
    //            for(j=0; j<4; j++)
    //                printf("%d ",mymap[6][i][j]);
    //
    //            printf("
    ");
    //        }
    
    //用来查看队列运行情况
    //for(i=0;i<10;i++)
    //    printf("%d %d %d
    ",que[i].x,que[i].y,que[i].pre);
        }
    
    
        return 0;
    }
    /*
    0 1 0 0 0
    0 1 0 1 0
    0 0 0 0 0
    0 1 1 1 0
    0 1 0 1 0
    */
    
    

  • 相关阅读:
    C#使用各种时间戳及转换
    Rust借用机制的理解分析
    C# QRCoder 生成二维码
    Android 使用 JCIFS 访问共享文件
    Linux samba 创建共享盘
    WPF 等待对话框
    IIS实现Nginx功能:转发
    js jquery ajax跨域
    依赖注入
    .net standard
  • 原文地址:https://www.cnblogs.com/hjch0708/p/7554842.html
Copyright © 2020-2023  润新知