HDU 1484 Basic wall maze (dfs + 记忆化)

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找到最短路,以及每个点的前驱节点,最后打印路径。代码中的注释很详细。题目本身很简单,就是代码写起来有点麻烦。

#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 + 记忆化)

时间: 2024-10-06 17:31:07

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

【HDOJ】1484 Basic wall maze

BFS. 1 /* 1484 */ 2 #include <iostream> 3 #include <queue> 4 #include <string> 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 typedef struct { 11 int x, y; 12 string s; 13 }

hdu 1501 Zipper (dfs+记忆化搜索)

Zipper Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6491    Accepted Submission(s): 2341 Problem Description Given three strings, you are to determine whether the third string can be formed

HDU 5024 Wang Xifeng&#39;s Little Plot (枚举 + DFS记忆化搜索)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 513    Accepted Submission(s): 338 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

不要62 hdu 2089 dfs记忆化搜索

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中的 dfs 记忆化搜索方法解. 模板: int dfs(int i, int s, bool e) { if (i==-1) return s==target_s; if (!e && f[i][s] != -1) return f[i][s]; int res = 0; int u = e?

HDU 1978 How many ways(记忆化)

Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 1.机器人一开始在棋盘的起始点并有起始点所标有的能量. 2.机器人只能向右或者向下走,并且每走一步消耗一单位能量. 3.机器人不能在原地停留. 4.当机器人选择了一条可行路径后,当他走到这条路径的终点时,他将只有终点所标记的能量. 如上图,机器人一开始在(1,1)点,并拥有4单位能量,蓝色方块表示他所能到达的点,如果他在这次路径选择中选择的终点是(2,4)

HDU 1208 Pascal&#39;s Travels( 记忆化搜索)

题目大意:每一小格代表能向右或者向下走几步,问从左上走到右下总共有多少种走法. dp[i][j]存放该格子有多少总走法. #include <iostream> #include <cstring> using namespace std; int n; char a[40][40]; int s[40][40]; __int64 dp[40][40]; int X[]={1, 0}; int Y[]={0, 1}; __int64 dfs(int x, int y) { if(d

HDU 4960 Another OCD Patient(记忆化搜索)

HDU 4960 Another OCD Patient 题目链接 记忆化搜索,由于每个碎片值都是正数,所以每个前缀和后缀都是递增的,就可以利用twopointer去找到每个相等的位置,然后下一个区间相当于一个子问题,用记忆化搜索即可,复杂度接近O(n^2) 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int INF = 0x3f3f3f

ZOJ 3644 Kitty&#39;s Game dfs,记忆化搜索,map映射 难度:2

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的因数,所以不会形成环,只需从1直接走,走到n即可. 但是如果这样的话时空复杂度就都是nk,明显不满足题意,而这个时候我们可以想到,每个状态都必然是k的约数,(点数不是k的约数的节点不在路上,可以无视),而约数的个数也就k^0.5个,可以直接用map映射,这样时空复杂度都是n*k^0.5,可以解出答案

How many ways(dfs+记忆化搜索)

Problem Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 1.机器人一开始在棋盘的起始点并有起始点所标有的能量. 2.机器人只能向右或者向下走,并且每走一步消耗一单位能量. 3.机器人不能在原地停留. 4.当机器人选择了一条可行路径后,当他走到这条路径的终点时,他将只有终点所标记的能量. [center] [img]../../../data/images/C113-1003-1.gif[/img]