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的更多相关文章
随机推荐
- selenium webdriver——JS滚动到指定位置
1.DOM滚动方法 1.scrollIntoView(alignWithTop) 滚动浏览器窗口或容器元素,以便在当前视窗的可见范围看见当前元素.如果alignWithTop为true,或者省略它, ...
- ffmpeg默认输出中文为 UTF-8
在使用ffmpeg 进行对音视频文件解码输出信息的时候会出现乱码. 从网上找到了说ffmpeg默认格式 为 utf-8 如果vs工程使用的的 Unicode 则需要将 utf-8转 Unicode 才 ...
- maven工程 ,通过maven更新后,jre恢复到1.5的解决方法
在maven setting.xml profiles节点下加入 <profile> <id>jdk-1.8</id> <activation> < ...
- springmvc 类型转换器 数据回显及提示信息
处理器的写法: 类型转换器的写法: 类型转换器在springmvc.xml中的配置如下: index.jsp的写法:
- eclipse中项目出现红色的!
eclipse中项目出现红色的!的原因有二个:1.jdk不匹配 2.缺少jar包
- C++ 20170807
mesos/3rdparty/stout/include/stout/err.hpp=======================================================str ...
- php7源码编译安装
以下以CentOS 7.2为例,安装php的运行环境,首先打开php官网http://php.net/点击导航栏的Downloads进入下载页面:http://php.net/downloads.ph ...
- C语言文件实现学生成绩管理
C语言实现学生成绩管理 项目简介 用C语言的链表及文件操作实现学生成绩的管理,实现主要的添加.修改.删除.查询的主要功能,并在程序关闭时将数据存储在二进制的文件中并加密.下一次打开程序,先解密二进制文 ...
- js高级——构造函数,实例对象和原型对象——prototype、__proto__和constructor构造器
一.前言 了解JavaScript面向对象,需要先了解三个名词: 构造函数,实例对象和原型对象. 注意:JavaScript中没有类(class)的概念,取而代之的是构造函数,两者类似却又有很大的差别 ...
- JAVA - 守护线程(Daemon Thread)
转载自:http://www.cnblogs.com/luochengor/archive/2011/08/11/2134818.html 在Java中有两类线程:用户线程 (User Thread) ...