题意: 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的更多相关文章

  1. 【bfs+优先队列】POJ2312-Battle City

    [思路] 题目中的“可以沿直线发射打破砖墙”可能会迷惑到很多人,实际上可以等价理解为“通过砖墙的时间为2个单位”,这样题目就迎刃而解了.第一次碰到时可能不能很好把握,第二次基本就可以当作水题了. [错 ...

  2. B - Battle City bfs+优先队列

    来源poj2312 Many of us had played the game "Battle city" in our childhood, and some people ( ...

  3. POJ - 2312 Battle City BFS+优先队列

    Battle City Many of us had played the game "Battle city" in our childhood, and some people ...

  4. C - Battle City BFS+优先队列

    Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...

  5. PAT甲级1013-1014-1015

    题目:1013 Battle Over Cities 思路:城市数也就1000, 对于每次询问暴力bfs一下看一下有多少连通块就行了.答案就是联通块数减一. #include<stdio.h&g ...

  6. poj 2312 Battle City【bfs+优先队列】

      Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7579   Accepted: 2544 Des ...

  7. POJ 2312:Battle City(BFS)

    Battle City Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9885   Accepted: 3285 Descr ...

  8. Battle City 优先队列+bfs

    Many of us had played the game "Battle city" in our childhood, and some people (like me) e ...

  9. POJ 2435Navigating the City(bfs)

    题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘s’,’E’ 起点和终点.输出从起点到终点的的 最短路径(包括方向和沿该方向的经过的十字路口数) 分析:ans[i][ ...

随机推荐

  1. mysql分页性能

    - select * from userinfo limit 20000,10  # 数据越往后越慢 - 索引表中扫: select * from userinfo where id in (sele ...

  2. 【Data Structure & Algorithm】求1+2+…+n

    求1+2+-+n 题目:求1+2+-+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A ? B : C). 分析:此题没多少实际意义,因为 ...

  3. now code——处女座的期末复习

    题目描述 快要期末考试了,处女座现在有n门课程需要考试,每一门课程需要花ai小时进行复习,考试的起始时间为bi,处女座为了考试可以不吃饭不睡觉,处女座想知道他能否复习完所有的科目(即在每一门考试之前复 ...

  4. 使用js在html文档的任意位置输出内容

    <script type="text/javascript">document.write('这里是内容');</script>

  5. 玲珑学院1072 【DFS】

    蛤蛤,略蠢. priority_queue 自定义优先级 和排序是反的 struct node { int x,y; friend bool operator< (node a,node b) ...

  6. OPENGL_变换与坐标系

    参考:http://blog.csdn.net/kandyer/article/details/12449973 坐标系 世界坐标系:绝对坐标 物体坐标系:以物体自身为原点的坐标系 摄像机坐标系:以摄 ...

  7. Linux 软连接和硬连接(转)

    1.Linux链接概念Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接. [硬连接]硬连接指通过索引节点 ...

  8. SpringBoot | idea新建项目

    1.new ----> Spring Initializr 2.设置相应文件名 3.选择需要配置

  9. springMVC-上传图片

    SpringMVC文件上传与下载 上传图片 配置多媒体文件解析器 配置虚拟目录 在tomcat上配置图片虚拟目录,在tomcat下conf/server.xml中添加: <Context doc ...

  10. Elasticsearch之入门知识

    elasticsearch是一个高度可扩展得开源全文搜索和分析的引擎.可以快速.近实时的存储,搜索和分析大量数据.通常用作底层引擎技术,为具有复杂搜索功能和要求的程序提供支持. 用处: • 运行网上商 ...