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个方向之一移动),但每步结束之后任何两个鬼不能占 ...
随机推荐
- 微信开发小结-PHP
功能点: 1. 网页授权获得微信用户信息 用户在微信客户端中访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,进而实现业务逻辑. 注意点:Scope为snsapi_base 只能获 ...
- ios多手势事件
开发ios应用时我们经常用到多手势来处理事情,如给scrollView增加点击事件,scrollView不能响应view的touch事件,但有时候却要用到多手势事件,那么我们可以给这个scrollVi ...
- spring security的标签库
应用标签库:<%@ taglib prefix='security ' uri='http://www.springframework.org/security /tags' %> < ...
- Python [Leetcode 344]Reverse String
题目描述: Write a function that takes a string as input and returns the string reversed. Example:Given s ...
- 【转】Github轻松上手6-推荐follow的牛人和值得watch的repo
转自:http://blog.sina.com.cn/s/blog_4b55f6860100zzk5.html Github作为一个social coding 网站,其作用远远超过了一个简单的VCS( ...
- AJAX overrideMimeType作用
我们经常在AJAX代码中发现如下代码: if (http_request.overrideMimeType) { http_request.overrideMimeType("te ...
- String定义与方法
//5种构造方法 public void Con(){ String str = "sfaj"; String str1 = new String("sfajdf&quo ...
- ylb:exists(存在)的应用实例
ylbtech-SQL Server:exists(存在)的应用实例 SQL Server exists(存在)的应用实例. 1,exists(存在)的应用实例 返回顶部 -- =========== ...
- ylb:子查询(嵌套子查询)和子查询(相关子查询)
ylbtech-SQL Server:SQL Server-子查询(嵌套子查询)和子查询(相关子查询) SQL Server 子查询(嵌套子查询)和子查询(相关子查询). 1,ylb:1,子查询(嵌套 ...
- hibernate建表多对多建表
Student.java package cn.itcast.hiberate.sh.domain; import java.util.Set; public class Student { priv ...