hdoj-1072 --Nightmar||我仿照hdoj-1044的差不多的思路写的(DFS+BFS, 主要是看到了路可以来回走, 可能是数据比较弱, 0ms过了,看到网上好多都是BFS就直接过了)

Nightmare

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9250    Accepted Submission(s): 4461


Problem Description
Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The labyrinth has an exit, Ignatius should get out of the labyrinth before the bomb explodes. The initial exploding time of the bomb is set to 6 minutes. To prevent the bomb from exploding by shake, Ignatius had to move slowly, that is to move from one area to the nearest area(that is, if Ignatius stands on (x,y) now, he could only on (x+1,y), (x-1,y), (x,y+1), or (x,y-1) in the next minute) takes him 1 minute. Some area in the labyrinth contains a Bomb-Reset-Equipment. They could reset the exploding time to 6 minutes.

Given the layout of the labyrinth and Ignatius' start position, please tell Ignatius whether he could get out of the labyrinth, if he could, output the minimum time that he has to use to find the exit of the labyrinth, else output -1.

Here are some rules:
1. We can assume the labyrinth is a 2 array.
2. Each minute, Ignatius could only get to one of the nearest area, and he should not walk out of the border, of course he could not walk on a wall, too.
3. If Ignatius get to the exit when the exploding time turns to 0, he can't get out of the labyrinth.
4. If Ignatius get to the area which contains Bomb-Rest-Equipment when the exploding time turns to 0, he can't use the equipment to reset the bomb.
5. A Bomb-Reset-Equipment can be used as many times as you wish, if it is needed, Ignatius can get to any areas in the labyrinth as many times as you wish.
6. The time to reset the exploding time can be ignore, in other words, if Ignatius get to an area which contain Bomb-Rest-Equipment, and the exploding time is larger than 0, the exploding time would be reset to 6.
 

Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case starts with two integers N and M(1<=N,Mm=8) which indicate the size of the labyrinth. Then N lines follow, each line contains M integers. The array indicates the layout of the labyrinth.
There are five integers which indicate the different type of area in the labyrinth:
0: The area is a wall, Ignatius should not walk on it.
1: The area contains nothing, Ignatius can walk on it.
2: Ignatius' start position, Ignatius starts his escape from this position.
3: The exit of the labyrinth, Ignatius' target position.
4: The area contains a Bomb-Reset-Equipment, Ignatius can delay the exploding time by walking to these areas.
 

Output
For each test case, if Ignatius can get out of the labyrinth, you should output the minimum time he needs, else you should just output -1.
 

Sample Input
  
  
3 3 3 2 1 1 1 1 0 1 1 3 4 8 2 1 1 0 1 1 1 0 1 0 4 1 1 0 4 1 1 0 0 0 0 0 0 1 1 1 1 4 1 1 1 3 5 8 1 2 1 1 1 1 1 4 1 0 0 0 1 0 0 1 1 4 1 0 1 1 0 1 1 0 0 0 0 3 0 1 1 1 4 1 1 1 1 1
 

Sample Output
  
  
4 -1

13

我的思路:

仿照hdoj1044的DFS+BFS思路(很是相似, 只是时间重置不太一样)

因为路可以来回走, 所以将“4”作为特殊点对待, 将每一个特殊点(起点、终点、和每一个“4”)都作为起点遍历一次图,并且将他们编号, 通过多次遍历就可以得到这些点之间的距离(遍历过程中如果发现两个特殊点之间的距离大于或者等于6就跳过【这里就是这题的不同点】,距离也就默认为初始化时的INF),然后用DFS来判断从起点出发, 走哪几个“4”特殊点(或者不走“4”直接从起点到达终点)到达终点所用的时间最短, 或者无论如何也走不到终点!!

AC代码:(估计数据很弱,运气好,0ms过了, 最坏情况要遍历将近64张图, 然后还要用DFS来搜索64个点的走法(这个耗时很凶, 这个时候就炸了, 这种方法对这题显然是个野路子, 只能小数据的时候使用))。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;

struct node{
	int x, y;
	int t;
}s, u, v;

const int INF = 1e8;
const int maxn = 9; 
int G[maxn][maxn]; 
int path[maxn*maxn][maxn*maxn];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int N, M, L, var, ans;
bool vis[maxn][maxn], vis2[maxn*maxn];

void bfs(int x, int y, int from) {
	memset(vis, false, sizeof(vis)); 
	s.x = x; s.y = y; s.t = 0; 
	vis[s.x][s.y] = true; 
	queue<node> que; 
	que.push(s);  //这个居然也会漏掉。。 
	while(!que.empty()) {
		u = que.front(); que.pop();
		//cout << u.x << " " << u.y << endl;
		for(int i = 0; i < 4; i++) {
			v.x = u.x + dir[i][0];
			v.y = u.y + dir[i][1];
			if(v.x < 0 || v.y < 0 || v.x >= N || v.y >= M || G[v.x][v.y] == 0 || vis[v.x][v.y]) continue; 
			vis[v.x][v.y] = true;//访问标记  常常忘记   这里漏掉会导致无限死循环 
			v.t = u.t+1;
			if(G[v.x][v.y] != 1 && v.t < 6){
				//if(v.x == 3 && v.y == 5) cout << "终点" << u.x  <<" "<< u.y << "|" << v.x <<" "<< v.y << " " << v.t << endl;
				if(G[v.x][v.y] == 2) path[from][0] = v.t;
				else if(G[v.x][v.y] == 3) path[from][L+1] = v.t;
				else path[from][G[v.x][v.y] - 53] = v.t;
				//v.t = 0;  删掉了这一行   然后对了   显然我对这个过程分析的还不是很透彻 
			}
			que.push(v); 
		}
	}
}

void dfs(int pos, int steps) {
	if(steps >= ans) return ;
	if(pos == L+1) {
		ans = min(ans, steps);
//		cout << "结束: "; 
//		for(int i = 1; i <= L+1; i++) {
//			if(vis2[i] == true) cout << "走 " << i ; 
//		}
//		cout << "  总路程是: " << ans << endl;
		return ;
	}
	for(int i = 1; i <= L+1; i++) {
		if(vis2[i]) continue; 
		vis2[i] = true;
		//cout << "走" << i  << "路程" << path[pos][i] << "当前总路:" << steps + path[pos][i] << endl; 
		dfs(i, steps + path[pos][i]); 
		vis2[i] = false;
	}
}

int main() {
	int T; scanf("%d", &T);
	while(T--) {
		L = 0; var = 49; ans = INF;
		scanf("%d%d", &N, &M);
		for(int i = 0; i < N; i++)
			for(int j = 0; j < M; j++) {
				scanf("%d", &G[i][j]);
				if(G[i][j] == 4) {
					G[i][j] += (++var);
					L++;
				//	cout << G[i][j] << endl;
				}
			}
		for(int i = 0; i <= L+1; i++) {
			for(int j = 0; j <= L+1; j++) {
				path[i][j] = INF;
			}
		}
//		for(int i = 0; i < N; i++) {
//			for(int j = 0; j < M ; j++)
//				if(G[i][j] == 2) cout << "S "; 
//				else if(G[i][j] == 3) cout << "E ";
//				else if(G[i][j] == 0) cout << "| "; 
//				else if(G[i][j] > 50 ) cout << G[i][j] - 53<< " ";
//				else cout << "  ";
//			cout << endl;
//		}
		for(int i = 0; i < N; i++)
			for(int j = 0; j < M; j++) {
				if(G[i][j] > 50) bfs(i, j, G[i][j]-53);
				else if(G[i][j] == 3) bfs(i, j, L+1);
				else if(G[i][j] == 2) bfs(i, j, 0);
			}
//		cout << path[1][4] << endl;
//		cout << path[2][4] << endl;
//		cout << path[3][4] << endl;
		memset(vis2, false, sizeof(vis2));
		dfs(0, 0);
	//	cout << ans << endl;
		if(ans == INF) printf("-1\n");
		else printf("%d\n", ans); 

	}
	return 0; 
}


现在来看看第二种做法:单纯的BFS解可往返点(这是我第一次只用一个BFS解答可以来回走的路的题目,参考了一些大神博客的题解)

这里要注意的是, 所有的“4”都只可以走一次, 重复走得话就没有意义, 可以想象一个背着炸弹的“炸弹人”刚跑到“4”重置了炸弹时间, 然后他出去走了两步, 然后又走回来两步重置炸弹的话就是在死循环, 其他的点(2【起点】, 3【终点】, 1【通道】)都可以来回走, 原因是起点和通道以及终点都是一样的, 其实都可以看成通道, 都可以来回走, 唯一不同的就是到了目标点(终点)这个点的时候, 统计一下最少步数即可。

AC代码:(比较上面的AC代码, 这个非常可靠(还是单纯的BFS!!))

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
using namespace std;

struct node {
	int x, y, t, steps; 
}s, u, v;

const int INF = 1e8;
const int maxn = 9;
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
bool vis[maxn][maxn];
int G[maxn][maxn]; 
int N, M;
int ans;

void bfs() {
	memset(vis, false, sizeof(vis)); 
	//vis[s.x][s.y] = true; 
	queue<node> que;
	que.push(s);
	while(!que.empty()) {
		u = que.front(); que.pop();
		for(int i = 0; i < 4; i++) {
			v.x = u.x + dir[i][0];
			v.y = u.y + dir[i][1];
			v.t = u.t-1;
			v.steps = u.steps+1;
			if(v.x < 0 || v.y < 0 || v.x >= N || v.y >= M || G[v.x][v.y] == 0 || vis[v.x][v.y] || v.t <= 0 || v.steps >= ans) continue;
			if(G[v.x][v.y] == 4) {
				v.t = 6;
				vis[v.x][v.y] = true;
			}
			else if(G[v.x][v.y] == 3) {
				ans = min(ans, v.steps); 
			}
			que.push(v);
		}
	}
}
int main() {
	int T; scanf("%d", &T);
	while(T--) {
		scanf("%d%d", &N, &M); 
		for(int i = 0; i < N; i++)
			for(int j = 0; j < M; j++) {
				scanf("%d", &G[i][j]);
				if(G[i][j] == 2) {
					s.x = i; s.y = j;
					s.t = 6; s.steps = 0;
				}
			}
		ans = INF;
		bfs();
		if(ans == INF) printf("-1\n"); 
		else printf("%d\n", ans);
	} 
	return 0; 	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值