题意: 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. Flutter实战视频-移动电商-37.路由_Fluro引入和商品详细页建立

    37.路由_Fluro引入和商品详细页建立 https://github.com/theyakka/fluro pages/details_page.dart新建页面 使用路由 先添加路由插件的引用 ...

  2. Identity Server 4 原理和实战(完结)_Implicit Flow 实例

    oidc-client.js貌似是IdentityServer4的团队开发的 服务端的设置 在服务端新增一个Client 之后需要在angular客户端页建两个页面,对应这两个url才行 登出之后要跳 ...

  3. 内存、缓存、cpu之间的关系

    一.缓存和内存 许多人认为,“缓存”是内存的一部分 许多技术文章都是这样教授的 但是还是有很多人不知道缓存在什么地方,缓存是做什么用的 其实,缓存是CPU的一部分,它存在于CPU中 CPU存取数据的速 ...

  4. csvreader 来操作csv文件

    http://www.cnitblog.com/rd416/archive/2010/07/08/47248.html

  5. 剑指offer面试题:输入某二叉树的前序遍历和中序遍历,输出后序遍历

    二叉树的先序,中序,后序如何遍历,不在此多说了.直接看题目描述吧(题目摘自九度oj剑指offer面试题6): 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结 ...

  6. MS SQL server对象类型type

    执行下面代码,将获取ms sql server对象类型以及其说明 IF OBJECT_ID('tempdb.dbo.#type') IS NOT NULL DROP TABLE #type CREAT ...

  7. Bootstrap Table 从新InsertRow 刷新表格 数据的问题处理方案

    1.第一步获取数据源 var rows = { //要插入的数据,这里要和table列名一致SkuCode: data.rows[i].SkuCode,BarCode: data.rows[i].Ba ...

  8. Verify the Developer App certificate for youraccount is trusted on your device

    运行时报错-Verify the Developer App certificate for youraccountis   trusted on your device. Open Settings ...

  9. salt命令

    salt-key -L list在master上所有收到的公钥连接请求 -A accept所有pending的请求. -D 删除所有 在minion上启动服务后,几十秒后会在/etc/salt/pki ...

  10. djangoAdmin组件

    定制后台页面功能 from django.contrib import admin from app import models # Register your models here. class ...