坦克大战
时间限制:
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
代码:
#include <iostream> #include <queue> #include <cstring> #include <cstdio> using namespace std; int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; char c[320][320]; int vis[320][320]; int i, j, x, y, n, m, si, sj; struct T { //定义结构体,用来记录坐标 int x; int y; int cnt; bool operator <(const T& a) const { return cnt > a.cnt; } }now,eed; int can(struct T a) {//判断是否能够经过 if(c[a.x][a.y] == 'S' || c[a.x][a.y] == 'R' || a.x < 0 || a.x >= n || a.y < 0 || a.y >= m || vis[a.x][a.y]) return 0; return 1; } int bfs() { priority_queue<T> map; //之所以使用优先队列,是因为在同等步数的情况下,要选择属于空地的 memset(vis, 0, sizeof(vis)); now.x = si; now.y = sj; now.cnt = 0; map.push(now); vis[now.x][now.y] = 1; while(!map.empty()) { now = map.top(); if(c[now.x][now.y] == 'T') return now.cnt; for(i = 0; i < 4; i++) { //向四个方向进行搜索 eed.x = now.x + dir[i][0]; eed.y = now.y + dir[i][1]; if(can(eed)) { if(c[eed.x][eed.y] == 'B') //碰到的是砖,需要两步 eed.cnt = now.cnt + 2; if(c[eed.x][eed.y] == 'E') //遇到的是空地,只需要一步 eed.cnt = now.cnt + 1; if(c[eed.x][eed.y] == 'T') //如果是Target,即目标,也需要一步 eed.cnt = now.cnt + 1; map.push(eed); vis[eed.x][eed.y] = 1; //对走过的地方进行标记 } } map.pop(); //出队 } return -1; } int main() { while(scanf("%d %d",&n, &m) && n+m != 0) { for(i = 0; i < n; i++) { for(j = 0; j < m; j++) { cin >> c[i][j]; if(c[i][j] == 'Y') { si = i; sj = j; } } } cout << bfs()<< endl; } return 0; }
-
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.