CSU-ACM2017暑期训练6-bfs I - The Labyrinth CodeForces - 616C
2017-08-01 20:16
519 查看
题目:
marked with '*'. Let's call two empty cells adjacent if they share a side.
Let's call a connected component any non-extendible set of cells such that any two of them are connected by the path of adjacent cells. It is a typical well-known definition of a connected component.
For each impassable cell (x, y) imagine that it is an empty cell (all other cells remain unchanged) and find the size (the number of cells) of the connected component which contains (x, y).
You should do it for each impassable cell independently.
The answer should be printed as a matrix with n rows and m columns. The j-th symbol of the i-th row
should be "." if the cell is empty at the start. Otherwise the j-th symbol of the i-th row should contain the only digit —- the answer modulo 10.
The matrix should be printed without any spaces.
To make your output faster it is recommended to build the output as an array ofn strings having length m and print it as a sequence of lines. It will be much faster than writing
character-by-character.
As input/output can reach huge size it is recommended to use fast input/output methods: for example, prefer to use scanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead
of Scanner/System.out in Java.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 1000) — the number of rows and columns in the field.
Each of the next n lines contains m symbols: "." for empty cells, "*" for impassable cells.
Output
Print the answer as a matrix as described above. See the examples to precise the format of the output.
Example
Input
3 3 *.* .*. *.*
Output
3.3 .5. 3.3
Input
4 5 **..* ..*** .*.*. *.*.*
Output
46..3 ..732 .6.4. 5.4.3
Note
In first example, if we imagine that the central cell is empty then it will be included to component of size 5 (cross). If any of the corner cell will be empty then it will be included to component of size 3 (corner).
题目大意:给你一个图里面有'*'和‘.’问你每个*的上下左右方向所联通的‘.’的个数.然后输出的时候用数字代表*(数字对10取余).
思路:一开始理所当然想到直接对每个*进行bfs,当然是超时啦,正确的做法是先用bfs或dfs对‘.’的连通块进行预处理,也就是对不同的联通快编号并且计数。然后直接对'*'的上下左右看有哪些联通快即可,这里注意上下左右也有可能出现相同的联通快,所以记得判重。
代码:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> using namespace std; typedef long long ll; const double PI = acos(-1.0); const double eps = 1e-6; const int INF = 1000000000; const int maxn = 100; int T,n,m,k=1; char maps[1111][1111]; long long book[1111][1111]; long long sums[1111][1111]; long long num[11111111]; int nexts[4][2]={0,1,-1,0,1,0,0,-1}; void dfs(int a,int b) { book[a][b]=k; num[k]++; for(int i=0;i<4;i++) { int aa=a+nexts[i][0]; int bb=b+nexts[i][1]; if(aa>=0&&bb>=0&&aa<n&&bb<m&&book[aa][bb]==0&&maps[aa][bb]=='.') dfs(aa,bb); } return; } int main() { scanf("%d%d ",&n,&m); memset(book,0,sizeof(book)); memset(num,0,sizeof(num)); for(int i=0;i<n;i++) scanf("%s",maps[i]); for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(maps[i][j]=='.'&&book[i][j]==0) { dfs(i,j); k++; } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { int sum=0; if(maps[i][j]=='*') { set<int>t; for(int p=0;p<4;p++) { int a=i+nexts[p][0]; int b=j+nexts[p][1]; if(maps[a][b]=='.'&&a>=0&&b>=0&&a<n&&b<m&&!t.count(book[a][b])) { t.insert(book[a][b]); sum+=(num[book[a][b]]%10); } } sums[i][j]=sum; } } } for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(maps[i][j]=='.') printf("."); else { printf("%d",(sums[i][j]+1)%10); } } printf("\n"); } return 0; }
相关文章推荐
- CSU-ACM2017暑期训练6-bfs G - Mike and Shortcuts CodeForces - 689B
- CSU-ACM2017暑期训练10-并查集&&HASH F - Flying to the Mars HDU - 1800(字符串hash)
- CSU-ACM2017暑期训练4-dfs E - The Settlers of Catan POJ - 2258
- CSU-ACM2017暑期训练4-dfs F - Twenty-four point CSU - 1600
- CSU-ACM2017暑期训练16-树状数组 H - Bubble Sort HDU - 5775
- CSU-ACM2017暑期训练1-Debug与STL hdu1022-Train Problem I
- CSU-ACM2017暑期训练10-并查集&&HASH C - 食物链 POJ - 1182 (并查集好题)
- CSU-ACM2017暑期训练12-KMP E - 循环
- Languages--CSU-ACM2017暑期训练1-Debug与STL
- CSU-ACM2017暑期训练12-KMP F - 前缀后缀
- CSU-ACM2017暑期训练16-树状数组 A - Ultra-QuickSort POJ - 2299
- CSU-ACM2017暑期训练1-Debug与STL -圆桌问题
- CSU-ACM2017暑假集训比赛1 C - Gourmet and Banquet CodeForces - 589F
- Function Run Fun--CSU-ACM2017暑期训练3-递推与递归
- CSU-ACM2017暑期训练12-KMP H - 结合dp
- CSU-ACM2017暑期训练12-KMP G - Next[]树 CodeForces - 432D
- CSU-ACM2017暑期训练5-三分 A - Strange fuction HDU - 2899
- CSU-ACM2017暑期训练16-树状数组 C - 按钮控制彩灯实验 CSU - 1770
- CSU-ACM2017暑期训练3-递推与递归 J - Non-boring sequences
- CSU-ACM2017暑期训练3-递推与递归 Erratic Expansion