传送门:
http://codeforces.com/problemset/problem/616/C
You are given a rectangular field of n × m cells. Each cell is either empty or impassable (contains an obstacle). Empty cells are marked with '.', impassable cells are 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 of n 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.
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.
Print the answer as a matrix as described above. See the examples to precise the format of the output.
3 3
*.*
.*.
*.*
3.3
.5.
3.3
4 5
**..*
..***
.*.*.
*.*.*
46..3
..732
.6.4.
5.4.3
题目意思:
求每个*能够到达的格子数量,只有.可以走(四个方向扩展),结果mod 10,替换 * 后输出
分析:
开始是想对每个*号进行bfs,但是思考了一下,这种做法态蠢了,也耗时
冥思苦想无果,看了一下xp大佬的博客
得到了思路:
对‘.’号进行遍历扩展,得到每个连通块的标号和大小,标号用vis数组储存
map储存标号对应的连通块的大小,然后对每个*号,把它上下左右的连通块的标号放入set中(去重重复的连通块标号,这里注意理解)
然后在map中找到对应标号的连通块大小,加起来之后加1(*自身)就是该*号周围的.号的个数
code:
#include<stdio.h> #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <math.h> #include <cstdlib> #include <queue> #include<map> #include<set> using namespace std; #define max_v 1005 char G[max_v][max_v]; int vis[max_v][max_v];//记录连通块的标号 int n,m; struct node { int x,y; }; queue<node> q; map<int,int> mm;//记录对应标号连通块的大小 set<int> ss;//去除*号周围重复的标号的连通块 set<int>::iterator it; int dir[4][2]= {1,0,0,1,-1,0,0,-1}; int check(int x,int y) { if(x<0||x>=n||y<0||y>=m) return 0; if(vis[x][y]) return 0; if(G[x][y]!='.') return 0; return 1; } void bfs(int x,int y,int cnt) { node p,next; vis[x][y]=cnt; int ans=0; p.x=x; p.y=y; q.push(p); while(!q.empty()) { ans++; p=q.front(); q.pop(); for(int i=0; i<4; i++) { next.x=p.x+dir[i][0]; next.y=p.y+dir[i][1]; if(check(next.x,next.y)) { vis[next.x][next.y]=cnt; q.push(next); } } } mm[cnt]=ans; // printf("%d-->%d\n",cnt,ans); } int main() { scanf("%d %d",&n,&m); for(int i=0; i<n; i++) scanf("%s",G[i]); memset(vis,0,sizeof(vis)); int cnt=1; for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(G[i][j]=='.'&&vis[i][j]==0) { bfs(i,j,cnt++);//得到连通块的标号和大小 } } } for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { if(G[i][j]=='*') { ss.clear(); //将*周围连通块的标号放入set去重 if(i>0) ss.insert(vis[i-1][j]); if(i<n-1) ss.insert(vis[i+1][j]); if(j>0) ss.insert(vis[i][j-1]); if(j<m-1) ss.insert(vis[i][j+1]); int res=1; for(it=ss.begin(); it!=ss.end(); it++) { res+=mm[*it];//通过map找到对应标号连通块的大小 // printf("set中值:%d map中的值:%d\n",*it,mm[*it]); } // printf("res=%d\n",res); G[i][j]=char('0'+res%10); } } } for(int i=0; i<n; i++) { printf("%s\n",G[i]); } }