poj 2395 bfs/记录路径
http://poj.org/problem?id=2935
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 3220 | Accepted: 1457 | 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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
bool can[][][][];
bool vis[][];
int path[][];
int fx[][]={,,-,,,,,-};
char idx[]={'N','S','W','E'};
struct node{int x,y,bs;}P[];
void print(int x,int y,int bs)
{
if(bs==) return;
for(int i=;i<;++i)
{
int dx=x+fx[i][];
int dy=y+fx[i][];
if(dx<||dy<||dx>||dy>||path[dx][dy]+!=bs||can[dx][dy][x][y]) continue;
else{
print(dx,dy,bs-);
printf("%c",idx[i]);
return;
}
}
}
void bfs(node s,node e)
{
memset(vis,,sizeof(vis));
memset(path,inf,sizeof(path));
path[s.x][s.y]=;
queue<node>q;
s.bs=;
q.push(s);
while(!q.empty()){
node t=q.front();q.pop();
if(t.x==e.x&&t.y==e.y) {print(t.x,t.y,t.bs);return;}
if(vis[t.x][t.y]) continue;
vis[t.x][t.y]=;
for(int i=;i<;++i)
{
node tt=t;
int dx=tt.x+fx[i][];
int dy=tt.y+fx[i][];
if(dx<||dy<||dx>||dy>||vis[dx][dy]||can[tt.x][tt.y][dx][dy]||path[dx][dy]<=tt.bs+) continue;
path[dx][dy]=tt.bs+;
q.push(node{dx,dy,tt.bs+});
}
}
}
int main()
{
while(scanf("%d%d",&P[].y,&P[].x)!=EOF){
if(P[].x==&&P[].y==) break;
scanf("%d%d",&P[].y,&P[].x);
memset(can,,sizeof(can));
for(int i=;i<;++i)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&y1,&x1,&y2,&x2);
if(x1==x2){
int miny=min(y1,y2)+,maxy=max(y1,y2);
for(int j=miny;j<=maxy;++j)
can[x1][j][x1+][j]=can[x1+][j][x1][j]=;
}
else{
int minx=min(x1,x2)+,maxx=max(x1,x2);
for(int j=minx;j<=maxx;++j)
{
can[j][y1][j][y1+]=can[j][y1+][j][y1]=;
}
}
}
bfs(P[],P[]);
puts("");
}
return ;
}
poj 2395 bfs/记录路径的更多相关文章
- - 迷宫问题 POJ - 3984 bfs记录路径并输出最短路径
定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...
- POJ.3894 迷宫问题 (BFS+记录路径)
POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...
- Codeforces-A. Shortest path of the king(简单bfs记录路径)
A. Shortest path of the king time limit per test 1 second memory limit per test 64 megabytes input s ...
- HDU1026--Ignatius and the Princess I(BFS记录路径)
Problem Description The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- (简单) POJ 3414 Pots,BFS+记录路径。
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- hdu 1026 Ignatius and the Princess I (bfs+记录路径)(priority_queue)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1026 Problem Description The Princess has been abducted ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- POJ 3414 Pots 记录路径的广搜
Description You are given two pots, having the volume of A and B liters respectively. The following ...
随机推荐
- app开发多少钱一个
经常听网友问app开发要多少钱,这个问题太宽泛了,需要根据具体的需求才好定价,也就是要先做好需求分析(前面我们写了一个app开发需求文档模板),不同的功能不同的价位,就像我们买电脑,cpu多少钱.主板 ...
- F110 参数保存和重新运行录屏
**初始界面回车 PERFORM frm_dynpro USING ' 'X'. PERFORM frm_dynpro USING '' 'BDC_CURSOR' 'F110V-LAUFD'. PER ...
- 20170520 BADI增强学习
一.要求:Tcode:FF_5 导入数据运行时,产生财务凭证之前修改某些字段值.Exmp:FEBRE-VWEZWBKPF-XBLNRFEBEP-CHECTBSEG-ZUONR there is a b ...
- Java多线程(Java总结篇)
Java总结篇:Java多线程 多线程作为Java中很重要的一个知识点,在此还是有必要总结一下的. 一.线程的生命周期及五种基本状态 关于Java中线程的生命周期,首先看一下下面这张较为经典的图: 上 ...
- Protobuf支持 pointf
Protobuf支持 pointf序列化 加入:ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(System.Drawing.PointF), fa ...
- Mac 环境 下使用Charles 抓包Http/Https请求
实现目标 在Mac 上 对 iOS 真机 和 模拟器 进行 Http/Https抓包 使用工具 Mac 上 Charles 4.2 安装 参考链接 1. 和 链接 2. 抓包 http 请求 (1 ...
- 每天一个Linux命令(43)at命令
at命令用于在指定时间执行命令.at允许使用一套相当复杂的指定时间的方法.可以用相对时间法指定,也可以用绝对时间法指定. (1)用法: 用法: at [选项参数] [时间 ...
- Linux基本命令 压缩命令
1.压缩命令 ================================================================================== 命令名称:gzip ...
- eclipse新建自定义EL函数
==================================================================================================== ...
- Windows下MarialDB使用
命令行控制启动和关闭:mysqld --console #这样启动ctrl+c即为关闭 启动:双击mysqld.exe即可 #此为后台启动 关闭:mysqladmin -uroot -pr ...