NYOJ-284 坦克大战
2014-07-27 19:53
239 查看
坦克大战
时间限制: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
BFS 从Y到T,遇B加 2 ,遇 E ,T 加 1
01.
#include<iostream>
02.
#include<cstring>
03.
#include<queue>
04.
#include<cstdio>
05.
using
namespace
std;
06.
int
m,n;
07.
char
map[310][310];
08.
bool
vis[310][310];
09.
struct
Node
10.
{
11.
int
x,y,steps;
12.
friend
bool
operator < (Node a,Node b)
13.
{
14.
return
a.steps > b.steps;
15.
}
16.
}you,tar;
17.
int
mov[][2]={0,1,0,-1,1,0,-1,0};
18.
priority_queue<Node> PQ;
19.
int
check(Node a)
20.
{
21.
if
(a.x<0||a.y<0||a.x>=m||a.y>=n)
22.
return
0;
23.
if
(vis[a.x][a.y])
return
0;
24.
if
(map[a.x][a.y]==
'B'
)
return
2;
25.
if
(map[a.x][a.y]==
'E'
)
return
1;
26.
if
(map[a.x][a.y]==
'T'
)
return
1;
27.
return
0;
28.
}
29.
int
BFS()
30.
{
31.
Node temp,sta;
32.
int
count;
33.
vis[you.x][you.y]=1;
34.
PQ.push(you);
35.
while
(!PQ.empty())
36.
{
37.
sta=temp=PQ.top();
38.
PQ.pop();
39.
for
(
int
i=0;i<4;i++)
40.
{
41.
temp.x+=mov[i][0];
42.
temp.y+=mov[i][1];
43.
if
(count=check(temp))
44.
{
45.
temp.steps+=count;
46.
if
(map[temp.x][temp.y]==
'T'
)
47.
return
temp.steps;
48.
vis[temp.x][temp.y]=1;
49.
PQ.push(temp);
50.
}
51.
temp=sta;
52.
}
53.
}
54.
return
-1;
55.
}
56.
int
main()
57.
{
58.
while
(
scanf
(
"%d%d"
,&m,&n),m||n)
59.
{
60.
for
(
int
i=0;i<m;i++)
61.
{
62.
scanf
(
"%s"
,map[i]);
63.
for
(
int
j=0;j<n;j++)
64.
if
(map[i][j]==
'Y'
)
65.
you.x=i,you.y=j;
66.
else
if
(map[i][j]==
'T'
)
67.
tar.x=i,tar.y=j;
68.
}
69.
memset
(vis,0,
sizeof
(vis));
70.
while
(!PQ.empty())
71.
PQ.pop();
72.
cout<<BFS()<<endl;
73.
}
74.
return
0;
75.
}
相关文章推荐
- nyoj284 坦克大战(dijkstra(bfs+优先队列))
- 南阳(NYOJ)284 坦克大战
- NYOJ-284-坦克大战(BFS)
- 坦克大战(nyoj284)
- nyoj 284 坦克大战(广搜+优先队列)
- nyoj 284 坦克大战
- NYOJ 284 坦克大战
- nyoj-284-坦克大战
- nyoj--284 坦克大战(bfs+优先队列)
- NYOJ 284 坦克大战
- nyoj 284 坦克大战
- nyoj 284 坦克大战
- nyoj 284 坦克大战(BFS)
- nyoj-----284坦克大战(带权值的图搜索)
- NYOJ-284坦克大战(优先队列)
- nyoj 284坦克大战(优先队列+BFS)
- NYOJ-284 坦克大战
- NYOJ 284 坦克大战(BFS)
- nyoj--284--坦克大战(bfs模板)
- nyoj 284 坦克大战