刷题集——POJ 2251 Dungeon Master(三维——求最少步数)

Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 76082 Accepted: 27287
Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

Is an escape possible? If yes, how long will it take?
Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a ‘#’ and empty cells are represented by a ‘.’. Your starting position is indicated by ‘S’ and the exit by the letter ‘E’. There’s a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input

3 4 5
S…
.###.
.##…
###.#

##.##
##…

#.###
####E

1 3 3
S##
#E#

0 0 0
Sample Output

Escaped in 11 minute(s).
Trapped!
Source

Ulm Local 1997


三维——求到达目的地最少步数
没一开始想到 bfs 不应该,不应该…

dfs版本(超时)
#include<iostream>
#include<memory.h>
using namespace std;
int ans, end_l, end_r, end_c;
int l, r, c;
bool dun[35][35][35];
int dp[35][35][35];

int L[8] = {0, 0, 0, 0, -1, 1};
int R[8] = {0, 0, -1, 1, 0, 0};
int C[8] = {-1, 1, 0, 0, 0, 0};

bool judge(int x, int y, int z){
	if(x<=0||x>l||y<=0||y>r||z<=0||z>c) return false;
	return dun[x][y][z];
}

void f(int x, int y, int z, int num){
	if(dp[x][y][z]<=num) return;
	dp[x][y][z] = num;
	if(x==end_l&&y==end_r&&z==end_c){
		ans = min(ans, num); 
		return;
	}
	
	for(int i=0; i<6; i++){
		int dl = x+L[i];
		int dr = y+R[i];
		int dc = z+C[i];
		if(judge(dl, dr, dc)){
			dun[dl][dr][dc] = false;
			f(dl, dr, dc, num+1);
			dun[dl][dr][dc] = true;
		}
	}
	
	return;
}

int main(){
	while(1){
		scanf("%d%d%d", &l, &r, &c);
		ans = 1e9; 
		memset(dun, false, sizeof(dun));
		if(l==0&&r==0&&c==0) break;
			
		int start_l, start_r, start_c;
		for(int i=1; i<=l; i++)
		for(int j=1; j<=r; j++)
		for(int k=1; k<=c; k++){
			char ch;
			dp[i][j][k] = 1e9;
			cin>>ch;
			if(ch == 'S'){
				start_l = i;
				start_r = j;
				start_c = k;
			}
			else if(ch == 'E'){
				end_l = i;
				end_r = j;
				end_c = k;
				dun[i][j][k] = true;
			}
			else if(ch == '.')
				dun[i][j][k] = true;
		}
		
		f(start_l, start_r, start_c, 0);
		
		if(ans!=1e9)
			cout<<"Escaped in "<<ans<<" minute(s).\n";
		else
			cout<<"Trapped!\n";
	}
		
	return 0;
}

bfs 版本(AC)
#include<iostream>
#include<memory.h>
#include<queue>
using namespace std;
int ans, end_l, end_r, end_c;
int l, r, c;
bool dun[35][35][35];
int L[8] = {0, 0, 0, 0, -1, 1};
int R[8] = {0, 0, -1, 1, 0, 0};
int C[8] = {-1, 1, 0, 0, 0, 0};

struct node{
	int x, y, z;
	int time;
}Node, top;

bool judge(int x, int y, int z){
	if(x<=0||x>l||y<=0||y>r||z<=0||z>c) 
		return false;
	return dun[x][y][z];
}

void bfs(int x, int y, int z){
	Node.time = 0;
	Node.x = x;
	Node.y = y;
	Node.z = z;
	queue<node> q;
	q.push(Node);
	while(!q.empty()){
		top = q.front();
		q.pop();
		if(top.x==end_l&&top.y==end_r&&top.z==end_c){
			ans = min(ans, top.time);
			continue;
		}
		for(int i=0; i<6; i++){
			int dl = top.x+L[i];
			int dr = top.y+R[i];
			int dc = top.z+C[i];
			if(judge(dl, dr, dc)){
				dun[dl][dr][dc] = false;
				Node.x = dl;
				Node.y = dr;
				Node.z = dc;
				Node.time = top.time+1;
				q.push(Node);
			}
		}
	}
	return;
}

int main(){
	while(cin>>l>>r>>c){
		ans = 1e9; 
		memset(dun, false, sizeof(dun));
		if(l==0&&r==0&&c==0) break;
			
		int start_l, start_r, start_c;
		for(int i=1; i<=l; i++)
		for(int j=1; j<=r; j++)
		for(int k=1; k<=c; k++){
			char ch;
			cin>>ch;
			if(ch == 'S'){
				start_l = i;
				start_r = j;
				start_c = k;
			}
			else if(ch == 'E'){
				end_l = i;
				end_r = j;
				end_c = k;
				dun[i][j][k] = true;
			}
			else if(ch == '.')
				dun[i][j][k] = true;
		}
		
		bfs(start_l, start_r, start_c);
		
		if(ans!=1e9)
			cout<<"Escaped in "<<ans<<" minute(s).\n";
		else
			cout<<"Trapped!\n";
	}
		
	return 0;
}
总结

dfs ----- 注重过程
bfs ----- 注重结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值