CodeForces 616C The Labyrinth (二维并查集)
来源:互联网 发布:qt编程用什么软件 编辑:程序博客网 时间:2024/05/21 06:28
题目链接: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 cellsadjacent 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 andm columns. The j-th symbol of thei-th row should be "." if the cell is empty at the start. Otherwise thej-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 usescanf/printf instead of cin/cout in C++, prefer to use BufferedReader/PrintWriter instead ofScanner/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
In first example, if we imagine that the central cell is empty then it will be included to component of size5 (cross). If any of the corner cell will be empty then it will be included to component of size3 (corner).
另一种简便做法是标号广搜 这里就只贴并查集的代码
#include<bits/stdc++.h>#define maxn 1000010using namespace std;struct xjy{ int x; int y;};int dir[4][2]={1,0,0,1,-1,0,0,-1};char mmap[1111][1111];xjy pre[1111][1111];int num[1111][1111];xjy findx(xjy xx){ xjy r=xx; while(pre[r.x][r.y].x!=r.x||pre[r.x][r.y].y!=r.y) { r=pre[r.x][r.y]; } xjy i=xx,j; while(pre[i.x][i.y].x!=r.x||pre[i.x][i.y].y!=r.y) { j=pre[i.x][i.y]; pre[i.x][i.y]=r; i=j; } return r;}void join (xjy xx,xjy yy){ xjy p1=findx(xx); xjy p2=findx(yy); if(p1.x!=p2.x||p1.y!=p2.y) { pre[p1.x][p1.y]=p2; num[p2.x][p2.y]+=num[p1.x][p1.y]; num[p1.x][p1.y]=0; }}int main(){ for(int i=0;i<=1001;i++) for(int j=0;j<=1001;j++) { xjy mid; mid.x=i; mid.y=j; pre[i][j]=mid; num[i][j]=0; mmap[i][j]='*'; } int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { char c; scanf(" %c",&c); if(c=='.') { mmap[i][j]=c; num[i][j]=1; } else mmap[i][j]=c; } for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(mmap[i][j]=='.') { xjy mid; mid.x=i; mid.y=j; xjy midmid; for(int ii=0;ii<4;ii++) { midmid.x=mid.x+dir[ii][0]; midmid.y=mid.y+dir[ii][1]; if(mmap[midmid.x][midmid.y]=='.') { join(mid,midmid); } } } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(mmap[i][j]=='*') { int sum=0; xjy mid; mid.x=i; mid.y=j; vector<xjy > sss; for(int ii=0;ii<4;ii++) { xjy midmid; midmid.x=mid.x+dir[ii][0]; midmid.y=mid.y+dir[ii][1]; if(mmap[midmid.x][midmid.y]=='.') { sum=0; midmid=findx(midmid); //printf("%d %d\n",midmid.x,midmid.y); //printf("%d\n",sss.size()); if(sss.size()==0) sss.push_back(midmid); else { int eend=sss.size(); int flag=1; for(int it=0;it!=eend;it++) if(midmid.x==sss[it].x&&midmid.y==sss[it].y) { flag=0; } if(flag) sss.push_back(midmid); } } } for(int it=0;it!=sss.size();it++) { xjy midmidmid=findx(sss[it]); int midx=midmidmid.x; int midy=midmidmid.y; sum+=num[midx][midy]; } printf("%d",(sum+1)%10); } else printf("%c",mmap[i][j]); } printf("\n"); }}
- CodeForces 616C The Labyrinth (二维并查集)
- Educational Codeforces Round 5 C.The Labyrinth(dfs+并查集)
- Educational Codeforces Round 5 C. The Labyrinth BFS,并查集
- codeforces 616C The Labyrinth(bfs)
- CodeForces 616 C. The Labyrinth(dfs)
- CodeForces 616C The Labyrinth(DFS)
- CodeForces 616C:The Labyrinth(BFS)
- Codeforces 616C The Labyrinth
- codeforces 616C The Labyrinth
- Codeforces 616C The Labyrinth
- The Labyrinth CodeForces - 616C
- codeforces 616C The Labyrinth (dfs)
- Educational Codeforces Round 5-C. The Labyrinth(简单dfs)
- Educational Codeforces Round 5 C. The Labyrinth (DFS **)
- CF 616C The Labyrinth(dfs)
- 二维并查集——CodeForces
- Educational Codeforces Round 5 C. The Labyrinth
- Educational Codeforces Round 5 C. The Labyrinth
- BOOTstrap3 各类CSS样式范例
- de.greenrobot:eventbus:3.0.0-beta1混淆的坑
- Ubuntu 10.04下fcitx输入法中去掉多余的输入方式
- MySQL不能启动 FTS optimize thread exiting
- Hdu 6168 Numbers【思维+暴力】
- CodeForces 616C The Labyrinth (二维并查集)
- VS2012、VS2013启用angularjs智能提示Intelligence
- filter/listener/interceptor区别与联系
- 带ToolTip验证框JS组件
- 斯坦福大学公开课机器学习课程(Andrew Ng)九经验风险最小化
- Qt 5.9.1、VS 2017创建DLL的互相调用
- Ubuntu 10.04下unrar的命令使用
- Design库-TabLayout属性详解
- win10+py-faster-rcnn