zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)

    题目链接:http://codeforces.com/problemset/problem/598/D

    题意是 给你一张行为n宽为m的图 k个询问点 ,求每个寻问点所在的封闭的一个上下左右连接的块所能看到的壁画有多少(大概这样吧)。

    我的做法是bfs(dfs也可以)这个为'.'的点,要是遇到上下左右其中有'*'的话就加起来。要是每次询问然后bfs一下肯定超时,所以我用一个ans[1005][1005]记录每个点的值,ok[1005][1005]数组判断是否访问过,初始化为false。然后开始遍历一次,遇到'*'的ans则为0,遇到'.' 且 ok[i][j] = false的就bfs周围相连的'.',算出答案然后再逐个赋值给一个房间的ans[i][j],都标记经过 即ok[i][j] = true ...。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <queue>
     6 
     7 using namespace std;
     8 char map[MAXN][MAXN];
     9 bool ok[MAXN][MAXN];
    10 int ans[MAXN][MAXN];
    11 int n , m , res , tx[] = {-1 , 0 , 1 , 0} , ty[] = {0 , 1 , 0 , -1};
    12 struct data {
    13     int x , y;
    14 };
    15 
    16 void init() {
    17     memset(ok , false , sizeof(ok));
    18 }
    19 
    20 bool judge(int x , int y) {              //判断点是否是'.'   且未经过
    21     if(x < n && y < m && x >= 0 && y >= 0 && map[x][y] != '*' && !ok[x][y]) {
    22         return true;
    23     }
    24     return false;
    25 }
    26 
    27 bool judge2(int x , int y) {            //判断点是否是'*'
    28     if(x < n && y < m && x >= 0 && y >= 0 && map[x][y] == '*') {
    29         return true;
    30     }
    31     return false;
    32 }
    33 
    34 int get(int x , int y) {     //壁画的相加
    35     int num = 0;
    36     for(int i = 0 ; i < 4 ; i++) {
    37         if(judge2(x + tx[i] , y + ty[i])) {
    38             num++;
    39         }
    40     }
    41     return num;
    42 }
    43 
    44 void bfs(int x , int y) {
    45     vector<data > v;    //用来存相连的'.'
    46     queue <data> Q;
    47     data a;
    48     a.x = x , a.y = y;
    49     Q.push(a);
    50     while(!Q.empty()) {
    51         data temp = Q.front();
    52         v.push_back(temp);
    53         Q.pop();
    54         if(map[temp.x][temp.y] == '.') {
    55             res += get(temp.x , temp.y);
    56             ok[temp.x][temp.y] = true;
    57         }
    58         for(int i = 0 ; i < 4 ; i++) {
    59             a.x = temp.x + tx[i] , a.y = temp.y + ty[i];
    60             if(judge(a.x , a.y)) {
    61                 Q.push(a);
    62                 ok[a.x][a.y] = true;
    63             }
    64         }
    65     }
    66     for(int i = 0 ; i < v.size() ; i++) {   
    67         ans[v[i].x][v[i].y] = res;
    68     }
    69     v.clear();  //清空
    70 }
    71 
    72 int main()
    73 {
    74     int q , sx , sy;
    75     ios::sync_with_stdio(false);
    76     while(cin >> n >> m >> q) {
    77         memset(ans , 0 , sizeof(ans));
    78         for(int i = 0 ; i < n ; i++) 
    79             cin >> map[i];
    80         init();
    81         for(int i = 0 ; i < n ; i++) {
    82             for(int j = 0 ; j < m ; j++) {
    83                 res = 0;
    84                 if(!ok[i][j] && map[i][j] == '.') {
    85                     bfs(i , j);
    86                 }
    87             }
    88         }
    89         while(q--) {
    90             cin >> sx >> sy;
    91             cout << ans[sx - 1][sy - 1] << endl;
    92         }
    93     }
    94 }
  • 相关阅读:
    log&& buffevent&&内存池 1
    ngx内存池设计概阅
    读 perf 笔记 简写
    smaps 使用&& 内存泄露
    cache占用高 文件delete cache
    工具小用法 dropwatch ss perf
    golang 读书笔记 数据类型
    重看ebpf 通信&&数据结构分析
    TCP 发送缓冲区问题--根本原因是gso引起 转载
    重看ebpf -代码载入执行点-hook
  • 原文地址:https://www.cnblogs.com/Recoder/p/4965879.html
Copyright © 2011-2022 走看看