Basic wall maze

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 168    Accepted Submission(s): 52

Special Judge

Problem 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
 

题意:给定一张地图。而且给定起点和终点,求起点到终点的最短距离,地图上有墙,与以往的题目不同的是,以往的题目障碍物都是在格子上。可是本题的障碍物墙是在格子与格子的边界线上,所以在输入的时候就要进行预处理下。将墙的位置转化为相邻格子的东西南北方向墙的状态,所以使用了一个3为数组来记录地图的信息map[x][y][0]-map[x][y][3] 分别表示坐标为x,y的格子的四个方向墙的情况,0为没墙。1为有墙,然后一个dfs找到最短路,以及每一个点的前驱节点,最后打印路径。代码中的凝视非常具体。题目本身非常easy。就是代码写起来有点麻烦。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack> using namespace std; const int MAX = 9,limit = 6,INF = 1000;
const int dirx[4]={0,-1,0,1},diry[4]={1,0,-1,0}; //map[x][y][0]-map[x][y][3] 分别表示坐标为x,y的格子的四个方向墙的情况,0为没墙,1为有墙
int map[MAX][MAX][4];
//pre[x][y][0]用来记录x,y的前驱格子的x坐标,pre[x][y][1]用来记录x,y的前驱格子的y坐标
int dist[MAX][MAX],pre[MAX][MAX][2];
int sx,sy,ex,ey,pax,pay,pbx,pby;
stack<char> st; void init(){
int i,j; for(i=0;i<MAX;++i){
for(j=0;j<MAX;++j){
dist[i][j] = INF;
map[i][j][0] = map[i][j][1] = map[i][j][2] = map[i][j][3] = 0;
}
}
} void dfs(int x,int y,int cnt){ int i,tx,ty; for(i=0;i<4;++i){
if(map[x][y][i]==1)continue;
tx = x+dirx[i];
ty = y+diry[i];
if(tx<1 || ty<1 || tx>limit || ty>limit)continue;
if(cnt+1>dist[tx][ty])continue;
//更短就要更新,而且记录前驱
dist[tx][ty] = cnt;
pre[tx][ty][0] = x;
pre[tx][ty][1] = y;
dfs(tx,ty,cnt+1);
}
} void Path(){
int px,py,x,y; x = ex,y = ey;
px = pre[x][y][0];
py = pre[x][y][1]; while(1){
//推断方向
if(x==px){//x坐标同样看y坐标的情况
if(py<y)st.push('E');
else st.push('W');
}else{//y坐标同样看x坐标的情况
if(px<x)st.push('S');
else st.push('N');
}
if(px==sx && py==sy)break;
x = px;
y = py;
px = pre[x][y][0];
py = pre[x][y][1];
} while(!st.empty()){
printf("%c",st.top());
st.pop();
}
printf("\n");
} int main(){
//freopen("in.txt","r",stdin);
//(author : CSDN iaccepted)
int i,j;
while(scanf("%d %d",&sy,&sx)){
if(sx==0 && sy==0)break;
scanf("%d %d",&ey,&ex); init();
for(i=0;i<3;++i){
scanf("%d %d %d %d",&pay,&pax,&pby,&pbx);
if(pax==pbx){
for(j=pay+1;j<=pby;++j){
map[pax][j][3] = 1;
map[pax+1][j][1] = 1;
}
}else{
for(j=pax+1;j<=pbx;++j){
map[j][pby][0] = 1;
map[j][pby+1][2] = 1;
}
}
} dfs(sx,sy,0); Path();
} return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 1484 Basic wall maze (dfs + 记忆)的更多相关文章

  1. 【HDOJ】1484 Basic wall maze

    BFS. /* 1484 */ #include <iostream> #include <queue> #include <string> #include &l ...

  2. poj-2935 BFS Basic Wall Maze

    Basic Wall Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3384   Accepted: 1525   ...

  3. Basic Wall Maze

    poj2935:http://poj.org/problem?id=2935 题意:在6*6的格子中,有一些,如果两个格子之间有墙的话,就不能直接相通,问最少要经过几步才能从起点走到终点.并且输出路径 ...

  4. poj 2935 Basic Wall Maze

    是一个图论的基础搜索题- 没什么好说的就是搜索就好 主要是别把 代码写的太屎,错了不好找 #include<cstdio> #include<algorithm> #inclu ...

  5. 不要62 hdu 2089 dfs记忆化搜索

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中 ...

  6. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  7. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  8. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  9. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

随机推荐

  1. DOM对象本身也是一个js对象,所以严格来说,并不是操作这个对象慢,而是说操作了这个对象后,会触发一些浏览器行为(转)

    一直都听说DOM很慢,要尽量少的去操作DOM,于是就想进一步去探究下为什么大家都会这样说,在网上学习了一些资料,这边整理出来. 首先,DOM对象本身也是一个js对象,所以严格来说,并不是操作这个对象慢 ...

  2. struts详细解释拦截器

    1.拦截器:Struts2拦截器将一个Action要么Action的方法.之前或截取后场,和Struts2拦截器是可插拔,拦截器AOP一种实现. WebWork:拦截器是动态拦截Action调用的对象 ...

  3. MySQL之终端(Terminal)管理MySQL

    原文:MySQL之终端(Terminal)管理MySQL 前言:MySQL有很多的可视化管理工具,比如“mysql-workbench”和“sequel-pro-”. 现在我写MySQL的终端命令操作 ...

  4. Nginx+Php-fpm+MySQL+Redis源码编译安装指南

    说明:本教程由三部分组成如下: 1.      源码编译安装Nginx 2.      源码编译安装php以及mysql.redis扩展模块 3.      配置虚拟主机 文中所涉及安装包程序均提供下 ...

  5. OpenGL缓冲区

    OpenGL缓冲区 颜色缓冲区 OpenGL时,先是在一个缓冲区中完毕渲染,然后再把渲染结果交换到屏幕上. 我们把这两个缓冲区称为前颜色缓冲区(屏幕)和后颜色缓冲区.在默认情况下,OpenGL命令是在 ...

  6. IOS开发——Protocol使用协议

    protocol ['prəutəkɔl] (样例:http://blog.sina.com.cn/s/blog_6aafe9c90100yozz.html ) 一.说明  两个类进行通讯,用协议就比 ...

  7. Base64中文不能加密问题

    最近用到了Base64.js来对url参数进行加密,字母和数字都可以很好地加密/解密. 但测试中文时发现不能进行转换,貌似Base64.js不支持中文字符. 联想到encodeURI()对url的编码 ...

  8. JavaScript的类型、值和变量的总结

    前言:JavaScript的数据类型分为两类:原始类型和对象类型.5种原始类型:数字.字符串.布尔值.null(空).undefined(未定义).对象是属性的集合,每个属性都由“名/值对”(值可以是 ...

  9. 错误 4 自定义工具错误: 无法生成服务引用“DepartMentService”的代码。请检查其他错

    原文:错误 4 自定义工具错误: 无法生成服务引用"DepartMentService"的代码.请检查其他错 问题:     错误 4 自定义工具错误: 无法生成服务引用" ...

  10. activiti入门2流程引擎API和服务基础设施

    RepositoryService : 管理和控制公布包和流程定义(包括了一个流程每一个环节的结构和行为)的操作 除此之外,服务能够 查询引擎中的公布包和流程定义. 暂停或激活公布包.相应所有和特定流 ...