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

OJ-ID:
poj-2935

author:
Caution_X

date of submission:
20191004

tags:
bfs

description modelling:
走迷宫问题,问最小步数

major steps to solve it:
1.建图:因为本题的点和墙比较特殊,先把地图扩大到原来两倍再处理
2.常规bfs

warnings:
注意本题的建图

AC Code:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
bool map[][];
bool hasfind;
int lp,rp;
struct STRUCT{
int row,col,pre,dir;
}thend,que[];
int dr[] = {-,,,};
int dc[] = {,-,,};//相应的为 北西东南
char trans(int w)
{
if(w==) return 'N';
if(w==) return 'W';
if(w==) return 'E';
if(w==) return 'S';
}
bool in_range(int r,int c)
{
if(r>=&&r<=&&c>=&&c<=) return true;
return false;
}
bool crosswall(int r,int c,int w)
{
if(w==) return map[r+][c];
if(w==) return map[r][c+];
if(w==) return map[r][c-];
if(w==) return map[r-][c];
}
void init(int sr,int sc)
{
memset(map,false,sizeof(map));
//边界上围墙
for(int i = ; i <= ; i++)
map[][i] = true;
for(int i = ; i <= ; i++)
map[i][] = true;
for(int i = ; i <= ; i++)
map[i][] = true;
for(int i = ; i <= ; i++)
map[][i] = true; sr = sr * - ;
sc = sc * - ; cin>>thend.col>>thend.row;
thend.row = thend.row * -;
thend.col = thend.col * -; int tr,tc,wr,wc;
for(int i = ; i < ; i++) {
cin>>tc>>tr>>wc>>wr;
tr *= ;
tc *= ;
wr *= ;
wc *= ; if(tc==wc) { //水平方向 造墙
for(int st = tr; st <= wr; st++)
map[st][tc] = true;
} else {
for(int st = tc; st <= wc; st++)
map[tr][st] = true;
}
}//for int
que[].row = sr;
que[].col = sc;
que[].pre = ;//没有前驱
que[].dir = -; hasfind = false;
lp = ,rp = ;
}//init
void bfs()
{
int tr,tc;
lp=;rp=;
map[que[lp].row][que[lp].col]=true;
while(!hasfind)
{
for(int i=;i<;i++) {
tr=que[lp].row+dr[i];
tc=que[lp].col+dc[i];
if(!crosswall(tr,tc,i)&&in_range(tr,tc)&&!map[tr][tc]) {
map[tr][tc]=true;
que[rp].col=tc;
que[rp].row=tr;
que[rp].pre=lp;
que[rp].dir=i;
if(tr==thend.row&&tc==thend.col) {
hasfind=true;
break;
}
rp++;
}
}
lp++;
}
}
void print(int k)
{
if(k==) return;
else {
print(que[k].pre);
cout<<trans(que[k].dir);
}
}
int main()
{
//freopen("input.txt","r",stdin);
int sc,sr;
while(~scanf("%d%d",&sc,&sr)&&sc&&sr)
{
init(sr,sc);
bfs();
print(rp);
printf("\n"); }
}

poj-2935 BFS Basic Wall Maze的更多相关文章

  1. HDU 1484 Basic wall maze (dfs + 记忆)

    Basic wall maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. poj 2935 Basic Wall Maze

    是一个图论的基础搜索题- 没什么好说的就是搜索就好 主要是别把 代码写的太屎,错了不好找 #include<cstdio> #include<algorithm> #inclu ...

  3. poj 3026 bfs+prim Borg Maze

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9718   Accepted: 3263 Description The B ...

  4. Basic Wall Maze

    poj2935:http://poj.org/problem?id=2935 题意:在6*6的格子中,有一些,如果两个格子之间有墙的话,就不能直接相通,问最少要经过几步才能从起点走到终点.并且输出路径 ...

  5. 【HDOJ】1484 Basic wall maze

    BFS. /* 1484 */ #include <iostream> #include <queue> #include <string> #include &l ...

  6. poj 2395 bfs/记录路径

    http://poj.org/problem?id=2935 Basic Wall Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  7. Borg Maze - poj 3026(BFS + Kruskal 算法)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9821   Accepted: 3283 Description The B ...

  8. (BFS)poj2935-Basic Wall Maze

    题目地址 题目与最基本的BFS迷宫的区别就是有一些障碍,可以通过建立三维数组,标记某个地方有障碍不能走.另一个点是输出路径,对此建立结构体时要建立一个pre变量,指向前一个的下标.这样回溯(方法十分经 ...

  9. Borg Maze POJ - 3026 (BFS + 最小生成树)

    题意: 求把S和所有的A连贯起来所用的线的最短长度... 这道题..不看discuss我能wa一辈子... 输入有坑... 然后,,,也没什么了...还有注意 一次bfs是可以求当前点到所有点最短距离 ...

随机推荐

  1. C# 如何添加自定义键盘处理事件 如何配置app.config ? | csharp key press event tutorial and app.config

    本文首发于个人博客https://kezunlin.me/post/9f24ebb5/,欢迎阅读最新内容! csharp key press event tutorial and app.config ...

  2. 黄聪:PHP转换网址相对路径到绝对路径的一种方法

    相信很多程序(尤其是采集类的程序)都会有需要把网址的相对路径转换成绝对路径的需要,例如采集到某页面的HTML代码中包含资源文件经常会看到这样的文件名: <link rel="style ...

  3. RabbitMQ的安装与使用(Centos7,linux版本)

    1.主流的消息中间件简单介绍哦. 1).ActiveMQ是Apache出品,最流行的,能力强劲的开源消息总线,并且它一个完全支持jms(java message service)规范的消息中间件.其丰 ...

  4. Koa + GraphQL 示例

    初始化项目 创建 graphql-example 文件夹进入后初始化一个 package.json 文件. $ mkdir graphql-example && cd $_ $ yar ...

  5. go-面向对象编程(上)

    一个程序就是一个世界,有很多对象(变量) Golang 语言面向对象编程说明 1) Golang 也支持面向对象编程(OOP),但是和传统的面向对象编程有区别,并不是纯粹的面向对 象语言.所以我们说 ...

  6. 初识HTML_表单

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 利用InsertStatusValueRequest消息为新增的statuscode设定指定值(Value)

    我是微软Dynamcis 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...

  8. nRF24L01+如何检测信道被占用-RSSI寄存器实战分享

    检测信道占用的需求场景 在使用nRF24L01模块做一对多或多对一的组网通信中,大家都会担心一个问题就是在发送的时候,希望不要有其他的模块也进行发送,因为这样就会使无线信号发生碰撞,信道被堵塞,造成通 ...

  9. MySQL 部署分布式架构 MyCAT (五)

    分片(水平拆分) 4.全局表 业务使用场景: 如果你的业务中有些数据类似于数据字典,比如配置文件的配置, 常用业务的配置或者数据量不大很少变动的表,这些表往往不是特别大, 而且大部分的业务场景都会用到 ...

  10. mysql操作数据表

    目录 创建数据表 列约束 查看数据表结构 列类型(字段类型) 整型 浮点型 字符串 时间日期类型 Date Time Datetime Timestamp Year 枚举enum 修改表名 增加字段 ...