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$ 有些棋子可以移动,而有些棋子固定,任何与空白的格子相邻(有公共的边)的格子上的棋子都可以移动到空白格子上 ...
随机推荐
- Java 关于微信公众号支付总结附代码
很多朋友第一次做微信支付的时候都有蒙,但当你完整的做一次就会发现其实并没有那么难 业务流程和应用场景官网有详细的说明:https://pay.weixin.qq.com/wiki/doc/api/js ...
- 64_c2
coin-or-Bcp-1.4.3-3.fc26.i686.rpm 22-May-2017 21:07 250866 coin-or-Bcp-1.4.3-3.fc26.x86_64.rpm 22-Ma ...
- ZZ:Solaris 10 软件包分析
ZZ:Solaris 10 软件包分析 http://blog.chinaunix.net/uid-22759617-id-276756.html # Last updated: 2006-02-14 ...
- 004 ConcurrentHashMap原理
下面这部分内容转载自: http://www.haogongju.net/art/2350374 JDK5中添加了新的concurrent包,相对同步容器而言,并发容器通过一些机制改进了并发性能.因为 ...
- mybatis-plus的学习
1.mybatisplus 提供了比较齐全的crud即增删改查,不需要在mapper.xml里写sql可以直接调用 原文链接:http://blog.csdn.net/u014519194/artic ...
- IE11中实现颜色渐变
background: -ms-linear-gradient(left,#daa23e,#ad7f27); 下面是css3中颜色渐变对各个浏览器的写法:background: -webkit-lin ...
- leetcode 之Reverse Linked List II(15)
这题用需要非常细心,用头插法移动需要考虑先移动哪个,只需三个指针即可. ListNode *reverseList(ListNode *head, int m, int n) { ListNode d ...
- SRM 638 Div.2
250 给一个字符串 要求从一种形式换成另一形式 class NamingConvention{ private: int a, b, c; public: int d; string toCamel ...
- sql server 2008 R2连接失败 错误:18456
这种问题的解决方法: 第一步:以windows验证模式进入数据库管理器. 第二步:在对新资源管理器中右击实例名称选择属性,弹出服务器属性对话框,我们在左侧栏选择[安全性]选项卡,选中”SQL Serv ...
- virtualenv--创建虚拟环境
一.virtualenv 优点 1.使用不同应用开发环境独立 2.环境升级不影响其他应用,也不会影响全局的python 环境二.安装 pip install virtualenv 三.使用virtua ...