Basic Wall Maze
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3384 Accepted: 1525 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

Source

OJ-ID:
poj-2935

author:
Caution_X

date of submission:
20191004

tags:
bfs

description modelling:
走迷宫问题,问最小步数

major steps to solve it:
1.建图:因为本题的点和墙比较特殊,先把地图扩大到原来两倍再处理
2.常规bfs

warnings:
注意本题的建图

AC Code:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
bool map[15][15];
bool hasfind;
int lp,rp;
struct STRUCT{
    int row,col,pre,dir;
}thend,que[50];
int dr[4] = {-2,0,0,2};
int dc[4] = {0,-2,2,0};//相应的为 北西东南
char trans(int w)
{
    if(w==0) return 'N';
    if(w==1) return 'W';
    if(w==2) return 'E';
    if(w==3) return 'S';
}
bool in_range(int r,int c)
{
    if(r>=0&&r<=12&&c>=0&&c<=12)    return true;
    return false;
}
bool crosswall(int r,int c,int w)
{
    if(w==0) return map[r+1][c];
    if(w==1) return map[r][c+1];
    if(w==2) return map[r][c-1];
    if(w==3) return map[r-1][c];
}
void init(int sr,int sc)
{
    memset(map,false,sizeof(map));
    //边界上围墙
    for(int i = 0; i <= 12; i++)
        map[0][i] = true;
    for(int i = 0; i <= 12; i++)
        map[i][0] = true;
    for(int i = 0; i <= 12; i++)
        map[i][12] = true;
    for(int i = 0; i <= 12; i++)
        map[12][i] = true;

    sr = sr * 2 - 1;
    sc = sc * 2 - 1;

    cin>>thend.col>>thend.row;
    thend.row = thend.row * 2 -1;
    thend.col = thend.col * 2 -1;

    int tr,tc,wr,wc;
    for(int i = 0; i < 3; i++) {
        cin>>tc>>tr>>wc>>wr;
        tr *= 2;
        tc *= 2;
        wr *= 2;
        wc *= 2;

        if(tc==wc) { //水平方向 造墙
            for(int st = tr; st <= wr; st++)
                map[st][tc] = true;
        } else {
            for(int st = tc; st <= wc; st++)
                map[tr][st] = true;
        }
    }//for int
    que[0].row = sr;
    que[0].col = sc;
    que[0].pre  = 0;//没有前驱
    que[0].dir = -1;

    hasfind = false;
    lp = 0,rp = 1;
}//init
void bfs()
{
    int tr,tc;
    lp=0;rp=1;
    map[que[lp].row][que[lp].col]=true;
    while(!hasfind)
    {
        for(int i=0;i<4;i++) {
            tr=que[lp].row+dr[i];
            tc=que[lp].col+dc[i];
            if(!crosswall(tr,tc,i)&&in_range(tr,tc)&&!map[tr][tc]) {
                map[tr][tc]=true;
                que[rp].col=tc;
                que[rp].row=tr;
                que[rp].pre=lp;
                que[rp].dir=i;
                if(tr==thend.row&&tc==thend.col) {
                    hasfind=true;
                    break;
                }
                rp++;
            }
        }
        lp++;
    }
}
void print(int k)
{
    if(k==0) return;
    else {
        print(que[k].pre);
        cout<<trans(que[k].dir);
    }
}
int main()
{
    //freopen("input.txt","r",stdin);
    int sc,sr;
    while(~scanf("%d%d",&sc,&sr)&&sc&&sr)
    {
        init(sr,sc);
        bfs();
        print(rp);
        printf("\n");

    }
}
01-18 16:48
查看更多