坦克大战
时间限制: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 【思路】1 遇到 河‘R’、钢墙‘S’过不去, 遇到‘E’用时1, 遇到砖墙‘B’用时2; 2 此题是在迷宫问题的基础上做了些改动,就是地图上能走的点可能耗费时间1,也可能耗费时间2。那么,元素在出队列时,不能简单的按照以前的入队顺序出队了,而应该让时间最短的先出队,这样就能够保证先入队的点一定是时间最短的,那么搜到终点时也时间也一定最小。现在回头想下,迷宫问题之所以没有考虑这个问题,是因为先入队的点的时间一定不大于后入队的。言归正传,如何让时间最短的先出队呢?----------STL已经帮我们弄好了,priority_queue;
优先队列+bfs重载方法一:
1 struct node 2 { 3 int x,y; 4 int step; 5 }; 6 priority_queue<node>q; //优先队列中元素的比较规则默认是按元素的值从大到小排序; 7 8 bool operator<(const node &a,const node &b) //括号里面是const 而且还必须是引用 9 { 10 return a.step > b.step; //从小到大排序。重载小于号。因为默认是从大到小 11 }
重载方法二:
1 struct node 2 { 3 int x,y; 4 int step; //定义一个优先队列 5 friend bool operator<(node a, node b) 6 { //从小到大排序采用“>”号;如果要从大到小排序,则采用“<”号 7 return a.step > b.step; //从小到大排序 8 } 9 }; 10 priority_queue<node>q; //优先队列中元素的比较规则默认是按元素的值从大到小排序;
切记:从小到大排序采用“>”号;如果要从大到小排序,则采用“<”号; AC代码:
1 #include<cstdio> 2 #include<queue> 3 #include<string.h> 4 using namespace std; 5 #define max 310 6 int n, m, sx, sy, ex, ey; 7 char map[305][305]; 8 int dx[4]={0, 0, 1, -1}; 9 int dy[4]={1, -1, 0, 0}; 10 struct node{ 11 int x, y, step; 12 friend bool operator < (node a, node b) 13 { 14 return a.step > b.step;//步数少的优先 15 } 16 }a, b; 17 int judge(int x, int y) 18 { 19 if(x < 0 || x >= m || y < 0 || y >= n) 20 return false; 21 if(map[x][y] == ‘R‘ || map[x][y] == ‘S‘) 22 return false; 23 return true; 24 } 25 bool bfs() 26 { 27 int flag = 0; 28 priority_queue<node>q; 29 a.x = sx; a.y = sy; a.step = 0; 30 q.push(a); 31 while(!q.empty()) 32 { 33 b = q.top(); 34 q.pop(); 35 if(b.x == ex && b.y == ey) 36 { flag = 1; break; } 37 for(int k = 0; k < 4; k++) 38 { 39 a.x = b.x + dx[k]; 40 a.y = b.y + dy[k]; 41 if(judge(a.x, a.y)) 42 { 43 if(map[a.x][a.y] == ‘B‘) 44 a.step = b.step + 2; 45 else 46 a.step = b.step + 1; 47 map[a.x][a.y] = ‘S‘; 48 q.push(a); 49 } 50 } 51 } 52 if(flag) printf("%d\n", b.step); 53 else printf("-1\n"); 54 } 55 int main() 56 { 57 while(scanf("%d %d", &m, &n) != EOF) 58 { 59 if(m + n == 0) break; 60 for(int i = 0; i < m; i++) 61 { 62 scanf("%s", &map[i]); 63 for(int j = 0; j < n; j++) 64 { 65 if(map[i][j] == ‘Y‘) 66 { sx = i; sy = j; } 67 if(map[i][j] == ‘T‘) 68 { ex = i; ey = j; } 69 } 70 } 71 bfs(); 72 } 73 return 0; 74 }
时间: 2024-10-15 22:40:35