zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 5

    题目链接:http://codeforces.com/contest/616/problem/C

    题意就是 给你一个n行m列的图,让你求’*‘这个元素上下左右相连的连续的’.‘有多少(本身也算一个),每个’*‘的结果取模10。要是为’*‘输出结果,否则输出’.‘。

    这个题目就是让你求连续的'.'联通块元素个数,求完一个联通块就把这个联通块标个记号(我设了ok[][]二维数组 表示这个位置的元素的联通块的标号,相连的则为同一个标号),之后这个联通块的每个元素的ans都为f(f为联通块元素个数),然后把这些dfs过的联通块标记为经过即可。当你求’*‘周围联通的’.‘个数的时候 判断上下左右的ok[][]数组是否一样,再加ans[][]答案就好。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <vector>
     5 #include <set>
     6 
     7 using namespace std;
     8 const int MAXN = 1e3 + 5;
     9 char map[MAXN][MAXN];
    10 int tox[] = {-1 , 0 , 1 , 0} , toy[] = {0 , -1 , 0 , 1} , m , n;
    11 int ok[MAXN][MAXN] , f , cont[MAXN * MAXN];
    12 typedef pair <int , int> P;
    13 vector <P> v;
    14 
    15 bool judge(int x , int y) {
    16     if(map[x][y] == '.' && !ok[x][y] && x >= 0 && x < n && y >= 0 && y < m) {
    17         return true;
    18     } 
    19     return false;
    20 }
    21 
    22 void dfs(int x , int y) {
    23     for(int i = 0 ; i < 4 ; i++) {
    24         if(judge(x + tox[i] , y  + toy[i])) {
    25             v.push_back(P(x + tox[i] , y + toy[i]));
    26             ok[x + tox[i]][y + toy[i]] = f;
    27             dfs(x + tox[i] , y + toy[i]);
    28         }
    29     }
    30 }
    31 
    32 int main()
    33 {
    34     f = 1;
    35     ios::sync_with_stdio(false);
    36     set <int> s;
    37     cin >> n >> m;
    38     for(int i = 0 ; i < n ; i++) {
    39         cin >> map[i];
    40     }
    41     memset(ok , 0 , sizeof(ok));
    42     memset(cont , 0 , sizeof(cont));
    43     for(int i = 0 ; i < n ; i++) {
    44         for(int j = 0 ; j < m ; j++) {
    45             if(!ok[i][j] && map[i][j] == '.') {
    46                 ok[i][j] = f;
    47                 v.clear();
    48                 v.push_back(P(i , j));
    49                 dfs(i , j);
    50                 cont[f++] = v.size();
    51             }
    52         }
    53     }
    54     int temp = 1;
    55     for(int i = 0 ; i < n ; i++) { 
    56         for(int j = 0 ; j < m ; j++) {
    57             if(map[i][j] == '*') {
    58                 temp = 1;
    59                 s.clear();
    60                 for(int k = 0 ; k < 4 ; k++) {
    61                     int x = tox[k] + i , y = toy[k] + j;
    62                     if(map[x][y] == '.' && x >= 0 && y >= 0 && y < m && x < n) {
    63                         s.insert(ok[x][y]);
    64                     }
    65                 }
    66                 set<int>::iterator it = s.begin();
    67                 for( ; it != s.end() ; it++) {
    68                     temp += cont[int(*it)];
    69                 }
    70                 cout << temp % 10;
    71             }
    72             else {
    73                 cout << '.';
    74             }
    75         }
    76         cout << endl;
    77     }
    78 }
  • 相关阅读:
    December 23rd 2016 Week 52nd Friday
    December 22nd 2016 Week 52nd Thursday
    December 21st 2016 Week 52nd Wednesday
    December 20th 2016 Week 52nd Tuesday
    December 19th 2016 Week 52nd Sunday
    December 18th 2016 Week 52nd Sunday
    uva294(唯一分解定理)
    uva11624Fire!(bfs)
    fzu2150Fire Game(双起点bfs)
    poj3276Face The Right Way
  • 原文地址:https://www.cnblogs.com/Recoder/p/5182456.html
Copyright © 2011-2022 走看看