zoukankan      html  css  js  c++  java
  • P1141 01迷宫

    第一次交的时候T了三个点,发现询问数量最大1e5, 当迷宫比较完美(从每一点开始都可以把整个迷宫跑一遍)的时候,这个复杂度为(1e5 * 1000*1000 = 10^{11}(O(n^2m))),超时

    如果一个位置A可以到达B,则A、B相互可达,并且从A和B出发能到达的位置数目相同,所以再搜索完一次之后可以把路径上的所有点的答案都填上,这样再下一次询问路径上的点的时候就可以直接输出答案,以最坏情况为例,复杂度降为(O(n^2)),因为只有第一次需要把整个图跑一遍

    #include<iostream>
    #include<cstring>
    #include<vector>
    #include<cstdio>
    
    using namespace std;
    
    #define PII pair<int, int>
    
    const int N = 1010;
    
    char g[N][N];
    int ans[N][N];
    int st[N][N];
    int n, m;
    
    vector<PII> path; 
    
    int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
    
    int dfs(int a, int b){
        st[a][b] = 1;
        path.push_back({a, b});
        int sum = 1;
        for(int i = 0; i < 4; i ++){
            int x = a + dx[i], y = b + dy[i];
            if(x < 0 || y < 0 || x >= n || y >= n || st[x][y]) continue;
            if(g[x][y] ^ g[a][b]) sum += dfs(x, y);
        }
        
        return sum;
    }
    
    int main(){
        scanf("%d%d", &n, &m);
        
        for(int i = 0; i < n; i ++) scanf("%s", g[i]);
        
        while(m --){
            path.clear();
            
            int a, b;
            scanf("%d%d", &a, &b);
            if(!ans[a - 1][b - 1]){
                int res = dfs(a - 1, b - 1);
                for(auto t : path) ans[t.first][t.second] = res;
            }
            cout << ans[a - 1][b - 1] << endl;
        }
        
        return 0;
    }
    
  • 相关阅读:
    Google app engine python 2.5.4 安装ssl
    Ubuntu 10.04分辨率
    Google Voice 国内用户开通全攻略(图文)
    (linux)查看及修改文件权限以及相关
    InstallAnyWhere使用笔记制作升级补丁时的一些判断
    openoffice 编译依赖关系履历
    匹配连续的任意字词
    BT3 无线密码
    All roads lead to Rome, some smooth, some rough.
    test
  • 原文地址:https://www.cnblogs.com/tomori/p/15027588.html
Copyright © 2011-2022 走看看