NYOJ-284 坦克大战 ------BFS/优先队列

 

坦克大战

时间限制: 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).
NYOJ-284 坦克大战 ------BFS/优先队列

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
  1 /* 功能Function Description:     NYOJ-284

  2    开发环境Environment:          DEV C++ 4.9.9.1

  3    技术特点Technique:

  4    版本Version:

  5    作者Author:                   可笑痴狂

  6    日期Date:                      20120813

  7    备注Notes:

  8         本题应该使用优先队列的,表示不会,就只能用数组模拟手动排序了,因为每次加入的节点的优先级可能不同(访问到砖块的要2单位时间,而访问到空白的只需1单位时间)

  9         所以要对队列中的step进行从小到大排序这样搜索得到的才是正确的解。

 10 */

 11 

 12 

 13 //代码一:

 14 #include<cstdio>

 15 #include<stdlib.h>

 16 

 17 char map[310][310];

 18 int m,n;

 19 int dir[4][2]={-1,0,0,1,1,0,0,-1};

 20 

 21 typedef struct point 

 22 {

 23     int x,y,step;

 24 }point;

 25 

 26 int cmp(const void *p,const void *q)

 27 {

 28     return (*(point *)p).step-(*(point *)q).step;

 29 }

 30 

 31 int BFS(point s,point t)

 32 {

 33     point q[10000];

 34     int head,tail,i;

 35     head=tail=1;

 36     point temp;

 37     q[tail++]=s;

 38     while(head!=tail)

 39     {

 40         s=q[head++];

 41         if(s.x==t.x&&s.y==t.y)

 42             return s.step;

 43         for(i=0;i<4;++i)

 44         {

 45             temp.x=s.x+dir[i][0];

 46             temp.y=s.y+dir[i][1];

 47             if(temp.x<=0||temp.x>n||temp.y<=0||temp.y>m)

 48                 continue;

 49             if(map[temp.y][temp.x]=='S'||map[temp.y][temp.x]=='R')

 50                 continue;

 51             else if(map[temp.y][temp.x]=='B')

 52             {

 53                 temp.step=s.step+2;

 54                 map[temp.y][temp.x]='S';

 55             }

 56             else

 57             {

 58                 temp.step=s.step+1;

 59                 map[temp.y][temp.x]='S';

 60             }

 61             q[tail++]=temp;

 62         }

 63         qsort(&q[head],tail-head,sizeof(point),cmp);

 64     }

 65     return -1;

 66 }

 67 

 68 int main()

 69 {

 70     int i,j;

 71     point s,t;

 72     while(scanf("%d%d",&m,&n),m||n)

 73     {

 74         getchar();

 75         for(i=1;i<=m;++i)

 76         {

 77             for(j=1;j<=n;++j)

 78             {

 79                 scanf("%c",&map[i][j]);

 80                 if(map[i][j]=='Y')

 81                 {

 82                     s.x=j;

 83                     s.y=i;

 84                     s.step=0;

 85                     map[i][j]='S';

 86                 }

 87                 else if(map[i][j]=='T')

 88                 {

 89                     t.x=j;

 90                     t.y=i;

 91                 }

 92             }

 93             getchar();

 94         }

 95         printf("%d\n",BFS(s,t));

 96     }

 97     return 0;

 98 }

 99 

100 

101 //代码二:------优先队列(转)

102 #include <iostream>

103 #include <queue>

104 #include <cstdio>

105 #include <cstring>

106 using namespace std;

107 #define  max 301

108 char map[max][max];

109 int n,m;

110 bool vist[max][max];

111 int d[4][2]={{1,0},{0,1},{-1,0},{0,-1}};

112 

113 struct node 

114 {

115     int x,y,c;

116     friend bool operator <(const node &s1,const node &s2)

117     {

118         return s1.c>s2.c;

119     }

120 };

121 node s,e;

122 

123 int bfs()

124 {

125     priority_queue < node > q;

126     memset(vist,0,sizeof(vist));

127     node now,t;

128     s.c=0;

129     q.push(s);

130     vist[s.x][s.y]=1;

131     while (!q.empty())

132     {

133         now=q.top();

134         q.pop();

135         if (now.x==e.x&&now.y==e.y)

136         {

137             return now.c;

138         }    

139         for (int i=0;i<4;i++)

140         {

141             t.x=now.x+d[i][0];

142             t.y=now.y+d[i][1];

143             if (t.x>=0&&t.x<n&&t.y>=0&&t.y<m&&!vist[t.x][t.y])

144             {        

145                 vist[t.x][t.y]=1;    

146                 if (map[t.x][t.y]=='B')

147                 {

148                     t.c=now.c+2;

149                     q.push(t);

150                 }

151                 else 

152                     if (map[t.x][t.y]=='E'||map[t.x][t.y]=='T')

153                     {

154                         t.c=now.c+1;

155                         q.push(t);

156                     }

157             }

158         }

159     }

160     return -1;

161 }

162 

163 int main()

164 {

165     int i,j;

166     while (cin>>n>>m,m+n)

167     {

168         for (i=0;i<n;i++)

169         {

170             for (j=0;j<m;j++)

171             {

172                 cin>>map[i][j];

173                 if (map[i][j]=='Y')                  

174                     s.x=i,s.y=j;                  

175                 if (map[i][j]=='T')                  

176                     e.x=i,e.y=j;                  

177             }

178         }

179         int sum=bfs();

180      cout<<sum<<endl;

181     }

182     

183     return 0;

184 }

 

你可能感兴趣的:(优先队列)