C. The Labyrinth

题目连接:

http://www.codeforces.com/contest/616/problem/C

Description

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.

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.

Sample Input

3 3

.

.*.

.

Sample Output

3.3

.5.

3.3

Hint

题意

给你n行m列的矩阵,矩阵*表示障碍,.表示空地

对于每一个障碍,让你输出去掉这个障碍之后,这个点所在的连通块的大小是多少

答案需要%10

题解:

这个块的联通块大小,实际上是由四个部分所组成的

上下左右的连通块加在一起就好了

但是有一个问题,就是你有可能加重复,所以我们需要有个vis处理

处理原来连通块,我是用的带权并查集去做的

dfs和bfs应该都可以,因为复杂度都是差不多的(其实应该比并查集还低

代码

#include<bits/stdc++.h>
using namespace std; char s[1150][1150];
int fa[1010050];
int num[1100500];
int v[1010050];
int n,m;
int id(int x,int y)
{
return x*m+y;
}
int fi(int x)
{
return x == fa[x]?x:fa[x]=fi(fa[x]);
}
int uni(int x,int y)
{
int p = fi(x),q = fi(y);
if(p != q)
{
fa[p]=fa[q];
num[q]=num[p]+num[q];
num[p]=0;
}
}
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int main()
{ scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",s[i]+1);
for(int i=0;i<=id(n,m)+5;i++)
num[i]=1,fa[i]=i;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(s[i][j]=='*')continue;
for(int k=0;k<4;k++)
{
int x = i + dx[k];
int y = j + dy[k];
if(x<1||x>n||y<1||y>m)continue;
if(s[x][y]=='*')continue;
uni(id(x,y),id(i,j));
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(s[i][j]=='.')
printf(".");
else
{
int ans = 1;
for(int k=0;k<4;k++)
{
int x = i + dx[k];
int y = j + dy[k];
if(x<1||x>n||y<1||y>m)continue;
if(s[x][y]=='*')continue;
int idx = fi(id(x,y));
if(v[idx]==0)
{
v[idx]=1;
ans+=num[idx];
}
}
printf("%d",ans%10);
for(int k=0;k<4;k++)
{
int x = i + dx[k];
int y = j + dy[k];
if(x<1||x>n||y<1||y>m)continue;
if(s[x][y]=='*')continue;
int idx = fi(id(x,y));
v[idx]=0;
}
}
}
printf("\n");
}
}

Codeforces Educational Codeforces Round 5 C. The Labyrinth 带权并查集的更多相关文章

  1. Codeforces Round #181 (Div. 2) B. Coach 带权并查集

    B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...

  2. Codeforces 1499G - Graph Coloring(带权并查集+欧拉回路)

    Codeforces 题面传送门 & 洛谷题面传送门 一道非常神仙的题 %%%%%%%%%%%% 首先看到这样的设问,做题数量多一点的同学不难想到这个题.事实上对于此题而言,题面中那个&quo ...

  3. Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array 带权并查集

    C. Destroying Array 题目连接: http://codeforces.com/contest/722/problem/C Description You are given an a ...

  4. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  5. codeforces 687D Dividing Kingdom II 带权并查集(dsu)

    题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...

  6. CodeForces - 688C:NP-Hard Problem (二分图&带权并查集)

    Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex c ...

  7. CodeForces - 687D: Dividing Kingdom II (二分图&带权并查集)

    Long time ago, there was a great kingdom and it was being ruled by The Great Arya and Pari The Great ...

  8. Codeforces 1156D 带权并查集

    题意:给你一颗树,树边的权值可能是0或1,问先走0边,再走1边,或者只走1边的路径有多少条? 思路:对于一个点,假设通过0边相连的点一共有x个(包括自己),通过1边相连的有y个(包括自己),那么对答案 ...

  9. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

随机推荐

  1. inno setup详细使用教程

    前段时间我完成了几个软件的汉化,想把它们打包起来,可是苦于我是一个很菜的鸟,很笨的瓜,只好上网找关于安装程序制作的文章.不幸我没能找到:-( 没法只好自己去华军软件园里找找制作安装程序的软件,并一把下 ...

  2. delphi 中字符串与16进制、10进制转换函数

      //字符串转成16进制代码function strToHexStr(str:string):string;varc:char;ss:string;i:integer;beginwhile str& ...

  3. php时间函数

    PHP中的时间函数有这么些:(1)date用法: date(格式,[时间]);如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:U 替换成从一个起始时间(好象是1970年1 ...

  4. PYTHON压平嵌套列表

    list 是 Python 中使用最频繁的数据类型, 标准库里面有丰富的函数可以使用.不过,如果把多维列表转换成一维列表(不知道这种需求多不多),还真不容易找到好用的函数,要知道Ruby.Mathem ...

  5. 通过ajax提交form表单

    $.ajax({ url : 'deliveryWarrant/update.do', data : $('#myform').serialize(), type : "POST" ...

  6. Debian openvpn 配置

    1.安装openvpn 和 iptables -- Debain 可以使用命令行`apt-get install openvpn iptables` 2.配置服务器 -- ```shell cp -R ...

  7. noip模拟赛 软件software

    地图上的 n个城市,由 n-1条道路连接,且任意两个城市连通.除 1号城市之外的每个都有 一台计算机,安装软件号城市之外的每个都有 一台计算机,安装软件一个 自己的安装时间.住在 1号城市的蒟蒻要给这 ...

  8. socket 粘包问题(转)

    https://www.v2ex.com/t/234785#reply3 1. 面向字节流的 IO 都有这个问题. socket 中 tcp 协议是面向流的协议,发送方发送和接收方的接收并不是一一对应 ...

  9. 【转】大数据以及Hadoop相关概念介绍

    原博文出自于: http://www.cnblogs.com/xdp-gacl/p/4230220.html 感谢! 一.大数据的基本概念 1.1.什么是大数据 大数据指的就是要处理的数据是TB级别以 ...

  10. 对比AMD 890、AMD 880、 AMD 790、AMD 785、 AMD 780、AMD 7

    770无集显.中低端独显主流. 780G带集显.现在可以无视. 785G现在是带集显的主流. 790GX高端带集显. 790FX专高端,无集显. 790X带集显.基本无视. 870 大板,无集显 88 ...