【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 ...
随机推荐
- bzoj 3028 母函数
首先我们可以求出来所有食物的母函数: 汉堡:f(x)=1/(1-x^2). 可乐:f(x)=1+x. 鸡腿:f(x)=1+x+x^2. 蜜桃多:f(x)=x/(1-x^2). 鸡块:f(x)=1/(1 ...
- cookie、session、localstorage
最早的Cookies问题主要就是太小,大概也就4KB的样子,而且IE6只支持每个域名20个cookies,太少了.优势就是大家都支持,而且支持得还蛮好.cookie的内容主要包括:名字,值,过期时间, ...
- VPS L2TP配置
原文地址:https://raymii.org/s/tutorials/IPSEC_L2TP_vpn_with_Ubuntu_14.04.html 只要保证ipsec verify没错,基本都可以成功 ...
- Mac下使用brew搭建PHP7+nginx+mysql开发环境
http://blog.csdn.net/mysteryhaohao/article/details/52230634 HomeBrew brew的安装,直接上官网:http://brew.sh/ 一 ...
- C语言比较巧妙的字符串分割程序
在解析字符串时,能够解析的给出每个字符串的长度.内容.以及每个字符串的第一个字符的地址. short i; ; //切割之后的字符串的个数 ,ItemLen[],Idx[], ThCommandLen ...
- iOS---弹出提示对话框
一.就一个选项的对话框 代码块 #pragma mark - 封装弹出对话框方法 // 提示错误信息 - (void)showError:(NSString *)errorMsg { // 1.弹框提 ...
- BZOJ 1010 玩具装箱toy(斜率优化DP)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1010 题目大意:P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他 ...
- 更换163的yum源
1.利用oss的文件目录形式进行各地项目的汇总保存.上报在A目录,统计过的放到B目录. 2.各地服务器健康状态检查,每5分钟检查项目, 如果有异常,就发短信+邮件进行汇报.不管是不是有异常,都以 ...
- vmware + ubuntu 64 安装 node.js v8.9.3
第一次使用虚拟机,第一次使用linux系统,第一次安装使用node.js 虚拟机安装不用多说,安装好之后下载ubuntu 64位版本文件 在vm中点击“创建新的虚拟机”,选择下载的ubuntu iso ...
- js表单提交回调函数
在研究表单的时候发现一个有意思的东西——在表单提交的时候如何保证数据全部提交完毕才会清空? 我们常用的<input type="reset" value="重置&q ...