POJ 2312 Battle City 优先队列+bfs
2017-03-07 20:17
393 查看
Battle City
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8597 Accepted: 2882
Description
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?
Input
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.
Output
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.
Sample Input
3 4
YBEB
EERE
SSTE
0 0
Sample Output
8
题目链接:http://poj.org/problem?id=2312
题意
计算从'Y'走到'T'的最短时间,其中'S'和'R'不能走,'E'花费时间为1,'B'花费时间为2。
题解
优先队列bfs,在遍历的过程中取出当前时间最少的节点进行展开即可。
#include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> #include <math.h> #include <stack> #include <queue> #include <vector> #define INF 0xffffffff using namespace std; const int maxn = 305; int ax, bx, ay, by; int m, n; char pic[maxn][maxn]; int vis[maxn][maxn]; int dr[4] = {-1,0,1,0}; int dc[4] = {0,1,0,-1}; struct Node{ int r, c, t; Node(){} Node(int r, int c, int t):r(r), c(c), t(t){} bool operator < (const Node & rhs) const{ //定义小于,传进来的比当前小为真 return t > rhs.t; } }; int go(int r, int c){ if(r<0 || r>=m || c<0 || c>=n || pic[r][c] == 'S' || pic[r][c] == 'R' || vis[r][c]) return 0; else if(pic[r][c] == 'T' || pic[r][c] == 'E') return 1; else return 2; } void solve(){ priority_queue<Node> q; Node st(ax, ay, 0); q.push(st); vis[ax][ay] = 1; while(!q.empty()){ Node u = q.top(); q.pop(); for(int i=0; i<4; i++){ int nextr = u.r + dr[i]; int nextc = u.c + dc[i]; int t = go(nextr, nextc); if(t){ vis[nextr][nextc] = 1; Node u1(nextr, nextc, u.t+t); q.push(u1); if(nextr == bx && nextc == by){ printf("%d\n", u1.t); return; } } } } printf("-1\n"); } int main(){ while(scanf("%d%d", &m, &n) == 2 && m && n){ for(int i=0; i<m; i++){ scanf("%s", pic[i]); for(int j=0; j<n; j++){ if(pic[i][j] == 'Y'){ ax = i, ay = j; } if(pic[i][j] == 'T'){ bx = i, by = j; } } } memset(vis, 0, sizeof(vis)); if(ax == bx && ay == by){ printf("0\n"); } else{ solve(); } } return 0; }
相关文章推荐
- POJ 2312 Battle City 优先队列+BFS
- POJ 2312 Battle City 优先队列+BFS
- 深入探讨POJ 2312 Battle City 优先队列+BFS
- POJ 2312 - Battle City
- POJ 2312 Battle City 优先队列+BFS
- 【BFS+优先队列】Battle City(poj-2312)
- POJ 2312 Battle City 笔记
- POJ 2312 Battle City (bfs+优先队列)
- poj2312 - Battle City (BFS变形)
- poj 2312 Battle City 优先队列+bfs 或 记忆化广搜
- POJ 2312 Battle City【Bfs+优先队列】
- Battle City(POJ - 2312)(深搜+优先队列)
- POJ - 2312 Battle City (记忆话搜索bfs+优先队列)
- POJ 2312 Battle City(优先队列+BFS)
- 【POJ】2312-Battle City (bfs,优先队列)
- [算法] BFS : poj 2312 Battle City 示例
- poj 2312:Battle City
- poj 2312 Battle City
- POJ 2312 Battle City
- POJ 2312 Battle City(优先队列 + BFS)