poj 2935 Basic Wall Maze

来源:互联网 发布:淘宝卖家订单管理系统 编辑:程序博客网 时间:2024/06/02 02:31

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, please output the turns you take at least in a separate line. If you can’t arrive at the target, output “-1” instead.

Sample Input

3 4
YBEB
EERE
SSTE
0 0

Sample Output

8

Source

POJ Monthly, 鲁小石

/*Problem: 2935  User: saruka Memory: 140K  Time: 0MS Language: C++  Result: Accepted */#include <cstdio>#include <queue>#include <cstring>#include <iostream>using namespace std;const int d[][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};const char dir[4] = {'N', 'W', 'S', 'E'};int sx, sy, dx, dy;int x1, y1, x2, y2;struct node{    int x, y;    int step, pre;    char d;};node cur, nxt;const int maxn = 40;node q[maxn];bool g[maxn][maxn], ee[maxn][maxn], ss[maxn][maxn];void readin(){    scanf("%d%d", &dx, &dy);    memset(g, false, sizeof(g));    memset(ee, false, sizeof(ee));    memset(ss, false, sizeof(ss));    g[sx][sy] = true;    for(int i = 0; i < 3; i++)    {        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);        if(x1 == x2)        {                   if(x1 > 0)            {                   for(int y = min(y1, y2) + 1; y <= max(y1, y2); y++)                    ee[x1][y] = true;            }        }        else        {                       if(y1 > 0)            {                   for(int x = min(x1, x2) + 1; x <= max(x1, x2); x++)                    ss[x][y1] = true;            }        }    }}void out(int k){    if(q[k].pre != -1)    {        out(q[k].pre);        printf("%c", q[k].d);    }}void bfs(){    cur.x = sx;    cur.y = sy;    cur.step = 0;    cur.pre = -1;    int head = 0, tail = 1;    q[head] = cur;    while(head < tail)    {        cur = q[head];        if((cur.x == dx) && (cur.y == dy))        {            out(head);            printf("\n");            return;        }        for(int i = 0; i < 4; i++)        {            nxt.x = cur.x + d[i][0];            nxt.y = cur.y + d[i][1];            if(nxt.x > 0 && nxt.x <= 6 && nxt.y > 0 && nxt.y <= 6 && !g[nxt.x][nxt.y])            {                switch(i)                {                    case 0: //'N'                        if(!ss[nxt.x][nxt.y])                        {                            nxt.step = cur.step + 1;                            nxt.pre = head;                            nxt.d = dir[i];                            q[tail] = nxt;                            tail++;                            g[nxt.x][nxt.y] = true;                        }                        break;                    case 1: //'W'                        if(!ee[nxt.x][nxt.y])                        {                            nxt.step = cur.step + 1;                            nxt.pre = head;                            nxt.d = dir[i];                            q[tail] = nxt;                            tail++;                            g[nxt.x][nxt.y] = true;                        }                        break;                    case 2: //'S'                        if(!ss[cur.x][cur.y])                        {                            nxt.step = cur.step + 1;                            nxt.pre = head;                            nxt.d = dir[i];                            q[tail] = nxt;                            tail++;                            g[nxt.x][nxt.y] = true;                        }                        break;                    case 3: //'E'                        if(!ee[cur.x][cur.y])                        {                            nxt.step = cur.step + 1;                            nxt.pre = head;                            nxt.d = dir[i];                            q[tail] = nxt;                            tail++;                            g[nxt.x][nxt.y] = true;                        }                        break;                }            }        }        head++;    }}int main(){    while(scanf("%d%d", &sx, &sy))    {        if(sx + sy == 0) break;        readin();        bfs();    }    return 0;}

Powered By Saruka.
Copyright © 2016 All Rights Reserved.

0 0