poj 2312 Battle City bfs +优先队列

Battle City
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6496 Accepted: 2176

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

Source

POJ Monthly,鲁小石


本来还以为是一个简单的bfs  但是做此题是要用到优先队列进行优化
题意:
坦克 遇到河(R),墙(S)不能走,其他的可以走,但是遇到b 要消耗2的时间,遇到e需要消耗1的时间
开始没多想,直接bfs 一直wa 。。。。。。。。。。。。。
遇到b时 要消耗2的时间 是因为在遇到b时 要停留一秒,在行进一秒
  由于bfs是逐层遍历的 所以虽然得到了答案 但是由于花费的时间不同,所以应进行处理(其实还是认为优不优先都一样 哭
 两种方法,一个是采用优先队列的方法  利用优先队列让队列中到起点的时间值最小的点先出队。(要思考。。。。。。。)
code:
#include<iostream>
#include<string.h>
#include<queue>
#include<algorithm>
#include<stdio.h>
using namespace std;
char map[1005][1005];
int sx,sy,ex,ey;
int vis[1005][1005];
int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int n,m;
struct Node
{
    int x,y,cost;
};

bool operator < (const Node &a, const Node &b) 
{
	return a.cost > b.cost;
}

int bfs(int sx,int sy)
{
    Node node,now,next;
   // cout<<n<<m<<endl;
    int x,y;
    	priority_queue<Node> que;
    while(!que.empty())
    {
    	que.pop();
    }
    node.x=sx;
    node.y=sy; 
    node.cost=0;
    vis[node.x][node.y]=1;    
    que.push(node);
    while (!que.empty())
    {    	
        now=que.top();
       // cout<<map[now.x][now.y]<<endl;       
        que.pop();
       // cout<<now.x<<" "<<ex<<endl;
      //  cout<<now.y<<" "<<ey<<endl;
        if (now.x==ex&&now.y==ey)
        {
       
            cout<<now.cost<<endl;
            return 1;
        }
        for (int i=0;i<4;i++)
        {
        //	cout<<endl<<endl;
            next.x=dir[i][0]+now.x;
			next.y=dir[i][1]+now.y;
           // cout<<next.x<<"  next.x "<<endl; 
           // cout<<next.y<<"  next.y "<<endl;
           
            if (next.x<n&&next.y<m&&next.x>=0&&next.y>=0&&(map[next.x][next.y]=='E'||map[next.x][next.y]=='B'||map[next.x][next.y]=='Y'||map[next.x][next.y]=='T')&&!vis[next.x][next.y])
            {
            	
            //	cout<<1<<endl;
			
                vis[next.x][next.y]=1;              
                if (map[next.x][next.y]=='B')
                    next.cost=now.cost+2;
                else
                    next.cost=now.cost+1;
                 
                que.push(next);
            }
            
        }
    }
    return 0;
}
int main()
{
    while (cin>>n>>m)
    {   
    
    	if(n==0&&m==0) break;
        memset(vis,0,sizeof(vis));
        memset(map,0,sizeof(map));
       // ex=ey=sx=sy=-100;
        for (int i=0;i<n;i++)
         {
         	   for (int j=0;j<m;j++)
            {
                cin>>map[i][j];
                if (map[i][j]=='Y')
                {
                    sx=i;
                    sy=j;
                }
                if (map[i][j]=='T')
                {
                    ex=i;
                    ey=j;
                }
                
            }
            
         }
        
        if (!bfs(sx,sy))
            cout<<-1<<endl;
    }
    return 0;
}
肾疼肾疼。。。。。。。。。。。
另外附上优先队列的声明方法
第一种:

struct node
{
int x,y;
int step;
};
priority_queue<node>q; //优先队列中元素的比较规则默认是按元素的值从大到小排序;
bool operator<(const node &a,const node &b) //括号里面是const 而且还必须是引用
{
return a.step>b.step; //从小到大排序。重载小于号。因为默认是从大到小
}


第二种:
struct node
{
int x,y;
int time; //定义一个优先队列
friend bool operator<(node a, node b)
{ //从小到大排序采用“>”号;如果要从大到小排序,则采用“<”号
return a.time> b.time; //从小到大排序
}
}; 
priority_queue<node>q; //优先队列中元素的比较规则默认是按元素的值从大到小排序;

切记:从小到大排序采用“>”号;如果要从大到小排序,则采用“<”号;

优先队列不只是用来提高效率滴~























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值