简单BFS。

 /* 2757 */
#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std; #define MAXN 1005 typedef struct node_t {
int k, t;
node_t() {}
node_t(int kk, int tt) {
k = kk; t = tt;
}
friend bool operator <(const node_t &a, const node_t &b) {
return a.t > b.t;
}
} node_t; int n, m;
int bx, by, ex, ey;
char map[MAXN][MAXN];
int visit[MAXN][MAXN];
int dir[][] = {
-,, -,, ,, ,,
,, ,-, ,-, -,-
}; inline bool check(int x, int y) {
return x< || x>=n || y<= || y>=m;
} int bfs() {
int xx, yy;
int x, y, t;
int i, j, k;
node_t nd;
priority_queue<node_t> Q; memset(visit, 0x3f, sizeof(visit));
visit[bx][by] = ;
nd.t = ;
nd.k = bx*+by;
Q.push(nd); while (!Q.empty()) {
nd = Q.top();
xx = nd.k/;
yy = nd.k%;
if (xx==ex && yy==ey)
return nd.t;
Q.pop();
for (i=; i<; ++i) {
x = xx + dir[i][];
y = yy + dir[i][];
if (check(x, y))
continue;
if (map[xx][yy] == i)
t = nd.t;
else
t = nd.t + ;
if (visit[x][y] > t) {
visit[x][y] = t;
k = *x+y;
Q.push(node_t(k, t));
}
}
} return ;
} int main() {
int i, j, k;
int t; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%d%d",&n,&m) != EOF) {
for(i=; i<n; ++i) {
scanf("%s", map[i]);
for (j=; j<m; ++j)
map[i][j] -= '';
}
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d",&bx,&by,&ex,&ey);
--bx; --by; --ex; --ey;
if (bx==ex && by==ey)
k = ;
else
k = bfs();
printf("%d\n", k);
}
} return ;
}

【HDOJ】2757 Ocean Currents的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  2. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  3. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  4. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  5. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  6. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  7. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  8. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  9. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

随机推荐

  1. iOS-UIControls介绍

    iOS-UIControls类介绍 UIControl的继承关系 UIControl是 UIKit中UISwitch(开关).UIButton(按钮).UISegmentedControl(分段控件) ...

  2. HTML5表单提交和PHP环境搭建

    HTML5表单提交相关内容和PHP环境搭建需要的软件(只备注) (2)举例介绍 (3)PHP环境搭建

  3. JQ 日期格式化

    将字符转换为日期格式: function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$ ...

  4. GoogleAuthenticator

    <?php /** * PHP Class for handling Google Authenticator 2-factor authentication * * @author Micha ...

  5. 最终版-perl工具解析数据库的报告文件0120

    ********************需要根据自己的实际环境修改哦**************************** ******************** 1. 收集awr报告样本   a ...

  6. Deep Learning 学习随记(五)Deep network 深度网络

    这一个多周忙别的事去了,忙完了,接着看讲义~ 这章讲的是深度网络(Deep Network).前面讲了自学习网络,通过稀疏自编码和一个logistic回归或者softmax回归连接,显然是3层的.而这 ...

  7. Visual Studio2010 安装msdn

    1.注册VS2010 断网(不要冒险)->运行Microsoft Visual Studio 2010->帮助->注册产品->YCFHQ-9DWCY-DKV88-T2TMH-G ...

  8. 【POJ2886】【线段树】Who Gets the Most Candies?

    Description N children are sitting in a circle to play a game. The children are numbered from 1 to N ...

  9. linux之uniq

    Linux命令uniq的作用是过滤重复部分显示文件内容,这个命令读取输入文件,并比较相邻的行.在正常情况下,第二个及以后更多个重复行将被删去,行 比较是根据所用字符集的排序序列进行的.该命令加工后的结 ...

  10. 浏览器兼容性判定写法格式(ie)

    条件注释判断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]> <!- ...