poj2935 Basic Wall Maze

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

poj2935 Basic Wall Maze_第1张图片

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

Ulm Local 2006



BFS,但是有个蛮讨厌的不能走的处理,其他还算简单。

#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
using namespace std;

#define MAXN 6

struct Point
{
    int x,y;
};

queue <Point> q;
int vis[MAXN+5][MAXN+5];
Point block[MAXN+5][MAXN+5];
vector<char> ans;

int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};

char Count(int t)
{
    if (t==0) return 'S';
    if (t==1) return 'E';
    if (t==2) return 'N';
    if (t==3) return 'W';
}

bool Comp(Point p,Point q)
{
    if (p.x==q.x && p.y==q.y) return true;
    return false;
}

bool Cross(Point s1,Point t1,Point s2,Point t2)
{
    int i;
    if (s2.y==t2.y)
    {
        for (i=min(s2.x,t2.x)+1;i<=max(s2.x,t2.x);i++)
        {
            if (Comp(s1,(Point){i,s2.y}) && Comp(t1,(Point){i,s2.y+1}) || Comp(t1,(Point){i,s2.y}) && Comp(s1,(Point){i,s2.y+1})) return true;
        }
    }
    if (s2.x==t2.x)
    {
        for (i=min(s2.y,t2.y)+1;i<=max(s2.y,t2.y);i++)
        {
            if (Comp(s1,(Point){s2.x,i}) && Comp(t1,(Point){s2.x+1,i}) || Comp(t1,(Point){s2.x,i}) && Comp(s1,(Point){s2.x+1,i})) return true;
        }
    }
    return false;
}

int main()
{
    int i,j,n;
    Point begin,end;
    while(1)
    {
        scanf("%d%d",&begin.x,&begin.y);
        if (begin.x==0 && begin.y==0) break;
        scanf("%d%d",&end.x,&end.y);
        for (i=0;i<3;i++)
        {
            for (j=0;j<2;j++)
            {
                scanf("%d%d",&block[i][j].x,&block[i][j].y);
            }
        }
        memset(vis,-1,sizeof(vis));
        while(!q.empty()) q.pop();
        q.push(begin);
        while(!q.empty())
        {
            Point tmp=q.front();
            if (Comp(tmp,end)) break;
            q.pop();
          //  printf("%d %d\n",tmp.x,tmp.y);
            for (i=0;i<4;i++)
            {
                Point tmp1=(Point){tmp.x+dx[i],tmp.y+dy[i]};
                if (tmp1.x<1 || tmp1.y<1 || tmp1.x>MAXN || tmp1.y>MAXN) continue;
            //    printf("%d %d~\n",tmp1.x,tmp1.y);
                for (j=0;j<3;j++)
                {
              //      printf("%d!~\n",j);
                    if (Cross(tmp,tmp1,block[j][0],block[j][1])) break;
                }
                if (j<3) continue;
                if (vis[tmp1.x][tmp1.y]!=-1) continue;
         //       printf("%d %d--->%d %d\n",tmp.x,tmp.y,tmp1.x,tmp1.y);
                vis[tmp1.x][tmp1.y]=i;
                q.push(tmp1);
            }
        }
        Point tmp=q.front();
        while(!Comp(tmp,begin))
        {
            int last=vis[tmp.x][tmp.y];
          //  printf("%d %d %d\n",tmp.x,tmp.y,last);
            ans.push_back(Count(last));
            tmp.x-=dx[last];
            tmp.y-=dy[last];
        }
        for (i=ans.size()-1;i>=0;i--)
        {
            printf("%c",ans[i]);
        }
        printf("\n");
        ans.clear();
    }
    return 0;
}



你可能感兴趣的:(poj2935 Basic Wall Maze)