poj2312 - Battle City (BFS变形)

题目链接:点击打开链接

题目:

Battle City
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8639 Accepted: 2895

Description

Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. 

What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers, steel walls and brick walls only. Your task is to get a bonus as soon as possible suppose that no enemies will disturb you (See the following picture). 

Your tank can't move through rivers or walls, but it can destroy brick walls by shooting. A brick wall will be turned into empty spaces when you hit it, however, if your shot hit a steel wall, there will be no damage to the wall. In each of your turns, you can choose to move to a neighboring (4 directions, not 8) empty space, or shoot in one of the four directions without a move. The shot will go ahead in that direction, until it go out of the map or hit a wall. If the shot hits a brick wall, the wall will disappear (i.e., in this turn). Well, given the description of a map, the positions of your tank and the target, how many turns will you take at least to arrive there?

Input

The input consists of several test cases. The first line of each test case contains two integers M and N (2 <= M, N <= 300). Each of the following M lines contains N uppercase letters, each of which is one of 'Y' (you), 'T' (target), 'S' (steel wall), 'B' (brick wall), 'R' (river) and 'E' (empty space). Both 'Y' and 'T' appear only once. A test case of M = N = 0 indicates the end of input, and should not be processed.

Output

For each test case, please output the turns you take at least in a separate line. If you can't arrive at the target, output "-1" instead.

Sample Input

3 4
YBEB
EERE
SSTE
0 0

Sample Output

8

题意:

从Y到T,问最少步数。其中S和R不可走,B要花费两步。


思路:

这里有一个陷阱是,从一个非B的点走到紧邻它的B点花费2步。回到最朴素的bfs实现的思想上,bfs是逐层向下迭代的,所以保证第一次走到终点时的步数是最小的。其中最关键的一点是,从上一层到下一层的路径长度一定是全部相同的。在这题中,从某个点走到下个为B的点花费了两步,这和初始的定义相悖,这将导致走到终点的路径不是最小。很容易举出一个例子来证明这一点。


写法1:

这里我们可以用记忆化搜索,一个vis[i][j]数组表示走到(i,j)这个点时,花费的步数cnt,如果存在某一次到达这个点花费的步数更少我们就把vis[i][j]更新,否则就不这样走。

这样写,必须把所有的可行路径的搜索一遍,效率比较低。

PS:使用这种写法时,在剪枝那一步我写tc>vis[tx][ty]超时了,改成tc>=vis[tx][ty]就AC了,而且还是180ms左右。剪枝还是要能剪就剪!


写法2:

使用优先队列。这道题已经可以看成是求单源最短路了,优先队列可以保证像bfs一样逐层迭代,第一次到达终点时即答案。


写法一:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {1, -1, 0, 0};
int m, n;
int vis[MAXN][MAXN];
char tu[MAXN][MAXN];

struct Node
{
	int x, y, cnt;
	Node(int x, int y, int cnt):x(x), y(y), cnt(cnt){}
};

Node st(0, 0, 0), ed(0, 0, 0);

void bfs(){
    int ans = INF;
	memset(vis, 0x3f, sizeof(vis));
	queue<Node> q;
	q.push(st);
	vis[st.x][st.y] = 0;
	while(!q.empty()){
		Node tmp = q.front(); q.pop();
		if(tmp.x==ed.x && tmp.y==ed.y && ans>tmp.cnt){
			ans = tmp.cnt;
			continue;
		}
		for(int i=0; i<4; ++i){
			int tx = tmp.x + dx[i];
			int ty = tmp.y + dy[i];
			int tc = tmp.cnt + 1;
			if(tx<0||ty<0||tx>=m||ty>=n) continue;
			if(tu[tx][ty]=='S' || tu[tx][ty]=='R') continue;
			if(tu[tx][ty]=='B') ++tc;
			if(tc>=vis[tx][ty]) continue;
			vis[tx][ty] = tc;
			q.push(Node(tx, ty, tc));
		}
	}
	if(INF==ans)
        printf("%d\n", -1);
    else
        printf("%d\n", ans);
}

int main(){
	while(scanf("%d%d", &m, &n)!=EOF){
		if(m==0&&n==0) break;
		for(int i=0; i<m; ++i){
            getchar();
			for(int j=0; j<n; ++j){
                scanf("%c", &tu[i][j]);
                if(tu[i][j]=='Y'){
					st.x = i, st.y = j, st.cnt = 0;
				}
				if(tu[i][j]=='T'){
					ed.x = i, ed.y = j;
				}
			}
		}
		bfs();
	}
	return 0;
}

写法二:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>

using namespace std;

const int MAXN = 305;
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {1, -1, 0, 0};
int m, n;
bool vis[MAXN][MAXN];
char tu[MAXN][MAXN];

struct Node
{
	int x, y, cnt;
	bool operator<(const Node &t)const{
		return cnt>t.cnt;
	}
	Node(int x, int y, int cnt):x(x), y(y), cnt(cnt){}
};

Node st(0, 0, 0), ed(0, 0, 0);

int bfs(){
    memset(vis, 0, sizeof(vis));
	priority_queue<Node> q;
	q.push(st);
	vis[st.x][st.y] = true;
	while(!q.empty()){
		Node tmp = q.top(); q.pop();
		if(tmp.x==ed.x && tmp.y==ed.y){
			return tmp.cnt;
		}
		for(int i=0; i<4; ++i){
			int tx = tmp.x + dx[i];
			int ty = tmp.y + dy[i];
			int tc = tmp.cnt + 1;
			if(tx<0||ty<0||tx>=m||ty>=n || vis[tx][ty]) continue;
			if(tu[tx][ty]=='S' || tu[tx][ty]=='R') continue;
			if(tu[tx][ty]=='B') ++tc;
			q.push(Node(tx, ty, tc));
			vis[tx][ty] = true;
		}
	}
	return -1;
}

int main(){
	while(scanf("%d%d", &m, &n)!=EOF){
		if(m==0&&n==0) break;
		for(int i=0; i<m; ++i){
            getchar();
			for(int j=0; j<n; ++j){
                scanf("%c", &tu[i][j]);
                if(tu[i][j]=='Y'){
					st.x = i, st.y = j, st.cnt = 0;
				}
				if(tu[i][j]=='T'){
					ed.x = i, ed.y = j;
				}
			}
		}
		printf("%d\n", bfs());
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值