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
#include
#include
#include
#include
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< 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<
肾疼肾疼。。。。。。。。。。。
另外附上优先队列的声明方法
第一种:
struct node
{
int x,y;
int step;
};
priority_queueq; //优先队列中元素的比较规则默认是按元素的值从大到小排序;
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_queueq; //优先队列中元素的比较规则默认是按元素的值从大到小排序;
切记:从小到大排序采用“>”号;如果要从大到小排序,则采用“<”号;
优先队列不只是用来提高效率滴~