广搜与优先队列-----POJ2312

来源:互联网 发布:mac自带抠图 编辑:程序博客网 时间:2024/06/05 16:24

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 4YBEBEERESSTE0 0

Sample Output

8



题意很简单,就是坦克(Y)去吃目标(T),河(R)和铁墙(S)不能走,空地(E)可以走,砖墙(B)需要花费一秒打碎它然后可以走,如果能到达目标

输出最短时间,否则输出-1



#include<cstdio>#include<queue>#include<algorithm>using namespace std;int m, n, sx1, sy1, ex, ey, ju[302][302];char tan[302][302];int xiu[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};struct dui{//定义动态坐标(x, y)和到达该坐标的时间int x, y, n;};void bfs(int sx, int sy){queue<dui> lie;dui fi, en;fi.x = sx;fi.y = sy;fi.n = 0;ju[sx][sy] = 0;//以'Y'点为初始坐标,到达改点时间初始化为0lie.push(fi);//把'Y'点情况加入队列while(!lie.empty()){//队列不空开始循环fi = lie.front();//取队首元素lie.pop();//将队首元素‘剔出’队伍for(int i = 0; i < 4; i++){//开始上下左右广搜en.x = fi.x + xiu[i][0];en.y = fi.y + xiu[i][1];if(en.x >= 0 && en.x < m && en.y >= 0 && en.y < n && tan[en.x][en.y] != 'S' && tan[en.x][en.y] != 'R'){//如果可以移动if(tan[en.x][en.y] == 'B'){en.n = fi.n + 2;}else{en.n = fi.n + 1;}if(ju[en.x][en.y] > en.n){//取到达该点时间最短的一条路线ju[en.x][en.y] = en.n;lie.push(en); }}}}}int main(){while(scanf("%d%d", &m, &n) == 2){if(m == 0 && n == 0){break;}getchar();for(int i = 0; i < 301; i++){//初始化,居然不能用memset,╮(╯▽╰)╭for(int k = 0; k < 301; k++){ju[i][k] = 100000;}}for(int i = 0; i < m; i++){scanf("%s", tan[i]);}for(int i = 0; i < m; i++){for(int k = 0; k < n; k++){if(tan[i][k] == 'Y'){sx1 = i;sy1 = k;}if(tan[i][k] == 'T'){ex = i;ey = k;}}}bfs(sx1, sy1);printf(ju[ex][ey] == 100000 ? "-1\n" : "%d\n", ju[ex][ey]);}return 0;}

0 0