POJ2935 Basic Wall Maze(BFS)

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

BFS求最优路径,下标是反着来的,还有就是那个墙讨论下横竖问题就好了
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define C 0.57721566490153286060651209
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1


using namespace std;

typedef long long LL;
const int INF=0x3f3f3f3f;
const double eps=1e-10;
const double PI=acos(-1.0);

const int maxn=1000009;

int dy[]={0,0,-1,1};
int dx[]={-1,1,0,0};
char ch[5]={'N','S','W','E'};

struct node
{
    int x,y;
    int pre;
    int id;
    char c;
};

bool vis[10][10];
node rc[3][3];
node p[50];
int ex,ey;

bool OK(node &a,node &b,int d)
{
    for(int i=0;i<3;i++)
    {
        if(d<2)
        {
            if(rc[i][0].x==rc[i][1].x&&b.y>rc[i][0].y&&b.y<=rc[i][1].y)
            {
                if(d==0&&b.x==rc[i][0].x)
                    return false;
                if(d==1&&a.x==rc[i][0].x)
                    return false;
            }
        }
        else
        {
            if(rc[i][0].y==rc[i][1].y&&b.x>rc[i][0].x&&b.x<=rc[i][1].x)
            {
                if(d==2&&b.y==rc[i][0].y)
                    return false;
                if(d==3&&a.y==rc[i][0].y)
                    return false;
            }
        }
    }
    return true;
}

void output(int id)
{
    if(p[id].pre==-1) return;
    output(p[id].pre);
    printf("%c",p[id].c);
}

void bfs(int x,int y)
{
    memset(vis,0,sizeof vis);
    vis[x][y]=1;
    int cnt=1;
    node a,b;
    queue<node>Q;
    p[0].pre=-1;
    p[0].x=x;
    p[0].y=y;
    p[0].id=0;
    Q.push(p[0]);
    while(!Q.empty())
    {
        a=Q.front();
        Q.pop();
        for(int i=0;i<4;i++)
        {
            b.x=a.x+dx[i];
            b.y=a.y+dy[i];
            if(b.x<1||b.x>6||b.y<1||b.y>6||vis[b.x][b.y])
                continue;
            if(OK(a,b,i))
            {
                vis[b.x][b.y]=1;
                b.c=ch[i];
                b.pre=a.id;
                b.id=cnt;
                p[cnt]=b;
                cnt++;
                Q.push(b);
                if(b.x==ex&&b.y==ey)
                {
                    output(b.id);
                    puts("");
                    return;
                }
            }
        }
    }
}

int main()
{
    int x,y;
    while(~scanf("%d%d",&y,&x),x||y)
    {
        scanf("%d%d",&ey,&ex);
        for(int i=0;i<3;i++)
        {
            scanf("%d%d%d%d",&rc[i][0].y,&rc[i][0].x,&rc[i][1].y,&rc[i][1].x);
        }
        bfs(x,y);
    }
    return 0;
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值