题目链接:http://poj.org/problem?id=2312

挺有趣的一道题目,然而很容易WA,我就WA了一次,虽然我Debug的时候已经知道哪里出问题了,就是比如说我搜到B和E时,从B搜第三个点,B左边的E就被搜了,step为3,然而其实他是step为2,

这里的处理方法很是巧妙,可以从B出发时,把B换成E,step+1,形成一个新的结点加入到队列中去.

之后,和杰哥交流了这下题,我对这个BFS又有了新的一点认识,BFS时,每次搜索的点,都要是同一级别的点,想这里B,和E就不是同一级别的点,所以将这个B,分成两部,注意B搜完后不要标记,B还没有访问完。

#include <stdio.h>
#include <string.h>
#include <queue> using namespace std; int to[][]= {{-,},{,},{,-},{,}}; struct Point
{
int r,c;
int step;
};
int r,c;
char maps[][];
bool vis[][]; bool judge (int rx,int cx)
{
if(rx<||rx>=r||cx<||cx>=c||maps[rx][cx]=='S'||maps[rx][cx]=='R'||vis[rx][cx])
return true;
else return false;
} int main()
{
int sr,sc;
while(scanf("%d%d",&r,&c),r)
{
memset(vis,false,sizeof(vis)); for(int i=; i<r; i++)
{
scanf("%s",maps[i]);
for(int j=; j<c; j++)
{
if(maps[i][j]=='Y')
{
sr = i;
sc = j;
}
}
}
int ans = -; queue<Point> Q;
Point a,next;
a.r = sr;
a.c = sc;
a.step = ;
vis[a.r][a.c] = true;
Q.push(a);
while(!Q.empty())
{
a = Q.front();
Q.pop(); if(maps[a.r][a.c]=='T')
{
ans = a.step;
break;
}
if(maps[a.r][a.c]=='B')
{
a.step ++;
maps[a.r][a.c] = 'E';
Q.push(a);
continue;
} for(int i=; i<; i++)
{
next.r = a.r + to[i][];
next.c = a.c + to[i][];
if(judge(next.r,next.c))
continue;
vis[next.r][next.c] = true;
next.step = a.step +;
Q.push(next); } }
printf("%d\n",ans);
}
return ;
}

Poj(2312),坦克大战,BFS的变形的更多相关文章

  1. NYOJ 284 坦克大战 bfs + 优先队列

    这类带权的边的图,直接广搜不行,要加上优先队列,这样得到的结果才是最优的,这样每次先找权值最小的,代码如下 #include <stdio.h> #include <iostream ...

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

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

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

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

  4. NYOJ 284 坦克大战 【BFS】+【优先队列】

    坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 Many of us had played the game "Battle city" ...

  5. nyoj-----284坦克大战(带权值的图搜索)

    坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 Many of us had played the game "Battle city" ...

  6. nyoj 284 坦克大战 简单搜索

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...

  7. 3D坦克大战游戏源码

    3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...

  8. 【blade04】用面向对象的方法写javascript坦克大战

    前言 javascript与程序的语言比如C#或者java不一样,他并没有“类”的概念,虽然最新的ECMAScript提出了Class的概念,我们却没有怎么用 就单以C#与Java来说,要到真正理解面 ...

  9. 3D坦克大战游戏iOS源码

    3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...

随机推荐

  1. Eclipse + Git + 码云

    1. 进入码云个人首页 点击自己的名称即可 2. 添加一个项目 3. 在Eclipse中创建一个本地Git Eclipse不建议git目录创建在项目的目录下,因此另选一个目录作为本地Git目录 选择一 ...

  2. Jmeter录制pc脚本

    1.打开jmeter后可以看到左边窗口有个“测试计划”和“工作台”,右键“测试计划”,添加 Threads(Users) →线程组,再右键 线程组→添加 配置元件→Http请求默认值 Http请求默认 ...

  3. 查看Python支持的.whl文件版本

    AMD64 import pip._internal print(pip._internal.pep425tags.get_supported()) WIN32 import pip print(pi ...

  4. 安装Python + Selenium

    1.Python下载与安装‍ 先去Python官网下载安装包:http://www.python.org/ 下载后按步骤安装(最好不要安装到系统盘) 安装好后将安装路径(Python和Scripts) ...

  5. python 矢量化的字符串

  6. Redis Intro - Skiplist

    http://zhangtielei.com/posts/blog-redis-skiplist.html https://juejin.im/entry/59197a390ce4630069fbcf ...

  7. python3.4中自定义wsgi函数,make_server函数报错问题

    别的不多说,先上代码 #coding:utf-8 from wsgiref.simple_server import make_server def RunServer(environ, start_ ...

  8. mysql 将null转代为0(转)

    1.如果为空返回0 select ifnull(null,0) 2.如果为空返回0,否则返回1 select if(isnull(col),0,1) as col. MYSQL 中的IFNULL函数 ...

  9. 随机练习:C#实现维吉尼亚加密与解密(解密前提为已知密匙)

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. 赶集网mysql开发36条军规

    写在前面的话: 总是在灾难发生后,才想起容灾的重要性: 总是在吃过亏后,才记得曾经有人提醒过. (一)核心军规 (1)不在数据库做运算    cpu计算务必移至业务层: (2)控制单表数据量    i ...