POJ 2935 Basic Wall Maze

http://poj.org/problem?id=2935


Basic Wall Maze

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2794   Accepted: 1271   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

Ulm Local 2006

[Submit]   [Go Back]   [Status]   [Discuss]

解题思路:简单bfs,关键在于墙体的处理,采用三维数组,map[8][8][4] 前二维代表坐标,最后一维代表四个方向,那个方位有墙体设为1,无墙体设为0

解题代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <queue>
 4 #include <string>
 5 #include <iostream>
 6 using namespace std;
 7
 8 int map[8][8][4]; //表示墙体            //  1
 9 bool vis[8][8]; //标记是否来过
10 const int turn_x[4] = {0, 1, 0, -1};    //  2
11 const int turn_y[4] = {-1, 0, 1, 0};    //  2
12 const char to[4] = {‘N‘, ‘E‘, ‘S‘, ‘W‘};//  3
13                        /* 1, 2, 3同步最后一个坐标 */
14 struct Node{
15     int x, y;
16     string pass;
17 };
18 int sta_x, sta_y, end_x, end_y;
19 queue <Node> Q;
20 Node p1, p2;
21
22 void BFS(){
23     int xx, yy;
24     while(!Q.empty())
25         Q.pop();
26     memset(vis, 0, sizeof(vis));
27     vis[sta_x][sta_y] = 1;
28     p1.x = sta_x;
29     p1.y = sta_y;
30     p1.pass = "";
31     Q.push(p1);
32     while(!Q.empty()){
33         p1 = Q.front();
34         Q.pop();
35         for (int i = 0; i < 4; i ++){  //此处同步坐标
36             xx = p1.x + turn_x[i];
37             yy = p1.y + turn_y[i];
38             if(xx <= 0 || xx > 6 || yy <= 0 || yy > 6)
39                 continue;
40             if(map[p1.x][p1.y][i] || vis[xx][yy])
41                 continue;
42             vis[xx][yy] = 1;
43             p2.x = xx;
44             p2.y = yy;
45             p2.pass = p1.pass + to[i];
46             if(p2.x == end_x && p2.y == end_y)
47                 return;
48             Q.push(p2);
49         }
50     }
51 }
52
53 int main(){
54     int xx1, yy1, xx2, yy2, tm;
55     while(~scanf("%d%d", &sta_x, &sta_y) && sta_x && sta_y){
56         scanf("%d%d", &end_x, &end_y);
57         memset(map, 0, sizeof(map));
58
59         for (int i = 0; i < 3; i ++){
60             scanf("%d%d%d%d", &xx1, &yy1, &xx2, &yy2);
61             if(xx1 == xx2){
62                 if(yy1 > yy2){
63                     tm = yy1;
64                     yy1 = yy2;
65                     yy2 = tm;
66                 }
67                 for (int j = yy1 +1; j <= yy2; j ++){
68                     map[xx1 +1][j][3] = 1;
69                     map[xx1][j][1] = 1;
70                 }
71             }
72             else{
73                 if(xx1 > xx2){
74                     tm = xx1;
75                     xx1 = xx2;
76                     xx2 = tm;
77                 }
78                 for (int j = xx1 +1; j <= xx2; j ++){
79                     map[j][yy1][2] = 1;
80                     map[j][yy1 +1][0] = 1;
81                 }
82             }
83         }
84         BFS();
85         cout << p2.pass << endl;
86     }
87     return 0;
88 }

POJ 2935 Basic Wall Maze

时间: 2024-07-29 15:50:37

POJ 2935 Basic Wall Maze的相关文章

HDU 1484 Basic wall maze (dfs + 记忆化)

Basic wall maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 168    Accepted Submission(s): 52 Special Judge Problem Description In this problem you have to solve a very simple maze consisti

22 poj2935 Basic Wall Maze --- bfs

对于每个点有四个方向可以走,把墙两边的点对应墙的方向能不能走给预处理一下,用vis[x][y][0..3]表示点(x,y)的该方向可不可以走. 然后bfs就好了,记录路径就把每个结点的前一个点记录下来,递归输出就可以了. #include<cstdio> #include<queue> #include<cstring> using namespace std; int dx[]={0,0,1,-1}; int dy[]={1,-1,0,0}; bool vis[7][

【HDOJ】1484 Basic wall maze

BFS. 1 /* 1484 */ 2 #include <iostream> 3 #include <queue> 4 #include <string> 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 typedef struct { 11 int x, y; 12 string s; 13 }

POJ2935 Basic Wall Maze bfs记录路径

链接:   POJ2935 题意: 6 X 6的地图   格子和格子可能有墙      整个地图中有三道墙      求起点起点到终点的路径 本题中的墙可以理解为某a位置的X方向不能走   即用一个三维数组map[x][y][z]表示(x,y)的Z方向不能走 关于记录路径可以用一个pre数组记录每个坐标的前一个坐标的复合值  最后倒序输出方向即可 代码 #include<iostream> #include<cstdio> #include<cstring> #incl

【POJ 3026】Borg Maze

[POJ 3026]Borg Maze 一个考察队搜索alien 这个考察队可以无限分割 问搜索到所有alien所需要的总步数 即求一个无向图 包含所有的点并且总权值最小(最小生成树 BFS+最小生成树 Prim/Kruskal-懒死了 就这么贴吧--凑活看( ̄┰ ̄*) #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <queue&

【POJ 1113】 Wall (凸包)

[POJ 1113] Wall 给n个点 连出一个凸包 然后在凸包外筑墙 要求墙与凸包每一处的距离都>=l 问需要建的最短的墙长 乍一看挺难 画画图就能看出来 凸包外建距离l的墙 其实就是在凸包每个顶点处 以顶点为圆心 做半径为l的弧 做到两侧半径与点的两边平行即可 然后把这些弧都用直线衔接 就是最短墙长 这样还不好求 呢把弧拿出来呢 其实就相当于把整个凸包作为一个点 以该点为圆心 l为半径做了个圆 这样弧的总长就是2*PI*l 那剩下的就是直线 平移下来其实就是凸包的周长 然后卷包裹法或者扫描

【POJ】1113 Wall(凸包)

http://poj.org/problem?id=1113 答案是凸包周长+半径为l的圆的周长... 证明?这是个坑.. #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include <algorithm> #include <queue> #include <set>

POJ 3026:Borg Maze(BFS建图+prim+MST)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8250   Accepted: 2762 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to descr

POJ 2935 BFS

给出6*6的矩阵,起点,终点,一共三堵墙,墙不会相交. 求起点到终点的最少步,保证有解 对每次移动判断相对应的是否有墙即可 #include "stdio.h" #include "string.h" #include "queue" using namespace std; const int dir[4][2]={ {1,0},{-1,0},{0,1},{0,-1} }; int wall[10][10][10][10]; int s_x,s