poj2312Battle City BFS
题意: M行N列矩阵, 'Y'表示开始位置, 'T'表示目标位置, 从开始位置到目标位置至少需要走多少步,其中, 'S', 'R'表示不能走, 'B' 花费为2, 'E'花费为1.
思路:纯 BFS.
很久没写BFS, 刚写居然不知道从何下手...
#include <iostream>
#include <queue>
using namespace std; struct postion
{
int i, j;
postion operator+(postion p)
{
postion t;
t.i = this->i+ p.i;
t.j = this->j+ p.j;
return t;
}
}; postion dir[] = {,, -,, ,, ,-}; int step[][];
//bool used[305][305];
queue<postion>que;
char map[][];
postion start, target;
int M, N; int main()
{
int i, j;
while (cin>>M>>N && M+N)
{
for (i=; i<M; i++)
for (j=; j<N; j++)
{
cin>>map[i][j];
step[i][j] = INT_MAX;
if (map[i][j] == 'Y')
start.i = i, start.j = j;
else if (map[i][j] == 'T')
target.i = i, target.j = j;
}
//memset(used, false, sizeof(used));
//memset(step, 0, sizeof(step));
//used[start.i][start.j] = true;
step[start.i][start.j] = ;
while (!que.empty())que.pop();
que.push(start);
postion tmp, t;
int sp;
while (!que.empty())
{
tmp = que.front();
que.pop();
for (i=; i<; i++)
{
t = tmp + dir[i];
if (t.i< || t.i>=M || t.j< || t.j>=N || map[t.i][t.j] == 'R' || map[t.i][t.j] == 'S')
continue;
if (map[t.i][t.j] == 'E')
sp = ;
if (map[t.i][t.j] == 'B')
sp = ;
if (step[t.i][t.j] > step[tmp.i][tmp.j] + sp)
{
step[t.i][t.j] = step[tmp.i][tmp.j] + sp;
que.push(t);
}
}
}
if (step[target.i][target.j] == INT_MAX)
step[target.i][target.j] = -;
cout<<step[target.i][target.j]<<endl;
}
return ;
}
poj2312Battle City BFS的更多相关文章
- 【bfs+优先队列】POJ2312-Battle City
[思路] 题目中的“可以沿直线发射打破砖墙”可能会迷惑到很多人,实际上可以等价理解为“通过砖墙的时间为2个单位”,这样题目就迎刃而解了.第一次碰到时可能不能很好把握,第二次基本就可以当作水题了. [错 ...
- 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 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 ...
- PAT甲级1013-1014-1015
题目:1013 Battle Over Cities 思路:城市数也就1000, 对于每次询问暴力bfs一下看一下有多少连通块就行了.答案就是联通块数减一. #include<stdio.h&g ...
- poj 2312 Battle City【bfs+优先队列】
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 Des ...
- 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 ...
- POJ 2435Navigating the City(bfs)
题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘s’,’E’ 起点和终点.输出从起点到终点的的 最短路径(包括方向和沿该方向的经过的十字路口数) 分析:ans[i][ ...
随机推荐
- 1.SJ-SLAM-14
1.引言 SLAM:Simultaneous Localization and Mapping 同时定位与地图构建 搭载特定传感器的主体,在没有环境先验信息的情况下,于运动过程中建立环境的模型,同时估 ...
- Event事件的三个阶段
转自www.w3school.com.cn/htmldom/event_bubbles.asp 在 2 级 DOM标准中,事件传播分为三个阶段: 第一,捕获阶段.事件从 Document 对象沿着文档 ...
- Python decorate 函数
1. decorate 函数需要在 "@wrap" 之前定义, 否则会报错
- Codevs 1794 修剪花卉
1794 修剪花卉 题目描述 Description ZZ对数学饱有兴趣,并且是个勤奋好学的学生,总是在课后留在教室向老师请教一些问题. 一天他早晨骑车去上课,路上见到一个老伯正在修剪花花草草,顿 ...
- MySQL · 性能优化 · MySQL常见SQL错误用法
1. LIMIT 语句 分页查询是最常用的场景之一,但也通常也是最容易出问题的地方.比如对于下面简单的语句,一般DBA想到的办法是在type, name, create_time字段上加组合索引.这样 ...
- android studio ffmpeg简单使用 (cmake)
编译ffmpeg android studio 新建项目,勾选上 将编译好的libffmpeg.so库扔到src/main/jniLibs/armeabi下(主要这里我只编译了arm的ffmpeg的库 ...
- IP服务-5-网络时间协议
NTP版本3(RFC1305)允许IP主机向一个通用的时钟源同步它们的日期和时间. 从设计上来说,大多数路由器和交换机都使用NTP客户端模式,根据NTP服务器所提供的时间来调整自己的时钟.NTP定义了 ...
- 描述符__get__,__set__,__delete__和析构方法__del__
描述符__get__,__set__,__delete__ 1.描述符是什么:描述符本质就是一个新式类,在这个新式类中,至少实现了__get__(),__set__(),__delete__()中的一 ...
- Codeforces Round #532(Div. 2) C.NN and the Optical IIIusion
链接:https://codeforces.com/contest/1100/problem/C 题意: 一个圆球外面由其他圆球包裹,两两相连. 给出n,r. n为外面圆球数量,r为内部圆球半径. 求 ...
- jQuery val()方法及valHooks源码解读
val: function( value ) { var hooks, ret, isFunction, elem = this[0]; if ( !arguments.length ) {//无参数 ...