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][ ...
随机推荐
- Flutter实战视频-移动电商-54.购物车_商品列表子项布局
54.购物车_商品列表子项布局 子项做成一个单独的页面 新建cartItem.dart文件 新建cart_page文件夹,在里面新建cart_item.dart页面, 页面名字叫做CartItem 定 ...
- C# 选择文件路径,选择文件
// 选择文件: private string SelectPath() { string path = string.Empty; var openFileDialog = new Microsof ...
- lightoj1145 【DP优化求方案】
题意: 有一个k面的骰子,然后问你n个骰子朝上的面数字之和=s的方案: 思路: dp[i][j] 代表 前 i 个骰子组成 j 有多少种方案: 显然 dp[i][j] = dp[i - 1][j - ...
- Codeforces 67A【模拟】
题意: 给一个字符串代表相邻学生的比较,L代表左边多,R表示右边多,=表示左右相等. 保证每个人拿糖>=1,在分糖最少的情况下,输出每个学生所分得的糖. 思路: 模拟一下,第一个人一开始拿1个, ...
- [Xcode 实际操作]九、实用进阶-(9)陀螺仪设备的使用
目录:[Swift]Xcode实际操作 本文将演示陀螺仪设备的使用. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit //导入需要用到的C ...
- assembly x86(nasm)子程序1
T: 将BUF开始的10个单元中的二进制数转换成两位十六进制数的ASCII码,在屏幕上显示出来.要求码型转换通过子程序HEXAC实现,在转换过程中,通过子程序DISP实现显示. 思路: Main主调程 ...
- Response.Redirect 产生的“正在中止线程”错误
Response.Redirect 产生的“正在中止线程”错误 今天在开发调试过程中,出现在一个 "正在中止线程"异常信息. 调用Response.Redirect()方法产生的, ...
- JS实现 类的 1.判断 2.添加 3.删除 4切换
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- linux mysql乱码问题
mysql,发现都是乱码,一堆问号,如下图: 查看mysql编码 需要修改mysql编码,打开/etc/my.cnf 文件 在下边添加如下行 [client] default_character_se ...
- springMVC-RESTful支持
RESTful支持 什么是restful? Restful就是一个资源定位及资源操作的风格.不是标准也不是协议,只是一种风格,是对http协议的诠释. 资源定位:互联网所有的事物都是资源,要求url中 ...