#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. zookeeper分布式锁和服务优化配置

    转自:https://www.jianshu.com/p/02eeaee4357f?utm_campaign=maleskine&utm_content=note&utm_medium ...

  2. How To Install Spring IDE In Eclipse

    Spring IDE is a very useful graphical user interface tool adding support for Spring Framework. In th ...

  3. springboot+shiro+jwt

    1.添加依赖 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-sp ...

  4. $or操作符

    [$or操作符] The $or operator performs a logical OR operation on an array of two or more <expressions ...

  5. open中的mode

    [open中的mode] 当使用O_CREAT标志的open来创建文件时,我们必须使用三个参数格式的open调用.第三个参数mode 是几个标志按位OR后得到的.他们是: S_IRUSR: 读权限,文 ...

  6. nasm 与 masm语法区别

    看到一篇文章,是介绍nasm语法的:http://blog.csdn.net/hitop0609/article/details/4329454 masm是微软专门为windows下汇编而写的,而na ...

  7. C# \/date(1498820611133+0800)\/ 转DateTime

    开发中经常遇到日期转换问题,特别是做接口的时候,现在整理了下时间戳转为C#格式时间的方法: /// <summary> /// 时间戳转为C#格式时间 /// </summary&g ...

  8. MyBatis 实用篇(一)入门

    MyBatis 实用篇(一)入门 MyBatis(http://www.mybatis.org/mybatis-3/zh/index.html) 是一款优秀的持久层框架,它支持定制化 SQL.存储过程 ...

  9. MySql 8小时解决方案:proxool连接池

    最近做的项目用的mysql数据库,前天挂在服务器上,昨天早晨上班一来,同事就说API数据接口访问不了了,我马上mstsc登陆服务器看,报错了.马上重启tomcat,结果还能正常运行,当时没管,今天过来 ...

  10. 仅仅 IE8 有效的 CSS hack 写法

    IE8 CSS hack 就是在属性后面加上 \9 或者 \0,代码如下: color:#FFF\0; /* IE8 */ color:#FFF\9; /* 所有IE浏览器(ie6+) */ 上面的 ...