zoukankan      html  css  js  c++  java
  • [ZOJ3781]Paint the Grid Reloaded

    思路:

    先用DFS缩点,然后BFS找出每个点出发能到达的最长路,取$min$。
    注意多组数据,初始化一定要仔细,刚开始存边的$e$忘记初始化,一直WA,调了半个晚上。
    一开始和网上的题解对拍$T=1$一直对不出错,后来测了大数据才发现是初始化的问题。

     1 #include<queue>
     2 #include<vector>
     3 #include<cstring>
     4 #include<iostream>
     5 const int inf=0x7fffffff;
     6 const int dx[]={-1,1,0,0},dy[]={0,0,-1,1};
     7 int n,m;
     8 const int N=42,V=1601;
     9 bool a[N][N],b[N][N];
    10 int g[N][N];
    11 bool map[V][V];
    12 int cnt,ans;
    13 std::vector<int> e[V];
    14 inline void init() {
    15     cnt=0,ans=inf;
    16     memset(a,0,sizeof a);
    17     memset(g,0,sizeof g);
    18     memset(map,0,sizeof map);
    19     memset(b,0,sizeof b);
    20     for(int i=1;i<=n;i++) {
    21         b[i][0]=b[i][m+1]=true;
    22     }
    23     for(int j=1;j<=m;j++) {
    24         b[0][j]=b[n+1][j]=true;
    25     }
    26     for(int i=0;i<V;i++) e[i].clear();
    27 }
    28 inline void add_edge(const int u,const int v) {
    29     if(!map[u][v]) {
    30         e[u].push_back(v);
    31         map[u][v]=true;
    32     }
    33 }
    34 void dfs(const int x,const int y) {
    35     b[x][y]=true;
    36     for(int i=0;i<4;i++) {
    37         int xx=x+dx[i],yy=y+dy[i];
    38         if(g[xx][yy]&&g[xx][yy]!=g[x][y]) {
    39             add_edge(g[x][y],g[xx][yy]);
    40             add_edge(g[xx][yy],g[x][y]);
    41         }
    42         if(b[xx][yy]) continue;
    43         if(a[x][y]==a[xx][yy]) {
    44             g[xx][yy]=g[x][y];
    45             dfs(xx,yy);
    46         }
    47     }
    48 }
    49 bool v[V];
    50 void bfs(const int s) {
    51     memset(v,0,sizeof v);
    52     std::queue<std::pair<int,int> > q;
    53     q.push(std::make_pair(s,0));
    54     v[s]=true;
    55     int max=0;
    56     while(!q.empty()) {
    57         int x=q.front().first,d=q.front().second;
    58         q.pop();
    59         for(unsigned i=0;i<e[x].size();i++) {
    60             int &y=e[x][i];
    61             if(v[y]) continue;
    62             q.push(std::make_pair(y,d+1));
    63             v[y]=true;
    64         }
    65         max=std::max(max,d);
    66     }
    67     ans=std::min(ans,max);
    68 }
    69 int main() {
    70     std::ios_base::sync_with_stdio(false);
    71     std::cin.tie(NULL);
    72     int T;
    73     std::cin>>T;
    74     while(T--) {
    75         std::cin>>n>>m;
    76         init();
    77         for(int i=1;i<=n;i++) {
    78             for(int j=1;j<=m;j++) {
    79                 char ch;
    80                 std::cin>>ch;
    81                 a[i][j]=ch=='O';
    82             }
    83         }
    84         for(int i=1;i<=n;i++) {
    85             for(int j=1;j<=m;j++) {
    86                 if(b[i][j]) continue;
    87                 g[i][j]=++cnt;
    88                 dfs(i,j);
    89             }
    90         }
    91         for(int i=1;i<=cnt;i++) bfs(i);
    92         std::cout<<ans<<std::endl;
    93     }
    94     return 0;
    95 }
  • 相关阅读:
    内存泄漏检测
    qt 关于内存泄漏的检测
    Valgrind 安装与使用
    Qt应用中检测内存泄露——VLD
    Visual C++内存泄露检测—VLD工具使用说明
    ArcGIS Runtime支持的GP工具列表(转 )
    c# 调用ArcEngine的GP工具
    ArcEngine 数据导入经验(转载)
    在ArcEngine中使用Geoprocessing工具-执行工具
    利用C#与AE调用GP工具
  • 原文地址:https://www.cnblogs.com/skylee03/p/7424965.html
Copyright © 2011-2022 走看看