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的更多相关文章
随机推荐
- ASP.NET中的几种弹出框提示基本实现方法
我们在.NET程序的开发过程中,常常需要和用户进行信息交互,比如执行某项操作是否成功,“确定”还是“取消”,以及选择“确定”或“取消”后是否需要跳转到某个页面等,下面是本人对常用对话框使用的小结,希望 ...
- GridView弹出对话框
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) ...
- leetcode257
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- asp.net 初级程序员面试题【待续】
C# 常见的排序方式 冒泡排序(Bubble sort) 堆排序(Heap sort) 插入排序(Insertion sort) 归并排序(Merge sort) 快速排序(Quick sort) ...
- StretchBlt
StretchBlt 函数从源矩形中复制一个位图到目标矩形,必要时按目前目标设备设置的模式进行图像的拉伸或压缩以满足目标矩形的尺寸. 原型: BOOL StretchBlt( HDC hdcDest ...
- 使用Visual Studio进行 Android开发的十大理由
[原文发表地址]Top 10 reasons to use Visual Studio for C++ Android Development! Visual Studio: C++跨平台的移动解决方 ...
- Add words to your picture
[Add words to your picture] How to add text to your photo, and then style it with the Type tool. 1.O ...
- Multithread之为什么spinlock必须是volatile?
[Multithread之为什么spinlock必须是volatile?] 1.编译器的优化 在本次线程内,当读取一个变量时,为提高存取速度,编译器优化时有时会先把变量读取到一个寄存器中:以后再取变量 ...
- for 续4
---------siwuxie095 (四)tokens=x,y,m-n 显示指定的列 tokens=x 只显示第 x 列 tokens=x,y,z 只显示第 x,y ...
- xshell上传下载文件(Windows、Linux)
经常有这样的需求,我们在Windows下载的软件包,如何上传到远程Linux主机上?还有如何从Linux主机下载软件包到Windows下:之前我的做法现在看来好笨好繁琐,不过也达到了目的,笨人有本方法 ...