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 ...
随机推荐
- Nuxt使用Vuex
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 基础知识这里不再重述,学习的话请自行到官网 ...
- postman 编码加密汇总
1.MD5加密 /*加密方式:将 请求头的user-agent内容+请求方式+当前时间+(Base64)请求body中的stacode参数 拼接后得到的字符串进行MD5加密*/ //1.获取reque ...
- MDF损坏或LDF文件损坏
MDF损坏或LDF损坏 MDF丢失或LDF丢失 注意,这些情况必须要相同版本的sql server才能操作成功 当MDF损坏时 1.备份结尾日志 http://www.cnblogs.com/gere ...
- hexo+yilia页脚添加总访问量
个人博客:https://www.yuehan.online 现在博客:http://www.wangyurui.top 脚本方法使用不蒜子计数 安装脚本 要使用不蒜子(官网:http://busua ...
- Android之四大组件、六大布局、五大存储
[-] Android六大界面布局方式 1 LinearLayout线性布局 LinearLayout的常用XML属性及相关方法 LinearLayout子元素支持的常用XML属性及方法 2 Tabl ...
- Nginx常用命令(加入系统服务)
nginx 服务器重启命令,关闭 nginx -s reload :修改配置后重新加载生效 nginx -s reopen :重新打开日志文件 nginx -t -c /path/to/nginx.c ...
- Safari通过JavaScript获取系统语言
IE6 IE7 IE8 Firefox Chrome Safari Opera navigator.language undefined zh-CN zh-CN navigator.userLan ...
- 关于dispatch_semaphore的使用
dispatch_semaphore是GCD用来同步的一种方式,与他相关的共有三个函数,分别是 dispatch_semaphore_create,dispatch_semaphore_signal, ...
- 【HackerRank】Coin on the Table
题目链接:Coin on the Table 一开始想用DFS做的,做了好久都超时. 看了题解才明白要用动态规划. 设置一个三维数组dp,其中dp[i][j][k]表示在时间k到达(i,j)所需要做的 ...
- 安装Discuz开源论坛
11.添加mysql普通用户 接着上篇的lamp这篇安装Discuz 配置虚拟主机 1.打开虚拟主机配置 [root@NFS-31 ~]# vim /usr/local/apache2/conf/ht ...