Codeforces - 723D. Lakes in Berland,bfs+sort
2017-02-19 21:48
239 查看
题目:
You are given arectangular field of n × m cells. Each cell is eitherempty or impassable (contains an obstacle). Empty cells are marked with '.',impassable cells are marked with '*'. Let's call two emptycells adjacent if they
share a side.
Let's calla connected component any non-extendible set of cells such that anytwo of them are connected by the path of adjacent cells. It is a typicalwell-known definition of a connected component.
For eachimpassable cell (x, y) imagine that it is an empty cell(all other cells remain unchanged) and find the size (the number of cells) ofthe connected component which contains (x, y). You should doit for
each impassable cell independently.
The answer shouldbe 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 ofthe i-th row
should contain the only digit —- the answermodulo 10. The matrix should be printed without any spaces.
To make youroutput faster it is recommended to build the output as an array of n stringshaving length m and print it as a sequence of lines. It willbe much faster than writing character-by-character.
As input/outputcan reach huge size it is recommended to use fast input/output methods: forexample, prefer to use scanf/printf instead of cin/cout inC++, prefer to use BufferedReader/PrintWriter insteadof Scanner/System.out in Java.
Input
The first linecontains two integers n, m (1 ≤ n, m ≤ 1000)— the number of rows and columns in the field.
Each of thenext n lines contains m symbols:"." for empty cells, "*" for impassable cells.
Output
Print the answeras a matrix as described above. See the examples to precise the format of theoutput.
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 tocomponent of size 5 (cross). If any of the corner cell will be emptythen it will be included to component of size 3 (corner).
思路:
题意求每个‘*’上下左右相连的'.'的个数包括‘*’本身。
不可以对每个‘*’直接进行bfs或者dfs 会t
预处理出每个联通的‘.'的区域,编号,并且记录个数。再扫一遍判断上下左右的‘.'的个数即可
代码:
You are given arectangular field of n × m cells. Each cell is eitherempty or impassable (contains an obstacle). Empty cells are marked with '.',impassable cells are marked with '*'. Let's call two emptycells adjacent if they
share a side.
Let's calla connected component any non-extendible set of cells such that anytwo of them are connected by the path of adjacent cells. It is a typicalwell-known definition of a connected component.
For eachimpassable cell (x, y) imagine that it is an empty cell(all other cells remain unchanged) and find the size (the number of cells) ofthe connected component which contains (x, y). You should doit for
each impassable cell independently.
The answer shouldbe 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 ofthe i-th row
should contain the only digit —- the answermodulo 10. The matrix should be printed without any spaces.
To make youroutput faster it is recommended to build the output as an array of n stringshaving length m and print it as a sequence of lines. It willbe much faster than writing character-by-character.
As input/outputcan reach huge size it is recommended to use fast input/output methods: forexample, prefer to use scanf/printf instead of cin/cout inC++, prefer to use BufferedReader/PrintWriter insteadof Scanner/System.out in Java.
Input
The first linecontains two integers n, m (1 ≤ n, m ≤ 1000)— the number of rows and columns in the field.
Each of thenext n lines contains m symbols:"." for empty cells, "*" for impassable cells.
Output
Print the answeras a matrix as described above. See the examples to precise the format of theoutput.
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 tocomponent of size 5 (cross). If any of the corner cell will be emptythen it will be included to component of size 3 (corner).
思路:
题意求每个‘*’上下左右相连的'.'的个数包括‘*’本身。
不可以对每个‘*’直接进行bfs或者dfs 会t
预处理出每个联通的‘.'的区域,编号,并且记录个数。再扫一遍判断上下左右的‘.'的个数即可
代码:
#include<cstdio> #include<queue> #include<set> #include<cstring> using namespace std; const int N=1005; char map ;char out ; int vis ; int sum ; struct point{ int x,y; point(int a,int b):x(a),y(b){} }; int dir[4][2]={-1,0,0,1,0,-1,1,0}; int pos=1; void bfs(int x,int y){ queue<point>que; que.push(point(x,y)); vis[x][y]=pos; int num=1; while(!que.empty()) { int tx=que.front().x,ty=que.front().y; que.pop(); for(int i=0;i<4;i++){ int dx=tx+dir[i][0],dy=ty+dir[i][1]; if(!vis[dx][dy]&&map[dx][dy]=='.'){ vis[dx][dy]=pos; que.push(point(dx,dy)); num++; } } } sum[pos]=num; pos++; } void init(){ pos=1; memset(map,'#',sizeof(map)); memset(vis,0,sizeof(vis)); memset(sum,0,sizeof(sum)); } int main() { int n,m; init(); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf(" %s",map[i]+1); out[i][m+1]='\0'; } for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(map[i][j]=='.'&&!vis[i][j]) bfs(i,j); } } set<int>st; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ if(map[i][j]=='.') out[i][j]='.'; else{ int t=0; for(int k=0;k<4;k++){ int dx=i+dir[k][0],dy=j+dir[k][1]; if(map[dx][dy]=='.'&&!st.count(vis[dx][dy])) t+=sum[vis[dx][dy]],st.insert(vis[dx][dy]); } st.clear(); out[i][j]=(t+1)%10+'0'; } } } for(int i=1;i<=n;i++) printf("%s\n",out[i]+1); return 0; }
相关文章推荐
- Codeforces 100187E:Two Labyrinths(搜索&&BFS)
- codeforces 788C The Great Mixing (bitset优化dp、bfs)
- Codeforces-242C-King's Path(bfs)
- CodeForces 500A New Year Transportation (BFS)
- Codeforces 883G Orientation of Edges(BFS+MAP)
- Codeforces 793B Igor and his way to work (dfs/bfs
- codeforces--652B z-sort(sorting)
- codeforces 652B B. z-sort(水题)
- Codeforces 652B z-sort 【思维】
- codeforces 659F (并查集 bfs)
- Codeforces 590C Three States(bfs)
- Codeforces 596C Wilbur and Points 【贪心 + sort】
- codeforces 793B - Igor and his way to work(bfs)
- Codeforces 190E Counter Attack【思维+Bfs】好题!
- CodeForces - 540C Ice Cave(BFS)
- CodeForces 329B(BFS)
- Codeforces 558A Lala Land and Apple Trees(Sort快排)
- Codeforces-540C-Ice Cave (BFS)
- codeforces 590C C. Three States(bfs+连通块之间的最短距离)
- Codeforces 788C 想法+bfs