POJ 2935 Basic Wall Maze

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   strong   

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 62 60 0 1 01 5 1 61 5 3 50 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};    //  212 const char to[4] = {‘N‘, ‘E‘, ‘S‘, ‘W‘};//  313                        /* 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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.