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. 开发常用Git/Linux/idea命令快捷键总结(持续更新)

    在开发过程中,会使用越来越多的命令,或快捷键,来帮助我们提高工作效率.本文记录了我在平时积累的常用命令,分享给大家. git命令 基本命令 set LESSCHARSET=utf-8 --idea T ...

  2. 为何我建议1-3年的Java程序员仔细看看这篇文章

    此文的目的是为了督促自己去不断学习,让自己有更明确的方向去提升自己.以技能树为基础,以面试要点为大纲,我觉得比抓住什么看什么要更有目的,更能坚持下去.世界瞬息万变,我们要时刻准备着.时刻提高着自己,才 ...

  3. C#上手练习7(构造方法语句)

    创建类的对象是使用“类名 对象名 = new 类名()”的方式来实现的. 实际上,“类名()”的形式调用的是类的构造方法,也就是说构造方法的名字是与类的名称相同的. 构造方法的定义语法形式如下. 访问 ...

  4. IPy过滤

    #coding=utf-8 from IPy import IP write=open('result.txt','a') allgame=open('allgame.txt') gameline=a ...

  5. H5常用新特性

    html5新特性 [注意]这些新特性都有兼容性的问题,基本是IE9+以上版本的浏览器才支持,如果不考兼容性问题,可以大量使用这些新特性 html5新增的语义话标签 <header> :头部 ...

  6. [转]C#操作Outlook

    本文转自:https://blog.csdn.net/yanlovehan/article/details/8500449 //引用Microsoft.Office.Interop.Outlook.d ...

  7. Flutter学习笔记(1)--环境安装

    flutter最近显得格外的火,公司的同事也一直在谈论flutter,感觉自己不学学就要失业了...所以决定顺应潮流学习以下flutter,做一下学习笔记,希望可以给需要的同学带来一些帮助~ 正文为f ...

  8. 什么是技术规划(TPP)?

    什么是技术? 1.技,巧也. ——<说文> 2.为了人类的目的而操纵自然世界的工具.机器.系统和技巧的集合. ——梅里特·罗·史密斯 3.人类都在利用自然和改造自然的过程中积累起来并在生产 ...

  9. matplotlib画3D图修改X,Y,Z,colorbar的刻度值

    修改X,Y,Z轴的刻度值 from matplotlib.ticker import MultipleLocator,FuncFormatter from mpl_toolkits.mplot3d i ...

  10. HTML 中img标签不显示

    异常 图片请求响应吗: 403, url响应如下: Request Method: GET Status Code: 403 Forbidden Remote Address: ***.***.**. ...