luogu P1979 华容道
solution
被假hash可了半天....sadQAQ
code
// luogu-judger-enable-o2
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
inline int read() {
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
while(c <= '9' && c >= '0')x = x * 10 + c - '0' ,c = getchar();
return x * f;
}
int n ,m,q;
const int maxn = 37;
int fs1[5]={-1,1,0,0},fs2[5]={0,0,-1,1};
int dis[maxn][maxn];
int map[maxn][maxn];
struct Node {
int x,y;
Node (int X = 0,int Y = 0) : x(X),y(Y) {};
};
int head[maxn * 100000 + maxn * 1000 + maxn * 10 + maxn * 2];
int num = 0;
struct Edge {
int u ,v, w,next;
}edge[maxn * maxn << 1];
int base = 10;
int a[17],Num;
inline void fz(int x) { while(x) {a[++Num] = x % 10;x /= 10;} }
inline int hash(int x,int y,int type) {
/*Num = 0;fz(type),fz(y),fz(x);
int ret = a[Num];
for(int i = Num - 1;i >= 1;-- i) ret = ret * base + a[i];*/
return x * 120 + y * 4 + type;
}
inline void add_edge(int u,int v,int dis) {
edge[++num].v = v;edge[num].u = u;edge[num].w = dis,edge[num].next = head[u];head[u] = num;
}
void bfs(int Blax,int Blay,int x,int y,int type) {
std::queue<Node>que;
memset(dis,0,sizeof dis);
dis[Blax][Blay] = 1;
que.push(Node(Blax,Blay));
while(!que.empty()) {
Node cur = que.front(); que.pop();
for(int i = 0;i < 4;++ i) {
int tx = cur.x + fs1[i],ty = cur.y + fs2[i];
if(map[tx][ty] && ! dis[tx][ty] && (tx !=x || ty != y) ) {
dis[tx][ty] = dis[cur.x][cur.y] + 1;
que.push(Node(tx,ty));
}
}
}
if(type == 11101001) return ;
for(int i = 0;i < 4;++ i) {
int tx = x + fs1[i],ty = y + fs2[i];
int a = hash(x,y,type),b = hash(x,y,i);
if(dis[tx][ty] && (tx != Blax || ty != Blay)) add_edge(a,b , dis[tx][ty] - 1);
}
add_edge(hash(x,y,type),hash(Blax,Blay,type ^ 1),1);
}
int Dis[maxn * 100000 + maxn * 10000 + maxn * 10 + maxn * 2];
bool vis[maxn * 100000 + maxn * 10000 + maxn * 10 + maxn * 2];
void spfa(int x,int y) {
memset(Dis,0x3f,sizeof Dis);
std::queue<int>que;
memset(vis,0,sizeof vis);
for(int tx,ty,i = 0;i < 4;++ i) {
tx = x + fs1[i],ty = y + fs2[i];
if(dis[tx][ty])
Dis[hash(x,y,i)] = dis[tx][ty] - 1 ,vis[hash(x,y,i)] = 1 ,que.push(hash(x,y,i));
//printf("%d %d %d %d\n",tx,ty,i,Dis[hash(tx,ty,i)]) ;
//printf("%d\n",dis[tx][ty]);
}
//printf("%d **********8\n",dis[hash(2,2,1)]) ;
while(!que.empty()) {
int u = que.front(); que.pop();
for(int i = head[u];i;i = edge[i].next) {
int v = edge[i].v;
if(Dis[v] > Dis[u] + edge[i].w) {
Dis[v] = Dis[u] + edge[i].w; if(!vis[v]) que.push(v),vis[v] = 1;
}
}
vis[u] = 0;
}
}
int main() {
//printf("%d\n",hash(233,233,0));
//printf("%d\n",0 % 10);
n = read(),m = read(),q = read();
for(int i = 1;i <= n;++ i)
for(int j = 1;j <= m;++ j)
map[i][j] = read();
for(int i = 1;i <= n;++ i)
for(int j = 1;j <= m;++ j)
if(map[i][j]) {
if(map[i - 1][j]) bfs(i - 1,j,i,j,0);
if(map[i + 1][j]) bfs(i + 1,j,i,j,1);
if(map[i][j - 1]) bfs(i,j - 1,i,j,2);
if(map[i][j + 1]) bfs(i,j + 1,i,j,3);
}
for(int a,b,c,d,e,f;q--;) {
a = read(),b = read(),c = read(),d = read(); e = read();f = read();
if(c == e && d == f){puts("0");continue;}
bfs(a,b,c,d,11101001);
spfa(c,d); int ans = 0x7fffffff;
for(int i = 0;i < 4;++ i) ans = std::min(ans,Dis[hash(e,f,i)]);//,printf("%d ** %d ** %d ** \n",e,f,Dis[hash(e,f,i)]);
printf("%d\n",ans == 0x3f3f3f3f ? -1 : ans);
}
return 0;
}
luogu P1979 华容道的更多相关文章
- Luogu P1979 华容道(bfs+最短路)
P1979 华容道 题意 题目描述 小B最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成, 最少需要多少时间. ...
- 洛谷 P1979 华容道 解题报告
P1979 华容道 题目描述 小\(B\)最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时 ...
- 洛谷P1979 华容道(70分 暴力)
P1979 华容道 题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少 ...
- luogu P1979 [NOIP2013] 华容道
传送门 这道题中,棋子的移动是要移动到空格上去,所以空格要在棋子旁边才能移动棋子;而棋子移动的方向由空格决定 所以我们可以记三维状态\(di_{i,j,k}\),表示状态为棋子在\((i,j)\),空 ...
- P1979华容道(神仙题)
题目描述 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 B 玩的华容道 ...
- [NOIP2013] 提高组 洛谷P1979 华容道
题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...
- P1979 华容道 spfa题解
题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...
- 洛谷P1979 华容道
神の契约 题目大意:自己看去... 题解:做了一下午...本蒟蒻立志要写全网最详细的题解.╭(╯^╰)╮ begin.... 暴力70分.可以让空格子到处乱走,只要某个状态的指定格子到目标格子,那么此 ...
- P1979 华容道
题意:$n*m$棋盘上$n*m-1$颗棋子,有且只有一个格子为空白格子,每个棋子大小$1*1$ 有些棋子可以移动,而有些棋子固定,任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上 ...
随机推荐
- codevs1066&&noip引水入城
这道题 解决第一问 用灌水法 枚举第一行的每一个点 查找是否最后一行的每一个点是否都能灌到水 第二问 用反灌水发 枚举最后一行的每一个点 解决第一行每一个点所能覆盖的左右端点 可以证明每个点所能覆盖的 ...
- 微信公众号支付开发全过程(Java 版)
一.微信官方文档微信支付开发流程(公众号支付) 首先我们到微信支付的官方文档的开发步骤部分查看一下需要的设置. [图片上传失败...(image-5eb825-1531014079742)] 因为微信 ...
- hdu 1150 Machine Schedule(二分匹配,简单匈牙利算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1150 Machine Schedule Time Limit: 2000/1000 MS (Java/ ...
- Java的9种基本数据类型的大小,以及他们的封装类
由于java程序是运行在虚拟机之上的,所以java的基本数据类型的大小是确定的,不会随着操作系统的位数的改变而改变. 在计算机中,存储的是0,1,0,1这样的二进制位,表示为bit,1Byte = 8 ...
- macaca安装失败的解决办法!
https://github.com/macacajs/macaca-android https://www.jianshu.com/p/76a5be6c1036
- 移动端测试===Android内存管理: 理解App的PSS
Android内存管理: 理解App的PSS 原文链接:http://www.littleeye.co/blog/2013/06/11/android-memory-management-unders ...
- linux内核网络接收数据流程图【转】
转自:http://blog.chinaunix.net/uid-23069658-id-3141409.html 4.3 数据接收流程图 各层主要函数以及位置功能说明: 1)s ...
- 获取并编译最新的Notepad++源码
获取并编译最新的Notepad++源码 http://blog.csdn.net/u012814856/article/details/68947310 Notepad++源码编译及其分析 http: ...
- 【jzoj6.24模拟B】
这场真是无聊,搬远古原题…… xjb做了做,(居然没AK真是身败名裂) A.教主的花园 答案明显具有可二分性,二分答案判定下就行. #include<bits/stdc++.h> #def ...
- HDU 6118 度度熊的交易计划 最大费用可行流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6118 题意:中文题 分析: 最小费用最大流,首先建立源点 s ,与超级汇点 t .因为生产一个商品需要 ...