NYOJ 284 坦克大战 bfs + 优先队列
这类带权的边的图,直接广搜不行,要加上优先队列,这样得到的结果才是最优的,这样每次先找权值最小的,代码如下
#include <stdio.h>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
typedef struct Node{
int x, y;
int step;
friend bool operator < (const Node &a, const Node &b)
{
return a.step > b.step;
}
}Node;
const int MAX = + ;
int N, M;
int Map[MAX][MAX];
int next[][] = {{, }, {, }, {, -}, {-, }};
Node s, e;
bool match(Node node)//判断是否到达终点
{
if (node.x == e.x && node.y == e.y)
return true;
return false;
}
bool check(Node node)//判断这个点是否能走
{
if (node.x < || node.y < || node.x >= N || node.y >= M || Map[node.x][node.y] == )
return false;
return true;
}
int bfs()
{
priority_queue<Node> q;//优先队列
q.push(s);
Node p1, p2;
while (!q.empty())
{
p1 = q.top();
q.pop();
for (int i = ; i < ; i++)
{
p2.x = p1.x + next[i][];
p2.y = p1.y + next[i][];
p2.step = p1.step + Map[p2.x][p2.y];
if (match(p2))
{
return p2.step;
}
if (check(p2))
{
Map[p2.x][p2.y] = ;
Node v = p2;
q.push(v);
}
}
}
return -;
}
int main()
{
char ch;
while (~scanf("%d%d", &N, &M) && N + M)
{
for (int i = ; i < N; i++)
{
for (int j = ; j < M; j++)
{
cin >> ch;
if (ch == 'Y')//起点
{
s.x = i;
s.y = j;
s.step = ;
Map[i][j] = ;
}
else if (ch == 'T')//终点
{
e.x = i;
e.y = j;
e.step = ;
Map[i][j] = ;
}
else if (ch == 'B')//普通砖块,权值为2
{
Map[i][j] = ;
}
else if (ch == 'E')//空地,权值为1
{
Map[i][j] = ;
}
else
{
Map[i][j] = ;//为0的时候表示此点不可走
}
}
}
printf("%d\n", bfs());
} return ;
}
NYOJ 284 坦克大战 bfs + 优先队列的更多相关文章
- nyoj 284 坦克大战 (优先队列)
题目链接:http://acm.nyist.net/JudgeOnline/status.php?pid=284 特殊数据: 5 5 BBEEY EEERB SSERB SSERB SSETB 7 非 ...
- nyoj 284 坦克大战 简单搜索
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=284 题意:在一个给定图中,铁墙,河流不可走,砖墙走的话,多花费时间1,问从起点到终点至少 ...
- NYOJ 284 坦克大战 【BFS】+【优先队列】
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描写叙述 Many of us had played the game "Battle city" ...
- NYOJ 284 坦克大战 (广搜)
题目链接 描述 Many of us had played the game "Battle city" in our childhood, and some people (li ...
- nyoj 483 Nightmare【bfs+优先队列】
Nightmare 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Ignatius had a nightmare last night. He found him ...
- poj 2312 Battle City【bfs+优先队列】
Battle City Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7579 Accepted: 2544 Des ...
- nyoj-----284坦克大战(带权值的图搜索)
坦克大战 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" ...
- java制作简单的坦克大战
坦克大战是我们小时候玩红白机时代的经典游戏,看到有不少小伙伴都使用各种语言实现了一下,手痒痒,也使用java做的一个比较简单的坦克大战,主要面向于学过Java的人群,与学了一段时间的人,有利于面向对象 ...
- 3D坦克大战游戏源码
3D坦克大战游戏源码,该游戏是基于xcode 4.3,ios sdk 5.1开发.在xcode4.3.3上完美无报错.兼容ios4.3-ios6.0 ,一款ios平台上难得的3D坦克大战游戏源码,有2 ...
随机推荐
- cursor变换
cursor:hand 与 cursor:pointer 的效果是一样,都像手形光标.但用FireFox浏览时才注意到使用cursor:hand在FireFox.chorme里并被支持. cursor ...
- 2D简单图形相关算法罗列
因为平常在Qt开发过程中经常会与一些简单的2D几何图形打交道,因此学习和掌握一些基本的2D几何计算还是很有必要的,在这里罗列一些常用的基本情况,之后会适时补充. [1] 两点之间距离,根据两个点的差值 ...
- ActiveX控件资料
Visual Studio 2008(c#)开发ActiveX控件及制作CAB包总结(1) 分类: C#2011-05-27 15:50 403人阅读 评论(0) 收藏 举报 c#stringhook ...
- Yii2的相关学习记录,初始化Yii2(二)
前面已经将Yii2下载下来了,那我们就需要能实际的使用. 一.初始化,因为我都是在windows系统下,所以用cmd命令打开下载下来的Yii2的根目录.然后运行下面命令: init 会提示选择0为开发 ...
- thinkphp框架之模型(数据库查询)
1. 模型定义 文件名称必须是 表名+Model.class.php 例如:UserModel.class.php namespace Home\Model; //该模型类的命名空间 use Thin ...
- Apache 相关配置
1. HTTP缓存设置 ExpiresActive On #ExpiresDefault 设置全局缓存时间,将导致一些get请求不能连续执行 #ExpiresDefault "access ...
- 平稳退化,JS和HTML标记分离,极致性能的JavaScript图片库
index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- css()和 attr()的区别
- Could not launch process failed:security
是因为 用了 企业的开发者账号 安装的时候需要 在 手机的设置中 找到 描述文件 然后点击信任这个对应的证书 才能使用这个由企业号发布的应用.
- Hash查找法在Keil C51中的实现
摘要:散列(hash)是一种重要的存储方法,也是一种常见的查找方法.它是指在记录的存储位置和它的关键字之间建立一个确定的对应关系.本文以射频卡门禁控制器为例,说明用射频卡卡号作为关键字,用Hash查找 ...