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"); }
}

最新文章

  1. PHP代码重用与函数编写
  2. centos 6 安装 gitlib
  3. Oracle 用户管理与权限控制
  4. 微信公众平台教程和SDK收集
  5. iOS开发——高级篇——地图 MapKit
  6. C# 计时器
  7. 【20160924】GOCVHelper 图像处理部分(2)
  8. JS中检测数据类型的几种方式及优缺点
  9. wxPython + Boa 练习程序
  10. Windows2003/2008/2008 R2下易语言点支持库配置就退出的问题
  11. centOS上安装redis
  12. 【4】项目结构+基本的Tornado服务
  13. 如何添加地图控件到Windows Phone 8的页面中
  14. NSThread创建线程的三种方法
  15. java 中 一个int类型的num,num&amp;1
  16. intellij idea 2017和Jprofiler 10的集成 报错问题
  17. C# 8中的范围类型(Range Type)
  18. 基于CC2530/CC2430 的光强采集系统--ADC实验
  19. 数据库连接池 maxActive,maxIdle,maxWait参数
  20. Mongodb实战使用指南

热门文章

  1. ASP.NET ---- Repeater 遍历出省市
  2. 查询安装webpack4.0是否成功时提示无法找到的解决方法
  3. tensorflow slim代码使用
  4. jQuery 源码分析(十五) 数据操作模块 val详解
  5. ros相关笔记
  6. Awesome Java: Github上关于Java相关的工具
  7. python爬虫:将数据保存到本地
  8. Flask 教程 第三章:Web表单
  9. linux shell通过curl获取HTTP请求的状态码
  10. FCC---Animate Elements Continually Using an Infinite Animation Count---设置animation-iteration-count的次数为无限,让小球一直跳动