优先级队列--坦克大战

坦克大战

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 3
描述
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?
输入
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.
输出
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.
样例输入
3 4
YBEB
EERE
SSTE
0 0
样例输出
8

因为遇到 B 时步数加2,E, T时加步数加1,广搜遍历所有可能的情况时,有可能步数多的反而先到达T,故要用到优先级队列,
从起点出发到目前为止步数小的先出队。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>//优先级队列priority_queue包含在queue.h中

using namespace std;

typedef struct{
	int r;
	int c;
	int step;
}node;
bool operator < (const node &a, const node &b){//自定义优先级,步数小的先出队
	return a.step > b.step;
}

int des_x, des_y;
int ans, n, m;
int visit[310][310];
char map[310][310];
int dir[4][4] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};

void bfs(int r, int c);
int main(){
	int start_x, start_y;
	int i, j;

	while(scanf("%d %d", &m, &n), m || n){
		for(i = 0; i < m; i++){
			scanf("%s", map[i]);
			for(j = 0; j < n; j++){
				if(map[i][j] == 'Y'){
					start_x = i;
					start_y = j;
				}
				if(map[i][j] == 'T'){
					des_x = i;
					des_y = j;
				}
			}
		}
		memset(visit, 0, sizeof(visit));
		ans = -1;
		bfs(start_x, start_y);
		printf("%d\n", ans);
	}
	return 0;
}

void bfs(int r, int c){
	int row, col;
	int i;
	node p, q;
	priority_queue <node> Q;//定义一个元素类型为node的优先级队列

	p.r = r;
	p.c = c;
	p.step = 0;
	visit[r][c] = 1;
	Q.push(p);

	while(!Q.empty()){
		q = Q.top();//与普通队列不同,Q.top(),队首元素出队
		Q.pop();

		if(map[q.r][q.c] == 'T'){
			ans = q.step;
			return ;
		}

		for(i = 0; i < 4; i++){
			row = q.r + dir[i][0];
			col = q.c + dir[i][1];
			if(row >= 0 && row < m && col >= 0 && col < n && !visit[row][col] && map[row][col] != 'S' && map[row][col] != 'R'){
				visit[row][col] = 1;
				if(map[row][col] == 'B'){
					p.step = q.step + 2;
				}
				else
					p.step = q.step + 1;
				p.r = row;
				p.c = col;
				Q.push(p);
			}
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值