#include<cstdio>
#include<cstring>
#define MIN(A,B) (A)<(B)?(A):(B)
using namespace std;
const int dx[]={,-,,},dy[]={,,,-},INF=0x3f3f3f3f;
int puz[][],n,m,step[][][][];
int bfs(int sx,int sy,int tx,int ty)
{
struct node
{
int x,y;
}q[],*qf=q,*qb=q+;
int d[][];
if(!puz[sx][sy]||!puz[tx][ty])return INF;
if(sx==tx&&sy==ty)return ;
memset(d,0x3f,sizeof(d));
qf->x=sx;
qf->y=sy;
d[sx][sy]=;
while(qf!=qb)
{
for(int i=;i<;i++)
{
qb->x=qf->x+dx[i];
qb->y=qf->y+dy[i];
if(puz[qb->x][qb->y]&&d[qb->x][qb->y]==INF)
{
d[qb->x][qb->y]=d[qf->x][qf->y]+;
if(qb->x==tx&&qb->y==ty)return d[tx][ty];
qb++;
}
}
qf++;
}
return INF;
}
void work()
{
struct node
{
int x,y,z;
}q[],*qf=q,*qb=q;
bool qing[][][];
int d[][][],ex=,ey=,sx=,sy=,tx=,ty=;
scanf("%d%d%d%d%d%d",&ex,&ey,&sx,&sy,&tx,&ty);
if(!puz[ex][ey]||!puz[sx][sy]||!puz[tx][ty])
{
puts("-1");
return;
}
if(sx==tx&&sy==ty)
{
puts("");
return;
}
memset(qing,,sizeof(qing));
memset(d,0x3f,sizeof(d));
puz[sx][sy]=;
for(int i=;i<;i++)
{
int x=sx+dx[i];
int y=sy+dy[i];
if(puz[x][y])
{
d[sx][sy][i]=bfs(ex,ey,x,y);
qing[sx][sy][i]=true;
qb->x=sx;
qb->y=sy;
qb->z=i;
qb++;
}
}
puz[sx][sy]=;
while(qf!=qb)
{
qing[qf->x][qf->y][qf->z]=false;
for(int i=;i<;i++)
{
qb->x=qf->x+dx[i];
qb->y=qf->y+dy[i];
qb->z=i^;
if(puz[qb->x][qb->y]&&d[qb->x][qb->y][qb->z]>d[qf->x][qf->y][qf->z]+step[qf->x][qf->y][qf->z][i]+)
{
d[qb->x][qb->y][qb->z]=d[qf->x][qf->y][qf->z]+step[qf->x][qf->y][qf->z][i]+;
if(!qing[qb->x][qb->y][qb->z])
{
qing[qb->x][qb->y][qb->z]=true;
qb++;
}
}
}
qf++;
}
int ans=INF;
for(int i=;i<;i++)
ans=MIN(ans,d[tx][ty][i]);
if(ans!=INF)printf("%d\n",ans);
else puts("-1");
}
int main()
{
//freopen("puzzle.in","r",stdin);
//freopen("puzzle.out","w",stdout);
int q=;
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",*(puz+i)+j);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
{
int t=puz[i][j];
puz[i][j]=;
for(int k=;k<;k++)
for(int h=;h<;h++)
step[i][j][k][h]=bfs(i+dx[k],j+dy[k],i+dx[h],j+dy[h]);
puz[i][j]=t;
}
while(q--)work();
return ;
}

测试数据 #0: Accepted, time = 0 ms, mem = 788 KiB, score = 5

测试数据 #1: Accepted, time = 0 ms, mem = 788 KiB, score = 5

测试数据 #2: Accepted, time = 0 ms, mem = 788 KiB, score = 5

测试数据 #3: Accepted, time = 0 ms, mem = 788 KiB, score = 5

测试数据 #4: Accepted, time = 0 ms, mem = 788 KiB, score = 5

测试数据 #5: Accepted, time = 0 ms, mem = 788 KiB, score = 5

测试数据 #6: Accepted, time = 0 ms, mem = 784 KiB, score = 5

测试数据 #7: Accepted, time = 0 ms, mem = 788 KiB, score = 5

测试数据 #8: Accepted, time = 0 ms, mem = 792 KiB, score = 5

测试数据 #9: Accepted, time = 0 ms, mem = 788 KiB, score = 5

测试数据 #10: Accepted, time = 0 ms, mem = 792 KiB, score = 5

测试数据 #11: Accepted, time = 0 ms, mem = 788 KiB, score = 5

测试数据 #12: Accepted, time = 0 ms, mem = 788 KiB, score = 5

测试数据 #13: Accepted, time = 15 ms, mem = 792 KiB, score = 5

测试数据 #14: Accepted, time = 31 ms, mem = 788 KiB, score = 5

测试数据 #15: Accepted, time = 15 ms, mem = 792 KiB, score = 5

测试数据 #16: Accepted, time = 0 ms, mem = 788 KiB, score = 5

测试数据 #17: Accepted, time = 15 ms, mem = 788 KiB, score = 5

测试数据 #18: Accepted, time = 15 ms, mem = 788 KiB, score = 5

测试数据 #19: Accepted, time = 15 ms, mem = 788 KiB, score = 5

Accepted, time = 106 ms, mem = 792 KiB, score = 100

NOIP2013PUZZLE的更多相关文章

随机推荐

  1. Liunx cannot remove `xxx': Operation not permitted

    链接: http://mangocool.com/detail_1_1439515930283.html 解到原来文件还可以设置隐藏权限,就是这个chattr设置,下面我们来看看这个命令的详解. [r ...

  2. notepad++ 行首行尾添加字符

    有一次要处理SQL,拿到了脚本.但是要将其写入java 代码中,要在行首和行尾添加上引号.利用notepad++进行编辑. $表示行尾,^表示行首. 如上图,就这样.很高效. 如果只是在行尾添加字符, ...

  3. python变量作用域

    [python变量作用域] 几个概念: python能够改变变量作用域的代码段是def.class.lamda. if/elif/else.try/except/finally.for/while 并 ...

  4. pg_hba.conf、pool_hba.conf 以及 pool_passwd 三者间的关系

    pg_hba.conf.pool_hba.conf 以及 pool_passwd 三者间的关系: 1.pg_hba.conf.pool_hba.conf 以及 pool_passwd 三者关系 pg_ ...

  5. Python操作符重载总结&列表模型

    操作符重载 二元运算符 特殊方法 + __add__,__radd__ - __sub__,__rsub__ * __mul__,__rmul__ / __div__,__rdiv__,__trued ...

  6. for 续4

    ---------siwuxie095             (四)tokens=x,y,m-n 显示指定的列     tokens=x 只显示第 x 列 tokens=x,y,z 只显示第 x,y ...

  7. set 续3

    -------siwuxie095                 set 技巧高级篇:     1.利用 set /a 进行赋值     在开启变量延迟情况下,要判断数组 S!n! 的值的情况, 不 ...

  8. input checkbox 复选框大小修改

    设置zoom属性(放大) 利用style: <input type="checkbox" name="returnfee"  style="zo ...

  9. 使用IntelliJ IDEA,gradle开发Java web应用步骤

    最近 正在学习gradle构建工具的使用,看了一堆的文档,有点一知半解,索性动作实践一把,在以后的自己的项目中尝试使用看看.目前手头用的是IntelliJ IDEA 14,搭建了一天终于明白怎么集成g ...

  10. 深入剖析java的try…catch…finally语句

    一.前言 前些天参加面试的时候有一道题: public class test { public static void main(String[] args){ try { return; } fin ...