POJ - 2312 Battle City BFS+优先队列
Battle City
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
Output
Sample Input
3 4
YBEB
EERE
SSTE
0 0
Sample Output
8
又是一道BFS+优先队列的题,和之前的WAR CHESS类似,但判断条件较少。这次主要是想作个比较,用暴力DFS跑一边,果断TLE。。BFS+优先队列只遍历最少步数之内的点一次,时间效率快很多
dfs length 861 time 1s+
bfs length 1469 time 0.016s memory 1m
| Status | Time Limit Exceeded |
|---|---|
| Length | 861 |
| Lang | G++ |
| Submitted | 2017-07-22 22:22:12 |
| Shared | |
| RemoteRunId | 17284030 |
| Status | Accepted |
|---|---|
| Time | 16ms |
| Memory | 1028kB |
| Length | 1469 |
| Lang | G++ |
| Submitted | 2017-07-22 22:22:58 |
| Shared | |
| RemoteRunId | 17284040 |
#include<stdio.h>
#include<string.h>
char a[][];
int b[][];
int n,m,f,bx,by,min;
void dfs(int x,int y,int s)
{
if(x<||y<||x>=n||y>=m) return;
if(b[x][y]==) return;
if(a[x][y]=='T'){
f=;
if(s<min) min=s;
return;
}
else if(a[x][y]=='S'){
b[x][y]=;
return;
}
else if(a[x][y]=='R'){
b[x][y]=;
return;
}
else if(a[x][y]=='B'){
s++;
}
b[x][y]=;dfs(x+,y,s+);b[x][y]=;
b[x][y]=;dfs(x,y+,s+);b[x][y]=;
b[x][y]=;dfs(x-,y,s+);b[x][y]=;
b[x][y]=;dfs(x,y-,s+);b[x][y]=;
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)&&!(n==&&m==)){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(i=;i<n;i++){
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='Y'){
bx=i;by=j;
}
}
}
f=;
min=;
dfs(bx,by,);
if(f==) printf("%d\n",min);
else printf("-1\n");
}
return ;
}
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std; char a[][];
int b[][];
int t[][]={{,},{,},{-,},{,-}};
int n,m,bx,by; struct Node{
int x,y,s;
friend bool operator<(Node a,Node b)
{
return a.s>b.s;
}
}node; void bfs()
{
int i,f=,tx,ty;
priority_queue<Node> q;
node.x=bx;
node.y=by;
node.s=;
b[bx][by]=;
q.push(node);
while(q.size()){
for(i=;i<;i++){
tx=q.top().x+t[i][];
ty=q.top().y+t[i][];
if(tx<||ty<||tx>=n||ty>=m) continue;
if(b[tx][ty]==) continue;
b[tx][ty]=;
if(a[tx][ty]=='T'){
printf("%d\n",q.top().s+);
f=;
return;
}
else if(a[tx][ty]=='S'){
b[tx][ty]=;
continue;
}
else if(a[tx][ty]=='R'){
b[tx][ty]=;
continue;
}
else if(a[tx][ty]=='B'){
node.x=tx;
node.y=ty;
node.s=q.top().s+;
b[tx][ty]=;
q.push(node);
}
else if(a[tx][ty]=='E'){
node.x=tx;
node.y=ty;
node.s=q.top().s+;
b[tx][ty]=;
q.push(node);
}
}
q.pop();
}
if(f==) printf("-1\n");
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)&&!(n==&&m==)){
memset(a,,sizeof(a));
memset(b,,sizeof(b));
for(i=;i<n;i++){
scanf("%s",a[i]);
for(j=;j<m;j++){
if(a[i][j]=='Y'){
bx=i;by=j;
}
}
}
bfs();
}
return ;
}
POJ - 2312 Battle City BFS+优先队列的更多相关文章
- poj 2312 Battle City【bfs+优先队列】
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 Des ...
- poj 2312 Battle City
题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...
- B - Battle City bfs+优先队列
来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...
- C - Battle City BFS+优先队列
Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...
- poj 2312 Battle City(优先队列+bfs)
题目链接:http://poj.org/problem?id=2312 题目大意:给出一个n*m的矩阵,其中Y是起点,T是终点,B和E可以走,S和R不可以走,要注意的是走B需要2分钟,走E需要一分钟. ...
- POJ 2312:Battle City(BFS)
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9885 Accepted: 3285 Descr ...
- Battle City 优先队列+bfs
Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...
- poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】
题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...
- POJ 1724 ROADS(BFS+优先队列)
题目链接 题意 : 求从1城市到n城市的最短路.但是每条路有两个属性,一个是路长,一个是花费.要求在花费为K内,找到最短路. 思路 :这个题好像有很多种做法,我用了BFS+优先队列.崔老师真是千年不变 ...
随机推荐
- sublime常用的插件
Sublime Text常用插件 1.Package Control 快捷键ctrl+~调出Sublime Text控制台,然后输入以下代码(Sublime Text3)安装Package Contr ...
- MySQL安装过程中出现“APPLY security settings错误”的解决方式
***********************************************声明*************************************************** ...
- ecshop 国付宝支付接口
function get_code($order, $payment){ $version = '2.2'; $charset = '1'; $language = '1'; $signType = ...
- linux 查找最后几条数据
tail(选项)(参数) -n<N>或——line=<N>:输出文件的尾部N(N位数字)行内容. 例如:grep 查询 2018-02-*/*.log |tail -n 5查询 ...
- 关于TCP通信程序中数据的传递格式
前言 在之前的回射程序中,实现了字符串的传递与回射.幸运的是,字符串的传递不用担心不同计算机类型的大小端匹配问题,然而,如果传递二进制数据,这就是一个要好好考虑的问题.在客户端和服务器使用不同的字节序 ...
- 如果在 Code First 模式下使用,则使用 T4 模板为 Database First 和 Model First
web.config里的链接字符串最好和app.config里相同,因为ef的链接字符串需要一些特殊的参数
- 导入EXCEL 时间数据为小数 问题
同事在做将EXCEL导入数据库功能时发现一个奇怪的问题:在EXCEL中,有一列数据明明呈现出时间格式,比如:18:35,但导到数据库中,居然一串长长的小数:0.7743055555555556,我靠, ...
- 多用GCD,少用performSelect系列方法
例如,要延后执行某项任务,可以有下面两种实现方式,而我们应该优先考虑第二种: // Using performSelect: withObject: afterDelay: [self perform ...
- EasyDarwin开源团队招募开发组成员
EasyDarwin开源流媒体服务器项目招募开发组成员,共同更新和维护EasyDarwin流媒体服务器,决策EasyDarwin后续开发方向: 加入要求: 1.对开源流媒体项目有浓厚兴趣: 2.有一定 ...
- Object/Relational Mapping 数学关系 反面向对象
[hibernate ORM 是对象关系映射框架 事实上的持久化存储引擎] http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/ ...