【HDOJ】2757 Ocean Currents
简单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的更多相关文章
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
- 【HDOJ】【1512】Monkey King
数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...
随机推荐
- 用 Qt 中的 QDomDocument类 处理 XML 文件(上)
我们可以看到,如果所要读取的XML文件不是很大,采用DOM读取方法还是很便捷的,由于我用的也是DOM树读取的方法,所以,本文所介绍的也主要是基于DOM的方法读取. 根据常用的操作,我简单的把对XM ...
- linux 管道--转
linux 管道 管道是Linux中很重要的一种通信方式,是把一个程序的输出直接连接到另一个程序的输入,常说的管道多是指无名管道,无名管道只能用于具有亲缘关系的进程之间,这是它与有名管道的最大区别. ...
- 【网络通信】服务器端Socket监听80端口,建立连接传输数据时也是使用的80端口么?
1. 服务器端Socket监听80端口,建立连接传输数据时也是使用的80端口么? 答:对.建立连接时服务器会分配一个新的Socket,但是用的源端口号还是80端口.套接字是由协议类型.源IP.目的IP ...
- Customizing the Test Runner
There are several situations where you want to customize Robolectric's test runner to perform some o ...
- Xcode6 viewDidLoad 中View的subviews 为空
Xcode 6 中勾选using size Classes 方法,在ViewDidLoad 方法中调用outlet 的一个myView.subviews 会为空,而myView 不会为空. stor ...
- Http,Https (SSL)的Url绝对路径,相对路径解决方案Security Switch 4.2 英文帮助文档 分类: ASP.NET 2014-10-28 10:50 147人阅读 评论(1) 收藏
Security Switch 4.2 =================== Security Switch enables various ASP.NET applications to auto ...
- C#语法糖之第一篇:自动属性&隐式类型
今天给大家分享一下C#语法糖的简单的两个知识点吧. 自动属性:在 C# 4.0 和更高版本中,当属性的访问器中不需要其他逻辑时,自动实现的属性可使属性声明更加简洁. 客户端代码还可通过这些属性创建对象 ...
- HBuilder+移动APP开发实例
mui: 官网:http://dcloudio.github.io/mui/ 说明:一般要把官网内容通读一遍,这是开发的基础 开始 1.新建项目 在首页点击新建移动App,如下: 或者在项目管理器内右 ...
- HTML5+移动APP(2)
原理: html 页面负责内容: ui 负责页面样式: js 负责调用原生app方法. html5: html5这部分负责页面,也就是app中你看到的东西,大概的架构和内容 ui: mui 介绍:和H ...
- java.lang.reflection打印一个类的全部信息
package com.ljy.chapter5; import java.lang.reflect.Constructor; import java.lang.reflect.Field; impo ...