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][ ...
随机推荐
- ASP.NET Core编程实现基本身份认证
概览 在HTTP中,基本认证(Basic access authentication,简称BA认证)是一种用来允许网页浏览器或其他客户端程序在请求资源时提供用户名和口令形式的身份凭证的一种登录验证方式 ...
- 洛谷 - P1072 Hankson - 的趣味题 - 质因数分解
https://www.luogu.org/problemnew/show/P1072 一开始看了一看居然还想放弃了的. 把 \(x,a_0,a_1,b_0,b_1\) 质因数分解. 例如 \(x=p ...
- ios NSFileManager创建目录、文件
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *str1 = NSHomeDirectory(); _fi ...
- POJ3696【欧拉函数+欧拉定理】
题意: 求最小T,满足L的倍数且都由8组成,求长度: 思路: 很强势的福利:点 图片拿出去食用更优 //#include<bits/stdc++.h> #include<cstdio ...
- laravel 遍历循环
@foreach($xxx as $k=>$y) {{$y->id}} @endforeach
- bzoj 2039: [2009国家集训队]employ人员雇佣【最小割】
一开始在https://www.cnblogs.com/lokiii/p/10770919.html基础上连(i,j,b[i][j])建了个极丑的图T掉了--把dinic换成isap勉强能卡过 首先因 ...
- bzoj 1901: Zju2112 Dynamic Rankings【整体二分+树状数组||主席树+树状数组】
整体二分: 对于每一个修改操作,标记为1,并且加一个标记为-1的这个位置原来值,并且对于a数列每个点都当成修改操作 然后整体二分,扫当前操作区间lr,把在值域区间标记为1和-1的操作都在树状数组对应位 ...
- [Xcode 实际操作]八、网络与多线程-(11)使用同步Post方式查询IP地址信息
目录:[Swift]Xcode实际操作 本文将演示如何通过Post请求,同步获取IP地址信息. 一旦发送同步请求,程序将停止用户交互,直至服务器返回数据. 在项目导航区,打开视图控制器的代码文件[Vi ...
- C 语言实例 - 删除字符串中的特殊字符
C 语言实例 - 删除字符串中的特殊字符 C 语言实例 C 语言实例 删除字符串中的除字母外的字符. 实例 #include<stdio.h> int main() { ]; int i, ...
- web.xml中一个filter配置多个url-pattern
需要在filter标签后添加多个filter-mapping标签,一个url-pattern就对应一个filter-mapping标签,不能直接把多个url-pattern配置到同一个filter-m ...