• 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

    [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 }
    View Code
  • 相关阅读:
    解决mybatis查询返回结果值串查
    MSSQL Export Excel
    Linux检测硬盘读取速度
    Linux修改用户密码
    Linux系统关闭防火墙端口
    Linux系统查看系统信息
    查找一个String中存储的多个数据
    编辑器vi命令
    去除一段文字最后一个符号
    替换Jar包中的一个文件 Replace a file in a JAR
  • 原文地址:https://www.cnblogs.com/shengshouzhaixing/p/3847556.html
Copyright © 2020-2023  润新知