POJ 3170 Knights of Ni (暴力,双向BFS)
题意:一个人要从2先走到4再走到3,计算最少路径。
析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意。其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4,
然后求最短路即可。
代码如下:
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
template<class T>T scan(T &x){
int f = 1; x = 0; char c;
while(c = getchar(), c < 48) if(c == '-') f = -1;
do x = x * 10 + (c^48);
while(c = getchar(), c > 47);
x *= f;
return x;
}
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-11;
const int maxn = 1000 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
struct node{
int x, y;
node(int xx = 0, int yy = 0) : x(xx), y(yy) { }
};
int n, m;
int a[maxn][maxn];
int vis1[maxn][maxn];
int vis2[maxn][maxn];
vector<node> v;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} void bfs(int r, int c, int vis[][maxn], bool ok){
queue<node> q;
q.push(node(r, c));
vis[r][c] = 0;
while(!q.empty()){
node u = q.front(); q.pop(); for(int i = 0; i < 4; ++i){
int x = u.x + dr[i];
int y = u.y + dc[i];
if(vis[x][y] != INF || !is_in(x, y) || a[x][y] == 1) continue;
if(!ok && a[x][y] == 3) continue;
vis[x][y] = vis[u.x][u.y] + 1;
q.push(node(x, y));
}
}
} int main(){
scanf("%d %d", &m, &n);
int sx, sy, ex, ey;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j){
scanf("%d", &a[i][j]);
if(a[i][j] == 4) v.push_back(node(i, j));
else if(a[i][j] == 2) sx = i, sy = j;
else if(a[i][j] == 3) ex = i, ey = j;
} for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
vis1[i][j] = vis2[i][j] = INF; bfs(sx, sy, vis1, false);
bfs(ex, ey, vis2, true);
int ans = INF;
for(int i = 0; i < v.size(); ++i){
ans = min(ans, vis1[v[i].x][v[i].y]+vis2[v[i].x][v[i].y]);
}
cout << ans << endl;
return 0;
}
POJ 3170 Knights of Ni (暴力,双向BFS)的更多相关文章
- 【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1671 从骑士bfs一次,然后从人bfs一次即可. #include <cstdio> # ...
- bzoj 1671: [Usaco2005 Dec]Knights of Ni 骑士【bfs】
bfs预处理出每个点s和t的距离d1和d2(无法到达标为inf),然后在若干灌木丛格子(x,y)里取min(d1[x][y]+d2[x][y]) /* 0:贝茜可以通过的空地 1:由于各种原因而不可通 ...
- 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...
- POJ 3126 Prime Path 解题报告(BFS & 双向BFS)
题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...
- POJ 1915-Knight Moves (单向BFS && 双向BFS 比)
主题链接:Knight Moves 题意:8个方向的 马跳式走法 ,已知起点 和终点,求最短路 研究了一下双向BFS,不是非常难,和普通的BFS一样.双向BFS只是是从 起点和终点同一时候開始搜索,可 ...
- Eight (HDU - 1043|POJ - 1077)(A* | 双向bfs+康拓展开)
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...
- [poj] 2549 Sumsets || 双向bfs
原题 在集合里找到a+b+c=d的最大的d. 显然枚举a,b,c不行,所以将式子移项为a+b=d-c,然后双向bfs,meet int the middle. #include<cstdio&g ...
- POJ——3126Prime Path(双向BFS+素数筛打表)
Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16272 Accepted: 9195 Descr ...
- UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)
题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...
随机推荐
- Servlet容器的启动(Tomcat为例)
一.容器简介 在tomcat容器等级中,context容器直接管理servlet在容器中的包装类Wrapper,所以Context容器如何运行将直接影响servlet的工作方式. tomcat容器模型 ...
- OpenGL ES之GLSurfaceView学习一:介绍
原文地址::http://120.132.134.205/cmdn/supesite/?uid-5358-action-viewspace-itemid-6527 GLSurfaceView是一个视图 ...
- 更改 input type 的值
需要实现的效果:一个输入框,当输入框未获得焦点的时候,value 值为 “密码”:当输入框失去焦点的时候,输入内容显示为”*****” <input name=”password” type=” ...
- background-size background-positon合并的写法
background:url('../../image/banner/banner1.jpg') #fff no-repeat 5px center/50px 50px; "/"前 ...
- ulimit 命令
用途:ulimit用于shell启动进程所占用的资源. 类别:shell内建命令 语法格式:ulimit [-acdfHlmnpsStvw] [size] 参数: -H 设置硬资源限制. -S 设置软 ...
- Hanoi塔问题
说明:河内之塔(Towers of Hanoi)是法国人M.Claus(Lucas)于1883年从泰国带至法国的,河内为越战时北越的首都,即现在的胡志明市:1883年法国数学家 Edouard Luc ...
- static_cast .xml
pre{ line-height:1; color:#1e1e1e; background-color:#d2d2d2; font-size:16px;}.sysFunc{color:#627cf6; ...
- Windows下安装GTK+
Step 1:到GTK官方网站上下载安装包.有32位的和64位,64位的有这句: Note that these 64-bit packages are experimental. Binary co ...
- WebGoat学习——跨站脚本攻击(Cross‐Site Scripting (XSS))
跨站脚本攻击(Cross‐Site Scripting (XSS)) XSS(Cross Site Script)跨站脚本攻击.是指攻击者向被攻击Web 页面里插入恶意html代码,当用户浏览该页之时 ...
- GC算法 垃圾收集器
GC算法 垃圾收集器 参考:http://www.cnblogs.com/ityouknow/p/5614961.html 概述 垃圾收集 Garbage Collection 通常被称为“GC”,它 ...