NOIP2013PUZZLE
#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的更多相关文章
随机推荐
- JS 根据url 下载
一. window.location="htpp://www.baidu.com/test.rar"; 二. var $form = $('<form method=&quo ...
- readlink 获取当前进程对应proc/self/exe
[readlink 获取当前进程对应proc/self/exe] shell中 readlink /proc/self/exe READLINK(2)NAME readlink - re ...
- JAVA Spring MVC中各个filter的用法
spring mvc的org.springframework.web.filter包下的Java文件如下: 类的结构如下: AbstractRequestLoggingFilter及其子类 Abstr ...
- golang之指针
先上代码 package main import "fmt" type myInt int //匿名函数 //值的接收 //指针的接收 //*myint表示myInt的指针类型 / ...
- CF 990 Educational Codeforces Round 45
既然补了就简单记录一下. 感觉还算有一点营养. 官方题解传送门:点我 A Commentary Boxes 对拆掉$n \mod m$个和新建$m - (n \mod m)$求个最小. #includ ...
- sklearn中决策树算法DesiciontTreeClassifier()调用以及sklearn自带的数据包sklearn.datasets.load_iris()的应用
决策树方法的简单调用记录一下 clf=tree.DecisionTreeClassifier() dataMat=[];labelMat=[] dataPath='D:/machinelearning ...
- Devexpress VCL Build v2013 vol 13.2.3 发布
继续修修补补,大过年的,就不吐槽了. What's New in 13.2.3 (VCL Product Line) New Major Features in 13.2 What's New i ...
- ASP.NET MVC与ASP.NET Web API的区别(转)
出处:http://blog.csdn.net/wangzl1163/article/details/72676616 MVC主要用来构建网站,既关心数据也关心页面展示,而Web API只关注数据 W ...
- JMS规范简介
一.JMS规范 Java消息服务定义: Java消息服务(Java Message Service)即JMS,是一个Java平台中面向消息中间件的API,用于在两个应用程序之间或分布式系统中发送/接受 ...
- Linux 基础教程 30-tcpdump命令-2
在上一节讲了一些tcpdump常用功能,本期我们继续学习tcpdump后续功能. 流量过滤 tcpdump不仅支持单个过滤表达式过滤,也还支持多个过滤表达式.但需要注意的是传入的过滤表 ...