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;
    }
    
  • 相关阅读:
    Python 一条语句如何在多行显示的问题
    代理模式
    MySQL workbench中的PK,NN,UQ,BIN,UN,ZF,AI说明
    异步加载 Echarts图的数据
    Web页面中两个listbox的option的转移
    半透明效果
    在地图上使图片透明
    加载图片方式
    获取鼠标坐标
    画笔与画刷
  • 原文地址:https://www.cnblogs.com/tomori/p/15027588.html
Copyright © 2011-2022 走看看