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的更多相关文章
随机推荐
- tensorflow 基本函数(1.tf.split, 2.tf.concat,3.tf.squeeze, 4.tf.less_equal, 5.tf.where, 6.tf.gather, 7.tf.cast, 8.tf.expand_dims, 9.tf.argmax, 10.tf.reshape, 11.tf.stack, 12tf.less, 13.tf.boolean_mask
1. tf.split(3, group, input) # 拆分函数 3 表示的是在第三个维度上, group表示拆分的次数, input 表示输入的值 import tensorflow ...
- c# 之 new 关键字
1.实例化变量 DataTable dt = new DataTable(); 2.调用构造函数 class CoOrds { public int x, y; // 实例构造函数(默认构造函数) ...
- Angular2中Input和Output
@Input @Input是用来定义模块的输入的,用来让父模块往子模块传递内容: @Output 子模块自定义一些event传递给父模块用@Output. 对于angular2中的Input和Outp ...
- tomcat加固
tomcat安全加固和规范 tomcat是一个开源Web服务器,基于Tomcat的Web运行效率高,可以在一般的硬件平台上流畅运行,因此,颇受Web站长的青睐.不过,在默认配置下其存在一定的安全隐患, ...
- LUA 运行期间不独占线程的递归,通过回调实现
function main(d) local function func(d) moveto(d, function() print("d=======", d) d = d - ...
- eclipse中使用maven的 maven install
windows -> preferences -> Java -> Installed JREs 在default VM arguments 中添加 -Dmaven.multiMod ...
- Kubernetes的UI界面Kubernetes Dashboard的搭建
1.搭建准备 Kubernetes集群的安装部署 2.搭建过程 2.1.在master节点上创建kubernetes-dashboard.yaml cd /etc/kubernetes vim kub ...
- 使用Log4J监控系统日志邮件警报
使用Log4J监控系统日志邮件警报 前言 在系统上线后,有时候遇到系统故障,这时候就可以登录服务器查看系统日志来排查问题.但是需要登录服务器,下载查找相关异常日志比较麻烦.而且没有监控的话,也无法实时 ...
- golang之流程控制(注意点)
Go在流程控制方面特点如下: 没有do和while循环,只有一个广义的for语句 switch语句灵活多变,还可以用于类型判断 if语句和switch语句都可以包含一条初始化子语句 break语句和c ...
- etl使用表
select * from etl_data_map t;select * from etl_column t;select * from etl_table_def t;select * from ...