【bfs+优先队列】POJ2312-Battle City
【思路】
题目中的“可以沿直线发射打破砖墙”可能会迷惑到很多人,实际上可以等价理解为“通过砖墙的时间为2个单位”,这样题目就迎刃而解了。第一次碰到时可能不能很好把握,第二次基本就可以当作水题了。
【错误点】
1.不能用裸的bfs。广搜的实际思想是将到达时间最短的放在队首,这样首次到达终点即为时间的最小值。通过砖墙的时间为两个单位,通过砖墙后可能不是时间最小值。用优先队列可以解决这一问题。
2.c++中memset初始化为memset(vis,0,sizeof(vis)),很多人可能会写成memset(vis,sizeof(vis),0)。
#include<iostream>
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
const int MAXN=+;
struct rec
{
int x,y,cost;
bool operator < (const rec &x) const
{
return (cost > x.cost);
}
};
char map[MAXN][MAXN];
int m,n,yx,yy; void init()
{
getchar();
for (int i=;i<m;i++)
{
for (int j=;j<n;j++)
{
scanf("%c",&map[i][j]);
if (map[i][j]=='Y')
{
yx=i;yy=j;
}
}
getchar();
}
} int bfs()
{
int vis[MAXN][MAXN];
int dx[]={,,,-};
int dy[]={,-,,};
priority_queue<rec> que;
memset(vis,,sizeof(vis));
vis[yx][yy]=;
rec now;
now.x=yx;now.y=yy;now.cost=;
que.push(now);
while (!que.empty())
{
rec head=que.top();
if (map[head.x][head.y]=='T') return(head.cost);
que.pop();
for (int i=;i<;i++)
{
int tempx=head.x+dx[i],tempy=head.y+dy[i];
if (tempx<||tempx>=m||tempy<||tempy>=n||map[tempx][tempy]=='R'||map[tempx][tempy]=='S'||vis[tempx][tempy]) continue;
vis[tempx][tempy]=;
now.x=tempx;now.y=tempy;
now.cost=head.cost+;
if (map[tempx][tempy]=='B') now.cost++;
que.push(now);
}
}
return(-);
} int main()
{
while (scanf("%d%d",&m,&n)!=EOF)
{
if (m==n && n==) break;
init();
cout<<bfs()<<endl;
}
return ;
}
【bfs+优先队列】POJ2312-Battle City的更多相关文章
- poj2312 Battle City 【暴力 或 优先队列+BFS 或 BFS】
题意:M行N列的矩阵.Y:起点,T:终点.S.R不能走,走B花费2,走E花费1.求Y到T的最短时间. 三种解法.♪(^∇^*) //解法一:暴力 //157MS #include<cstdio& ...
- B - Battle City bfs+优先队列
来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...
- poj 2312 Battle City【bfs+优先队列】
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 Des ...
- 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+优先队列
Battle City 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)
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9885 Accepted: 3285 Descr ...
- poj 2312 Battle City
题目连接 http://poj.org/problem?id=1840 Battle City Description Many of us had played the game "Bat ...
- Battle City
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7208 Accepted: 2427 Descr ...
随机推荐
- SQL注入之逗号拦截绕过
目前所知博主仅知的两个方法 1.通过case when then 2.join [一]case when then mysql,,,,,,, ) ) end; +----+-----------+-- ...
- 在 kernel 下打出 有帶參數的log。 怪異現象與解決方式。
code battery_log(BAT_LOG_CRTI, "youchihwang abc10010 xxxaaa8-2\r\n"); battery_log(BAT_LOG_ ...
- (十七)vmware无法将网络更改为桥接状态
故障现象,导致虚拟机无法正常上网 设备管理器中的驱动设备正常加载,但是注意这两个虚拟网卡是有问题的 将这两个虚拟网卡删除 只剩物理网卡了,重新启动电脑 将虚拟机里的网络设置删除 清空网卡后点击恢复默认 ...
- C json实战引擎 三 , 最后实现部分辅助函数
引言 大学读的是一个很时髦的专业, 学了四年的游戏竞技. 可惜没学好. 但认真过, 比做什么都认真. 见证了 ...... 打的所有游戏人物中 分享一位最喜爱 的 “I've been alone ...
- pycaffe使用.solverstate文件继续训练
import caffe solver_file = "solver.prototxt" solverstate = "xx.solverstate" caff ...
- Django REST Framework JWT提供的登录签发的视图
Django REST Framework JWT提供了一个视图.在我们登录的时候,会校验用户名.密码是否正确.如果信息无误,可以返回一个JWT token.就可以简单地实现了记录用户登录状态. 用法 ...
- python_day4学习笔记
一.内置函数
- selenium+python自动化80-文件下载(不弹询问框)【转载】
转至博客:上海-悠悠 前言 上一篇是点弹出框上的按钮去保存文件,本篇介绍一种更加优雅的方法,加载Firefox和Chrome的配置文件,不弹出询问框后台下载. 一.FirefoxProfile 1.点 ...
- Mysql用户管理(远程连接、授权)
1.新建用户 登录MYSQL: @>mysql -u root -p @>密码 创建用户: mysql> insert into mysql.user(Host,User,Passw ...
- npm更新包
方法一手动跟新: 手动修改package.json中依赖包版本,执行npm install --force,强制从远程下载所有包更新本地包 方法二使用第三方插件: npm install -g npm ...