Educational Codeforces Round 5 - C. The Labyrinth (dfs联通块操作)

题目链接: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 }

 

转载于:https://www.cnblogs.com/Recoder/p/5182456.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值