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"); }
}
poj-2935 BFS Basic Wall Maze的更多相关文章
- HDU 1484 Basic wall maze (dfs + 记忆)
Basic wall maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- poj 2935 Basic Wall Maze
是一个图论的基础搜索题- 没什么好说的就是搜索就好 主要是别把 代码写的太屎,错了不好找 #include<cstdio> #include<algorithm> #inclu ...
- poj 3026 bfs+prim Borg Maze
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9718 Accepted: 3263 Description The B ...
- Basic Wall Maze
poj2935:http://poj.org/problem?id=2935 题意:在6*6的格子中,有一些,如果两个格子之间有墙的话,就不能直接相通,问最少要经过几步才能从起点走到终点.并且输出路径 ...
- 【HDOJ】1484 Basic wall maze
BFS. /* 1484 */ #include <iostream> #include <queue> #include <string> #include &l ...
- poj 2395 bfs/记录路径
http://poj.org/problem?id=2935 Basic Wall Maze Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- Borg Maze - poj 3026(BFS + Kruskal 算法)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9821 Accepted: 3283 Description The B ...
- (BFS)poj2935-Basic Wall Maze
题目地址 题目与最基本的BFS迷宫的区别就是有一些障碍,可以通过建立三维数组,标记某个地方有障碍不能走.另一个点是输出路径,对此建立结构体时要建立一个pre变量,指向前一个的下标.这样回溯(方法十分经 ...
- Borg Maze POJ - 3026 (BFS + 最小生成树)
题意: 求把S和所有的A连贯起来所用的线的最短长度... 这道题..不看discuss我能wa一辈子... 输入有坑... 然后,,,也没什么了...还有注意 一次bfs是可以求当前点到所有点最短距离 ...
随机推荐
- Algorithm: Prime & Euler Function & Productive Function
素数筛 朴素算法 一般来说,可以用试除法判断某一个数是不是素数: bool isPrime(int n) { if(n < 2) return false; for(int i = 2; i & ...
- flex——justify-content属性引起的一个样式问题
前言 在flex布局出现以前,我一般习惯使用浮动布局(float)来实现下列布局 现在尽量少用浮动布局,虽然好用,但有时会带来一些意想不到的问题,甚至导致布局错位, 一开始浮动布局只是为了 ...
- 数据库——SQL-SERVER练习(1)连接与子查询
一.实验准备 1.复制实验要求文件及“CREATE-TABLES.SQL”文件, 粘贴到本地机桌面. 2.启动SQL-SERVER服务. 3. 运行查询分析器, 点击菜单<文件>/< ...
- pandas 学习 第7篇:DataFrame - 数据处理(应用、操作索引、重命名、合并)
DataFrame的这些操作和Series很相似,这里简单介绍一下. 一,应用和应用映射 apply()函数对每个轴应用一个函数,applymap()函数对每个元素应用一个函数: DataFrame. ...
- Java生鲜电商平台-生鲜供应链(采购管理)
Java生鲜电商平台-生鲜供应链(采购管理) 在生鲜供应链系统中采购中心这一模块,它是电商公司管理采购的模块,包含供应商管理,采购订单管理,采购商品管理,在该模块中采购订单是采购中心的核心模块.在其他 ...
- CSS @规则
最近在看极客时间winter大神的重学前端系列,遇到了许多不常用但是很重要的知识点.感觉视野得到了极大的开阔(打个广告,哈哈). 其中css @规则令人印象深刻.简单的做下笔记: @namespace ...
- sftp-server 搭建编译
下载开源代码 https://github.com/zwx230741/openssh-portable 编译 # autoconf # ./configure --prefix=xxx # make ...
- [b0007] windows 下 eclipse 开发 hdfs程序样例
目的: 学习使用hdfs 的java命令操作 相关: 进化: [b0010] windows 下 eclipse 开发 hdfs程序样例 (二) [b0011] windows 下 eclipse 开 ...
- js 常用工具方法
1.格式化字符串 String.prototype.format = function () { let args = arguments; return this.replace(/\{(\d+)\ ...
- express 将 Router 实例模块化
为了更好的组织代码,将 Router 实例进行模块化,将 get / post 等快捷方式放在Router上,而不是 App 上,然后将该 Router 作为中间件,use 到 server.js 上 ...