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$ 有些棋子可以移动,而有些棋子固定,任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上 ...
随机推荐
- bootstrap框架的搭建
bootstrap框架 Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快 ...
- poj 2762 tarjan缩点+拓扑序
2013-09-08 10:00 var m, n :longint; t :longint; f, last :..] of longint; pre, other :..] of longint; ...
- Spring Boot提供的特性
一.导览 本文主要按以下模块介绍spring Boot(1.3.6.RELEASE)提供的特性. SpringApplication类 外部化配置 Profiles 日志 开发WEB应用 Securi ...
- python中的binascii模块
binascii模块拿来干嘛的? 答:进制转换xxoo #!/usr/bin/env python # encoding:utf-8 # by i3ekr import binascii s = &q ...
- DirectX介绍(转)
原文转自 https://baike.baidu.com/item/Direct3D/910353
- SAE如何使用Git
了解Git及远程git仓库 请先看博文<Git入门及上传项目到github中>,弄懂了之后我相信我下面说的就相当于废话了. SAE的git远程仓库就相当于github. 向SAE的远程仓库 ...
- 使用Webpack搭建Vue项目
前提: 1. 借助Node.js环境里的npm来安装, 2. 设置好npm镜像, (比如淘宝的npm镜像:输入 引用 npm install -g cnpm –registry=https://r ...
- C++ 模板的用法
C++中的高阶手法就会用到泛型编程,主要有函数模板, 在程序中使用模板的好处就是在定义时不需要指定具体的参数类型,而在使用时确可以匹配其它任意类型, 定义格式如下 template <class ...
- ACE_Reactor类
.ACE反应器框架简介 反应器(Reactor):用于事件多路分离和分派的体系结构模式 对一个文件描述符指定的文件或设备的操作, 有两种工作方式: 阻塞与非阻塞. 在设计服务端程序时,如果采用阻塞模式 ...
- Jquery屏蔽浏览器的F1-F12快捷键,在IE,GOOGLE下测试均无问题
在网上找了找,很多都是js实现的,东找西找,再加上自己的想法也勉强的完成了,直接看代码 <script type="text/javascript" src="Sc ...