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的更多相关文章
随机推荐
- Angular2中Input和Output
@Input @Input是用来定义模块的输入的,用来让父模块往子模块传递内容: @Output 子模块自定义一些event传递给父模块用@Output. 对于angular2中的Input和Outp ...
- VS2008项目移植到Linux
不少人都遇到过这种情况:在Windows下用Visual Studio工具开发的程序需要移植到Linux系统中,做成Linux版本的,但程序比较大,在Linux上又离不开Make,手动编写Makefi ...
- python portia
docker run -i -t --rm -v <PROJECTS_FOLDER>:/app/data/projects:rw -p 9001:9001 scrapinghub/port ...
- Shder中实现TintColor
[Shder中实现TintColor] TintColor实现上相当于一个滤镜,若TintColor的R为0,则原图的R通道颜色应该为0.基于此,实现TintColor很容易,原图颜色直接乘以Tint ...
- SAP middb主键加索引
alter table DEPTBIGCPDL_DBC drop constraint DEPTBIGCPDL_DBC_PK CREATE UNIQUE INDEX TABLEAU_USER.DEPT ...
- synchronized的简单理解
synchronized能够保证在同一时刻只有一个线程执行该段代码. 使用synchronized能够防止多个线程同时并发访问程序的某些资源. synchronized既可以修饰变量,也可以修饰方法, ...
- java工具类 --千分位方法
/** * 千分位方法 * @param text * @return */ public static String fmtMicrometer(String text) { DecimalForm ...
- XSS学习笔记
本片文章是读<<XSS跨站脚本gj剖析与防御>>一书的总结 常见的XSS攻击主要用于1.网络钓鱼,盗用用户账号2.窃取cookies 非httponly情况下,读取docume ...
- centos firewall-cmd常用命令
firewall-cmd --list-all firewall-cmd --zone=public --add-port=12345/tcp --permanent firewall-cmd --z ...
- DataStage 六、安装和部署集群环境
DataStage序列文章 DataStage 一.安装 DataStage 二.InfoSphere Information Server进程的启动和停止 DataStage 三.配置ODBC Da ...