zoukankan      html  css  js  c++  java
  • NOIP2013PUZZLE

      1 #include<cstdio>
      2 #include<cstring>
      3 #define MIN(A,B) (A)<(B)?(A):(B)
      4 using namespace std;
      5 const int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1},INF=0x3f3f3f3f;
      6 int puz[32][32],n,m,step[32][32][4][4];
      7 int bfs(int sx,int sy,int tx,int ty)
      8 {
      9     struct node
     10     {
     11         int x,y;
     12     }q[10000],*qf=q,*qb=q+1;
     13     int d[32][32];
     14     if(!puz[sx][sy]||!puz[tx][ty])return INF;
     15     if(sx==tx&&sy==ty)return 0;
     16     memset(d,0x3f,sizeof(d));
     17     qf->x=sx;
     18     qf->y=sy;
     19     d[sx][sy]=0;
     20     while(qf!=qb)
     21     {
     22         for(int i=0;i<4;i++)
     23         {
     24             qb->x=qf->x+dx[i];
     25             qb->y=qf->y+dy[i];
     26             if(puz[qb->x][qb->y]&&d[qb->x][qb->y]==INF)
     27             {
     28                 d[qb->x][qb->y]=d[qf->x][qf->y]+1;
     29                 if(qb->x==tx&&qb->y==ty)return d[tx][ty];
     30                 qb++;
     31             }
     32         }
     33         qf++;
     34     }
     35     return INF;
     36 }
     37 void work()
     38 {
     39     struct node
     40     {
     41         int x,y,z;
     42     }q[10000],*qf=q,*qb=q;
     43     bool qing[32][32][4];
     44     int d[32][32][4],ex=0,ey=0,sx=0,sy=0,tx=0,ty=0;
     45     scanf("%d%d%d%d%d%d",&ex,&ey,&sx,&sy,&tx,&ty);
     46     if(!puz[ex][ey]||!puz[sx][sy]||!puz[tx][ty])
     47     {
     48         puts("-1");
     49         return;
     50     }
     51     if(sx==tx&&sy==ty)
     52     {
     53         puts("0");
     54         return;
     55     }
     56     memset(qing,0,sizeof(qing));
     57     memset(d,0x3f,sizeof(d));
     58     puz[sx][sy]=0;
     59     for(int i=0;i<4;i++)
     60     {
     61         int x=sx+dx[i];
     62         int y=sy+dy[i];
     63         if(puz[x][y])
     64         {
     65             d[sx][sy][i]=bfs(ex,ey,x,y);
     66             qing[sx][sy][i]=true;
     67             qb->x=sx;
     68             qb->y=sy;
     69             qb->z=i;
     70             qb++;
     71         }
     72     }
     73     puz[sx][sy]=1;
     74     while(qf!=qb)
     75     {
     76         qing[qf->x][qf->y][qf->z]=false;
     77         for(int i=0;i<4;i++)
     78         {
     79             qb->x=qf->x+dx[i];
     80             qb->y=qf->y+dy[i];
     81             qb->z=i^1;
     82             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]+1)
     83             {
     84                 d[qb->x][qb->y][qb->z]=d[qf->x][qf->y][qf->z]+step[qf->x][qf->y][qf->z][i]+1;
     85                 if(!qing[qb->x][qb->y][qb->z])
     86                 {
     87                     qing[qb->x][qb->y][qb->z]=true;
     88                     qb++;
     89                 }
     90             }
     91         } 
     92         qf++;
     93     }
     94     int ans=INF;
     95     for(int i=0;i<4;i++)
     96         ans=MIN(ans,d[tx][ty][i]);
     97     if(ans!=INF)printf("%d
    ",ans);
     98     else puts("-1");
     99 }
    100 int main()
    101 {
    102     //freopen("puzzle.in","r",stdin);
    103     //freopen("puzzle.out","w",stdout);
    104     int q=0;
    105     scanf("%d%d%d",&n,&m,&q);
    106     for(int i=1;i<=n;i++)
    107         for(int j=1;j<=m;j++)
    108             scanf("%d",*(puz+i)+j);
    109     for(int i=1;i<=n;i++)
    110         for(int j=1;j<=m;j++)
    111         {
    112             int t=puz[i][j];
    113             puz[i][j]=0;
    114             for(int k=0;k<4;k++)
    115                 for(int h=0;h<4;h++)
    116                     step[i][j][k][h]=bfs(i+dx[k],j+dy[k],i+dx[h],j+dy[h]);
    117             puz[i][j]=t;
    118         }
    119     while(q--)work();
    120     return 0;    
    121 }

    测试数据 #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

  • 相关阅读:
    iOS开发之--打印一堆奇怪东西的解决方案
    iOS开发之--从URL加载图片
    iOS开发之--搭建本地的SVN服务器
    HTML5
    swift
    swift
    HTML 换行
    HTML 注释
    HTML 水平线
    /etc/rc.d/rc.local
  • 原文地址:https://www.cnblogs.com/JebediahKerman/p/6075384.html
Copyright © 2011-2022 走看看