POJ 2312(优先队列+bfs)

       

                         Battle City

            

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
 1 /*由于碰到brick walls步数会增2,所以要用优先队列,优先选取步数最小的进行扩展*/
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 using namespace std;
 6
 7 char map[305][305];
 8 bool visit[305][305];
 9 int sx,sy,ex,ey,m,n;
10 int d[4][2]={1,0,0,1,-1,0,0,-1};
11
12 struct point
13 {
14     int x;
15     int y;
16     int step;
17     bool operator <(const point &temp)const
18     {
19         return step>temp.step;
20     }
21 };
22
23 int BFS()
24 {
25     point init;
26     init.x=sx;
27     init.y=sy;
28     init.step=0;
29     priority_queue<point>q;
30     q.push(init);
31     visit[init.x][init.y]=true;
32     point node,newnode;
33     while(!q.empty())
34     {
35         node=q.top();
36         q.pop();
37         if(node.x==ex&&node.y==ey)
38         return node.step;
39         for(int i=0;i<4;i++)
40         {
41             newnode.x=node.x+d[i][0];
42             newnode.y=node.y+d[i][1];
43             if(newnode.x>=0&&newnode.x<m&&newnode.y>=0&&newnode.y<n&&!visit[newnode.x][newnode.y])
44             if(map[newnode.x][newnode.y]!=‘S‘&&map[newnode.x][newnode.y]!=‘R‘)
45             {
46                 if(map[newnode.x][newnode.y]==‘B‘)
47                 newnode.step=node.step+2;
48                 else
49                 newnode.step=node.step+1;
50                 q.push(newnode);
51                 visit[newnode.x][newnode.y]=true;//这个一开始写外面了一直TLE
52             }
53         }
54     }
55     return -1;
56 }
57
58 int main()
59 {
60     //freopen("in.txt","r",stdin);
61     int i,j;
62     while(scanf("%d%d",&m,&n),m||n)
63     {
64         memset(visit,false,sizeof(visit));
65         getchar();
66         for(i=0;i<m;i++)
67         {
68             for(j=0;j<n;j++)
69             {
70                 scanf("%c",&map[i][j]);
71                 if(map[i][j]==‘Y‘)
72                 sx=i,sy=j;
73                 else if(map[i][j]==‘T‘)
74                 ex=i,ey=j;
75                 else;
76             }
77             getchar();
78         }
79         int ans=BFS();
80         printf("%d\n",ans);
81     }
82     return 0;
83 }
时间: 2024-07-28 16:13:16

POJ 2312(优先队列+bfs)的相关文章

poj 1724 ROADS (bfs+优先队列)

题目链接 题意:在有费用k限制的条件下,求从1到n的最短距离,如果最短距离相同求费用最小的,边为有向边,其中可能有 多个相同的源点和目标点,但是距离和费用不同. 分析:用bfs和邻接表来把每一个边搜一下,因为用了优先队列,所以先到n的一定是最小的 . 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio&

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

POJ 3287 (基础BFS) Catch That Cow

这是做的第一道BFS,很基础很简单的题目 广度优先搜索算法如下:(用QUEUE)(1) 把初始节点S0放入Open表中:(2) 如果Open表为空,则问题无解,失败退出:(3) 把Open表的第一个节点取出放入Closed表,并记该节点为n:(4) 考察节点n是否为目标节点.若是,则得到问题的解,成功退出:(5) 若节点n不可扩展,则转第(2)步:(6) 扩展节点n,将其不在Closed表和Open表中的子节点(判重)放入Open表的尾部,并为每一个子节点设置指向父节点的指针(或记录节点的层次)

poj 1724ROADS(bfs和dfs做法)

1 /* 2 dfs比较好想,就是测试数据的问题,导致在遍历边的时候要倒着遍历才过! 3 */ 4 #include<iostream> 5 #include<cstdio> 6 #include<cstring> 7 #include<vector> 8 #include<algorithm> 9 #define Max 0x3f3f3f3f 10 using namespace std; 11 12 struct node{ 13 int D

Ignatius and the Princess I (hdu 1026 优先队列+bfs+输出路径)

Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14624    Accepted Submission(s): 4634 Special Judge Problem Description The Princess has been abducted by the BEelzeb

poj 1915 双向 BFS 利用数组 a[x][y] = a[cp.x][cp.y] + 1; b[x][y] = b[cp.x][cp.y] + 1;保留步数

#include<iostream>#include<queue> using namespace std; struct point{    int x, y;};point bufa[8] ={    {-2, 1}, {-1, 2}, {1, 2}, {2, 1},    {2, -1}, {1, -2}, {-1, -2}, {-2, -1}}; int n, a[305][305], b[305][305]; int rule(int x,int y)//判断是否符合棋盘

poj 1562 简单 bfs

// 简单 bfs #include <iostream>#include<fstream>using namespace std; char map[110][110];int flag[110][110];int qu[11000][2],qe,qs,m,n;int add[8][2]={-1,-1,  -1,0, -1,1,   0,-1,  0,1,  1,-1,    1,0,   1,1 }; void bfs(int r,int c){    int i,tr,tc;

poj 2970 优先队列

先按di排序,(从小到大).然后依次完成合同,若发现第i个合同无法在截止日期前完成,便从之前已经完成的任务中选一个aj最大的合同,付钱来使得这个合同尽快完成. #include<cstring> #include<cstdio> #include<iostream> #include<queue> #include<algorithm> using namespace std; struct node { int q; int w; bool o

POJ 2312 Battle City(优先队列+BFS)

题目链接:http://poj.org/problem?id=2312 Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7085   Accepted: 2390 Description Many of us had played the game "Battle city" in our childhood, and some people (like me) even often pl