题意:一个人要从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)的更多相关文章

  1. 【BZOJ】1671: [Usaco2005 Dec]Knights of Ni 骑士(bfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1671 从骑士bfs一次,然后从人bfs一次即可. #include <cstdio> # ...

  2. 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:由于各种原因而不可通 ...

  3. 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS

    [Usaco2005 Dec]Knights of Ni 骑士 Description  贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...

  4. POJ 3126 Prime Path 解题报告(BFS & 双向BFS)

    题目大意:给定一个4位素数,一个目标4位素数.每次变换一位,保证变换后依然是素数,求变换到目标素数的最小步数. 解题报告:直接用最短路. 枚举1000-10000所有素数,如果素数A交换一位可以得到素 ...

  5. POJ 1915-Knight Moves (单向BFS &amp;&amp; 双向BFS 比)

    主题链接:Knight Moves 题意:8个方向的 马跳式走法 ,已知起点 和终点,求最短路 研究了一下双向BFS,不是非常难,和普通的BFS一样.双向BFS只是是从 起点和终点同一时候開始搜索,可 ...

  6. 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 ...

  7. [poj] 2549 Sumsets || 双向bfs

    原题 在集合里找到a+b+c=d的最大的d. 显然枚举a,b,c不行,所以将式子移项为a+b=d-c,然后双向bfs,meet int the middle. #include<cstdio&g ...

  8. POJ——3126Prime Path(双向BFS+素数筛打表)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16272   Accepted: 9195 Descr ...

  9. UVa 1601 || POJ 3523 The Morning after Halloween (BFS || 双向BFS && 降维 && 状压)

    题意 :w*h(w,h≤16)网格上有n(n≤3)个小写字母(代表鬼).要求把它们分别移动到对应的大写字母里.每步可以有多个鬼同时移动(均为往上下左右4个方向之一移动),但每步结束之后任何两个鬼不能占 ...

随机推荐

  1. POJ 3692 Kindergarten (补图是二分图的最大团问题)

    题意 幼稚园里有m个男孩和n个女孩(m.n范围都是[1,200]),男孩之间相互认识,女孩之间也相互认识,另外有部分男孩和女孩也认识.现在要举办一个活动,选取一些同学,要求所有选取的同学之间两两相互认 ...

  2. GrepCode

    /***************************************************************************** * GrepCode * 声明: * 最近 ...

  3. Scrum&Kanban在移动开发团队的实践 (二)

    Scrum&Kanban在移动开发团队的实践系列: Scrum&Kanban在移动开发团队的实践 (一) Scrum&Kanban在移动开发团队的实践 (二) 在第一篇分享文章 ...

  4. C的输入输出函数的基本用法

    printf输出函数: printf()函数是格式化输出函数, 一般用于向标准输出设备按规定格式输出信息. printf()函数的调用格式为: printf("<格式化字符串>& ...

  5. matplotlib 绘制柱状图的几个例子

    1 error bar #!/usr/bin/env python # a bar plot with errorbars import numpy as np import matplotlib.p ...

  6. 剑指offer—算法之位运算(二进制中1的个数)

    位运算: 左移:m<<n将m左移n位,左移后低位补充0: 右移:m>>n将m右移n位,右移后高位补充的是符号位,负数补充1,整数补充0.(正数的边界值为(1,ox7FFFFFF ...

  7. Golang 绘图基础 -绘制简单图形

    前一节讲的是 绘图到不同输出源,请看地址: http://www.cnblogs.com/ghj1976/p/3440856.html 上一节的例子效果是通过设置每一个点的的RGBA属性来实现的,这是 ...

  8. NewtonPrincipia_物体的运动_求向心力

    NewtonPrincipia_物体的运动_求向心力 让我们看一下十七世纪的被苹果砸中的艾萨克,是怎样推导出向心力公式的 在现在的观点看来,其中涉及到的很多没有符号表示的微分量.下面的内容只是叙述了推 ...

  9. python的编码

    python的编码 1.概述 讲述编码,那么就要涉及到几个方面,包括系统中如何来显示字符,文件中如何来保存字符. 1.1 系统环境 在系统中显示字符,那么就必须要考虑到系统中使用的编码格式. 在lin ...

  10. ansible控制windows的官方翻译

    Ansible控制windows 1.    Windows下如何工作 在ansible控制linux的时候,用的是ssh的方式,在windows中,使用的是power shell,在客户端机器上也是 ...