【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 ...
随机推荐
- configure JDBCRealm JAAS for mysql and tomcat 7 with form based authentication--reference
Hello all, In this tutorial we are going to configure JDBCRealm JAAS for tomcat 7 and mysql database ...
- Java基础知识强化之集合框架笔记23:ArrayList的实现原理
1. ArrayList的实现原理: 这个可以直接参考网友的博客:http://www.cnblogs.com/ITtangtang/p/3948555.html
- springmvc xml 空模板
<?xml version="1.0" encoding="UTF-8"?><!-- Bean头部 --><beans xmlns ...
- Asp.net主题(theme)和皮肤(skin)的使用
asp.net 的服务器端控件提供了多种样式的设计,如果对每个控件都单独设置,是比较繁琐的事情,所以微软也提供了针对这些服务器端控件的样式管理,其实也可以通过 css来控制部分服务器端控件的样式,比如 ...
- 枚举,Enum,常规使用demo记录
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mv ...
- struts2中方法拦截器(Interceptor)的中的excludeMethods与includeMethods的理解
http://www.cnblogs.com/langtianya/archive/2013/04/10/3012205.html
- Missing artifact com.sun:tools:jar:1.5.0
http://java-suddy.iteye.com/blog/1821871(解决了我的问题)
- C#获取本机IP搜集整理7种方法
今天打算试着写个小聊天程序,但是要用到获取本机IP,以前从没用过.摆渡百度了一会儿,出于贪心,想把各种获取本机IP的方法给找出来.摆渡+测试了几个小时,于是有了下面的成果,有点小累,但看到这些成果,也 ...
- 基于nodejs的消息中心
参考:http://t42dw.iteye.com/blog/1767013
- SGU 249.Matrix(Gray码)
题意: 用0到2^(n+m-1)这2^(n+m-1)个数填在一个2^n*2^m的矩阵里,使得所有相邻的数的二进制表示只有一位不同. Solution: Gray码.对于第i行第j列的数,由i的Gray ...