#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. Maven(六) eclipse 使用Maven deploy命令部署构建到Nexus

    转载于:http://blog.csdn.net/jun55xiu/article/details/43051627 1  应用场景:SYS-UTIL(系统工具)项目部署.构建成JAR包(SYS-UT ...

  2. angular性能优化心得

    原文出处 脏数据检查 != 轮询检查更新 谈起angular的脏检查机制(dirty-checking), 常见的误解就是认为: ng是定时轮询去检查model是否变更.其实,ng只有在指定事件触发后 ...

  3. MVC各层的职责

    Model(模型):模型代表着核心的业务逻辑和数据(不要理解成Model只是实体类) View(视图):视图应该关注与如何展示数据,而不应该包含任何业务逻辑(业务逻辑应写在Model中) Contro ...

  4. docker-compose搭建单机多节点es + kibana

    docker-compose.yml配置如下: version: '2.2' services: elasticsearch: image: docker.elastic.co/elasticsear ...

  5. exe怎么找main函数

    先说DEBUG版本的命令行EXE main函数有两种,第一种是int main(),第二种是int main(int argc,char* argv[]) 不管哪种只要查找j___p___argc就能 ...

  6. 查看binlog的简单方法!

    今天学到一个牛逼的东西,不用打开binlog文件就可以查看binlog里的event事件. 命令为:show binlog events in 'mysql-bin.000001' from 4 li ...

  7. Python服务器开发 -- 网络基础-乾颐堂

    网络由下往上分为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. HTTP是高层协议,而TCP/IP是个协议集,包过许多的子协议.包括:传输层的 FTP,UDP,TCP协议等,网络层的ip ...

  8. [Selenium] 最大化或自定义浏览器的大小

      driver.manage().window().maximize(); //将浏览器设置为最大化的状态   driver.manage().window().setSize(new Dimens ...

  9. Yii2 upload

    http://webtips.krajee.com/advanced-upload-using-yii2-fileinput-widget/ http://webtips.krajee.com/upl ...

  10. Transform动画初解 in Swift

    创建一个界面,就像这样的: 顶部是一个UISegmentControl,用来制定transform的类型.分别是:CGAffineTransformMakeTranslation.CGAffineTr ...