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 华容道的更多相关文章

  1. Luogu P1979 华容道(bfs+最短路)

    P1979 华容道 题意 题目描述 小B最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成, 最少需要多少时间. ...

  2. 洛谷 P1979 华容道 解题报告

    P1979 华容道 题目描述 小\(B\)最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时 ...

  3. 洛谷P1979 华容道(70分 暴力)

    P1979 华容道 题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少 ...

  4. luogu P1979 [NOIP2013] 华容道

    传送门 这道题中,棋子的移动是要移动到空格上去,所以空格要在棋子旁边才能移动棋子;而棋子移动的方向由空格决定 所以我们可以记三维状态\(di_{i,j,k}\),表示状态为棋子在\((i,j)\),空 ...

  5. P1979华容道(神仙题)

    题目描述 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 B 玩的华容道 ...

  6. [NOIP2013] 提高组 洛谷P1979 华容道

    题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...

  7. P1979 华容道 spfa题解

    题目描述 [问题描述] 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面, 华容道是否根本就无法完成,如果能完成, 最少需要多少时间. 小 ...

  8. 洛谷P1979 华容道

    神の契约 题目大意:自己看去... 题解:做了一下午...本蒟蒻立志要写全网最详细的题解.╭(╯^╰)╮ begin.... 暴力70分.可以让空格子到处乱走,只要某个状态的指定格子到目标格子,那么此 ...

  9. P1979 华容道

    题意:$n*m$棋盘上$n*m-1$颗棋子,有且只有一个格子为空白格子,每个棋子大小$1*1$ 有些棋子可以移动,而有些棋子固定,任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上 ...

随机推荐

  1. 【EverydaySport】健身笔记——背部训练

    背部训练大致可以分为两种. 1 下拉式动作 躯干纵向上下位移的动作 典型代表 这样的下拉类动作 针对的是背阔肌 也就是两边像翅膀一样的部分 2 垂直于躯干的方向作用 向内拉 主要针对的是,背部的中部 ...

  2. 金融分析二:Matplotlib模板

    Matplotlib:绘图和可视化 Matplotlib是一个强大的Python绘图和数据可视化的工具包. 安装方法:pip install matplotlib 引用方法:import matplo ...

  3. C++之编译器与链接器工作原理

    原文来自:http://blog.sina.com.cn/s/blog_5f8817250100i3oz.html 这里并没不是讨论大学课程中所学的<编译原理>,只是写一些我自己对C++编 ...

  4. 解决linux下终端无法输入的假死问题

    有时在linux下shell终端中,会突然出现终端应用卡死,无法接受键盘输入, 但是其它分屏, 系统都是正常的.这本来是一个终端的很老的功能, 叫软件流控制(XON/XOFF flow control ...

  5. python基础===装饰器@property 的扩展

    以下来自Python 3.6.0 Document: class property(fget=None, fset=None, fdel=None, doc=None) Return a proper ...

  6. Centos7 IP地址配置方法

    1.编辑 ifcfg-eth0 文件 # vim /etc/sysconfig/network-scripts/ifcfg-eth0 2.修改如下内容 BOOTPROTO=”static” #dhcp ...

  7. FineReport——JS二次开发(分页预览)

    BS访问某个cpt模板,报表servlet将会将cpt文件解析成对应的html,报表内容最终转换为一个table,位于id=content-container的div中. 在模板和html页面中,他们 ...

  8. JS实现判断滚动条滚到页面底部并执行事件的方法

    需要了解三个dom元素,分别是:clientHeight.offsetHeight.scrollTop. clientHeight:这个元素的高度,占用整个空间的高度,所以,如果一个div有滚动条,那 ...

  9. jmeter压力测试案例实战

    1.  测试目标地址:http://www.cnblogs.com/ 2.  1秒内有100个用户同时访问,看性能如何 3.  步骤 线程组.http请求.查看结果树.聚合报告 添加http请求如下: ...

  10. Leetcode 之Binary Tree Inorder Traversal(43)

    树的中序遍历.先不断压入左结点至末尾,再访问,再压入右结点.注意和先序遍历的比较 vector<int> inorderTraversal(TreeNode *root) { vector ...