nyoj 284-坦克大战

http://acm.nyist.net/JudgeOnline/problem.php?pid=284

 

坦克大战

时间限制: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
5 6
BYEESS
BSREEE
BSRSSE
BBBEEE
SSTSSS 0 0
样例输出
8
12

原题中只有一个样例,第二个样例是在讨论区高手发的~我刚开始写用队列bfs,第二个样例总是过不了~后来在讨论区看到优先队列,然后果断百度学习了一下,这才通过~~每组数据后要把队列清空,我因为这又wa了几次~~我看别人的代码没有用vis数组,果断把vis删了试了一下,毫无疑问超时~~又仔细看了看,发现把map改了一下~~暂时我还没理解,把别人的代码借用一下,供大家参考~~

这是我的代码:

  1 //106496 jiaolinfeng 坦克大战 Accepted  0  680 C/C++ 12-18 10:06:10   
2 //106495 jiaolinfeng 坦克大战 Accepted 4 328 C/C++ 12-18 10:02:31
3 //106494 jiaolinfeng 坦克大战 TimeLimitExceeded -- -- C/C++ 12-18 10:01:48
4 //106493 jiaolinfeng 坦克大战 TimeLimitExceeded -- -- C/C++ 12-18 09:59:57
5 //106492 jiaolinfeng 坦克大战 Accepted 0 1056 C/C++ 12-18 09:58:50
6 //106491 jiaolinfeng 坦克大战 TimeLimitExceeded -- -- C/C++ 12-18 09:56:39
7 //106490 jiaolinfeng 坦克大战 WrongAnswer -- -- C/C++ 12-18 09:53:35
8 //105311 jiaolinfeng 坦克大战 WrongAnswer -- -- C/C++ 12-13 18:21:56
9 //105310 jiaolinfeng 坦克大战 WrongAnswer -- -- C/C++ 12-13 18:20:25
10 #include<iostream>
11 #include<cstdio>
12 #include<string.h>
13 #include<queue>
14 using namespace std;
15 int M, N;
16 int dx[] = {-1, 1, 0, 0};
17 int dy[] = {0, 0, -1, 1};
18 int map[310][310];
19 char str[310];
20 int ok, bx, by, ex, ey;
21 typedef struct
22 {
23 int v;
24 int step;
25 }State;
26 int vis[310][310];
27 priority_queue<State> q;
28 State s;
29 bool operator<(State a, State b)
30 {
31 return a.step > b.step;
32 }
33 void bfs(int x, int y)
34 {
35 int nx, ny, d;
36 s.v = x * N + y;
37 vis[x][y] = 1;
38 q.push(s);
39 while(!q.empty())
40 {
41 State u = q.top();
42 q.pop();
43 x = u.v / N;
44 y = u.v % N;
45 for(d = 0; d < 4; d++)
46 {
47 nx = x + dx[d];
48 ny = y + dy[d];
49 if(nx >= 0 && nx < M && ny >= 0 && ny < N && !vis[nx][ny])
50 {
51 if(nx == ex && ny == ey)
52 {
53 printf("%d\n", u.step+1);
54 ok = 1;
55 return;
56 }
57 else if(map[nx][ny] == 1)
58 {
59 State v;
60 v.v = nx * N + ny;
61 v.step = u.step + 1;
62 vis[nx][ny] = 1;
63 q.push(v);
64 // map[nx][ny] = 'S';
65 }
66 else if(map[nx][ny] == 'S' || map[nx][ny] == 'R')
67 continue;
68 else if(map[nx][ny] == 'B')
69 {
70 State v;
71 v.v = nx * N + ny;
72 v.step = u.step + 2;
73 vis[nx][ny] = 1;
74 q.push(v);
75 // map[nx][ny] = 'S';
76 }
77 }
78 }
79 }
80 }
81 int main()
82 {
83 int i, j;
84 while(scanf("%d%d", &M, &N) && (M || N))
85 {
86 for(i = 0; i < M; i++)
87 {
88 scanf("%s", str);
89 for(j = 0; j < N; j++)
90 {
91 switch(str[j])
92 {
93 case 'Y': bx = i; by = j; map[i][j] = 1; break;
94 case 'T': ex = i; ey = j; map[i][j] = 1; break;
95 case 'E': map[i][j] = 1; break;
96 case 'B': map[i][j] = 'B'; break;
97 case 'S': map[i][j] = 'S'; break;
98 case 'R': map[i][j] = 'R';
99 }
100 }
101 }
102 s.step = 0;
103 ok = 0;
104 memset(vis, 0, sizeof(vis));
105 bfs(bx, by);
106 if(!ok)
107 printf("-1\n");
108 while(!q.empty())
109 q.pop();
110 }
111 return 0;
112 }




这个是在CSDN上看到的,没用vis数组,没超时~~我把网址贴下来,表示引用~~http://blog.csdn.net/null_nowonder/article/details/7001994

我同时也向理解的高手求教

 1 #include<iostream>
2 #include<cstdio>
3 #include<queue>
4 using namespace std;
5 typedef struct node
6 {
7 int x,y,step;
8 }node;
9 bool operator<(const node &a,const node &b)
10 {
11 return a.step>b.step;
12 }
13 int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
14 int main()
15 {
16 char map[305][305];
17 int i,j,ans,n,m;
18 node a,b;
19 priority_queue<node>q;
20 while(scanf("%d%d",&n,&m)&&(n||m))
21 {
22 a.x=0;
23 for(i=0;i<n;i++)
24 {
25 scanf("%s*c",&map[i]);
26 for(j=0;j<m;j++)
27 {
28 if(map[i][j]=='Y')
29 {
30 a.x=i;
31 a.y=j;
32 a.step=0;
33 q.push(a);
34 }
35 }
36 }
37 bool flag=false;
38 while(!q.empty())
39 {
40 a=q.top();
41 q.pop();
42 for(i=0;i<4;i++)
43 {
44 b.x=a.x+dir[i][0];
45 b.y=a.y+dir[i][1];
46 if(b.x>=0 && b.x<n && b.y>=0 && b.y<m)
47 {
48 if(map[b.x][b.y]=='T')
49 {
50 ans=a.step+1;
51 flag=true;
52 break;
53 }
54 else if(map[b.x][b.y]=='E')
55 {
56 b.step=a.step+1;
57 q.push(b);
58 map[b.x][b.y]='S';
59 }
60 else if(map[b.x][b.y]=='B')
61 {
62 b.step=a.step+2;
63 q.push(b);
64 map[b.x][b.y]='S';
65 }
66 }
67 }
68 if(flag)
69 break;
70 }
71 if(flag)
72 printf("%d\n",ans);
73 else
74 printf("-1\n");
75 while(!q.empty())
76 {
77 q.pop();
78 }
79 }
80 return 0;
81 }

再次表示感谢~~

 
posted @ 2011-12-18 10:19  枫萧萧  阅读(913)  评论(2编辑  收藏  举报