poj-2935 BFS 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:
- a 6 by 6 grid of unit squares
- 3 walls of length between 1 and 6 which are placed either horizontally or vertically to separate squares
- 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
poj-2935
author:
Caution_X
date of submission:
20191004
tags:
bfs
description modelling:
走迷宫问题,问最小步数
major steps to solve it:
1.建图:因为本题的点和墙比较特殊,先把地图扩大到原来两倍再处理
2.常规bfs
warnings:
注意本题的建图
#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"); }
}
最新文章
- PHP代码重用与函数编写
- centos 6 安装 gitlib
- Oracle 用户管理与权限控制
- 微信公众平台教程和SDK收集
- iOS开发——高级篇——地图 MapKit
- C# 计时器
- 【20160924】GOCVHelper 图像处理部分(2)
- JS中检测数据类型的几种方式及优缺点
- wxPython + Boa 练习程序
- Windows2003/2008/2008 R2下易语言点支持库配置就退出的问题
- centOS上安装redis
- 【4】项目结构+基本的Tornado服务
- 如何添加地图控件到Windows Phone 8的页面中
- NSThread创建线程的三种方法
- java 中 一个int类型的num,num&;1
- intellij idea 2017和Jprofiler 10的集成 报错问题
- C# 8中的范围类型(Range Type)
- 基于CC2530/CC2430 的光强采集系统--ADC实验
- 数据库连接池 maxActive,maxIdle,maxWait参数
- Mongodb实战使用指南
热门文章
- ASP.NET ---- Repeater 遍历出省市
- 查询安装webpack4.0是否成功时提示无法找到的解决方法
- tensorflow slim代码使用
- jQuery 源码分析(十五) 数据操作模块 val详解
- ros相关笔记
- Awesome Java: Github上关于Java相关的工具
- python爬虫:将数据保存到本地
- Flask 教程 第三章:Web表单
- linux shell通过curl获取HTTP请求的状态码
- FCC---Animate Elements Continually Using an Infinite Animation Count---设置animation-iteration-count的次数为无限,让小球一直跳动