zoukankan      html  css  js  c++  java
  • Lightoj--1337 1337 The Crystal Maze

    题目链接:

    http://lightoj.com/volume_showproblem.php?problem=1337

    这个题最重要的就是记忆化搜索,根据这个题我所知道的记忆化搜索就是:当出现某一测试数据时把与其有关的数据都搜索出来,以备后用。

    AC代码:

     1 #include <iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include <cstring>
     5 using namespace std;
     6 int ans[505][505];
     7 char s[505][505];
     8 int n,m,p,t,u,v;
     9 struct node
    10 {
    11     int x,y;
    12 }que[255000];
    13 int bfs(int x,int y)
    14 {
    15     int head,tail,tx,ty,sum;
    16     int a[4]={1,-1,0,0},b[4]={0,0,1,-1};
    17     head=tail=1;
    18     que[tail].x=x;que[tail].y=y;
    19     tail++;
    20     sum=0;
    21     if(s[x][y]=='C')
    22     sum++;
    23     s[x][y]='#';
    24     while(head<tail)
    25     {
    26         for(int i=0;i<4;i++)
    27         {
    28             tx=que[head].x+a[i];
    29             ty=que[head].y+b[i];
    30             if(tx>=n||tx<0||ty>=m||ty<0||s[tx][ty]=='#')
    31             continue;
    32             if(s[tx][ty]=='C')
    33             {
    34                 sum++;
    35             }
    36             if(s[tx][ty]=='.'||s[tx][ty]=='C')
    37             {
    38                 s[tx][ty]='#';
    39                 que[tail].x=tx;
    40                 que[tail].y=ty;
    41                 tail++;
    42             }
    43         }
    44         head++;
    45     }
    46     for(int i=1;i<tail;i++)
    47     ans[que[i].x][que[i].y]=sum;
    48     return 0;
    49 
    50 }
    51 int main()
    52 {
    53     while(~scanf("%d",&t))
    54     {
    55         for(int k=1;k<=t;k++)
    56         {
    57             scanf("%d%d%d",&n,&m,&p);
    58             for(int i=0;i<n;i++)
    59             scanf("%s",s[i]);
    60             memset(que,0,sizeof(que));
    61             memset(ans,0,sizeof(ans));
    62             printf("Case %d:
    ",k);
    63             for(int i=0;i<p;i++)
    64             {
    65                 scanf("%d%d",&u,&v);
    66                 if(s[u-1][v-1]!='#')
    67                 {
    68                     bfs(u-1,v-1);
    69                     memset(que,0,sizeof(que));
    70                 }
    71                 printf("%d
    ",ans[u-1][v-1]);
    72             }
    73         }
    74     }
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    【LOJ#6277】数列分块1
    【LOJ6284】数列分块8
    【洛谷P3275】糖果
    【洛谷P3810】陌上花开
    【洛谷P1052】过河 离散化+dp
    【洛谷P2042】维护数列
    【模板】文艺平衡树
    【洛谷P4145】花神游历各国
    【洛谷P4878】布局
    hdu 5748(LIS)
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/5765327.html
Copyright © 2011-2022 走看看