NYOJ 284--坦克大战【BFS && 优先队列】
2015-11-23 17:33
525 查看
坦克大战
时间限制: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
解析:很水的BFS,要注意的一点是,每走一步用的时间不一样,所以要用优先队列来处理。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
char map[400][400];
int n, m;
int sx, sy;
int dir[][2] = {0, 1, 0, -1, 1, 0, -1, 0};
int vis[400][400];
struct node {
int x, y, t;
friend bool operator < (node a, node b){
return a.t > b.t;
}
};
bool check(int x, int y){
if(x >= 0 && x < n && y >= 0 && y < m && map[x][y] != 'S' && map[x][y] != 'R')
return true;
else
return false;
}
void bfs(){
memset(vis, 0, sizeof(vis));
priority_queue<node>q;
node st, ed;
st.x = sx;
st.y = sy;
st.t = 0;
vis[st.x][st.y] = 1;
q.push(st);
while(!q.empty()){
st = q.top();
q.pop();
if(map[st.x][st.y] == 'T'){
printf("%d\n", st.t);
return;
}
for(int i = 0; i < 4; ++i){
ed = st;
ed.x += dir[i][0];
ed.y += dir[i][1];
if(!check(ed.x, ed.y)) continue;
if(map[ed.x][ed.y] == 'B')
ed.t += 2;
else
ed.t += 1;
if(!vis[ed.x][ed.y]){
vis[ed.x][ed.y] = 1;
q.push(ed);
}
}
}
printf("-1\n");
}
int main (){
while(scanf("%d%d", &n, &m), n || m){
for(int i = 0; i < n; ++i){
scanf("%s", map[i]);
for(int j = 0; j < m; ++j)
if(map[i][j] == 'Y'){
sx = i;
sy = j;
}
}
bfs();
}
return 0;
}
相关文章推荐
- [Python]Flask构建网站分析应用
- [Python]Flask构建网站分析应用
- HTTP请求过程
- AndroidStudio单元测试——instrumentation
- 利用第三方异步网路加载库AsyncHttpClient
- Python 基础
- 软件工程 BUAAMOOC项目Postmortem结果
- python环境准备以及easy_install和pip的安装
- 第十一周--数据结构--用二叉树求解代数表达式
- C#读取本机IP
- Linux内核模块命令相关
- 解决打印中的乱码问题
- postman中 form-data、x-www-form-urlencoded、raw、binary的区别
- js 语法
- 八皇后问题
- RecyclerView完全解析
- Linux input子系统实例分析(一)
- Jquery-uploadify多文件上传插件使用介绍
- android重要的依赖库与SDK收集
- MyEclipse2015个性化设置