Educational Codeforces Round 5-C. The Labyrinth(简单dfs)
2016-01-12 12:07
381 查看
C. The Labyrinth
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 test(s)
Input
Output
Input
Output
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).
这道题一开始就用dfs写了一半了,之后就估计了一下时间复杂度,发现dfs会超时就没写下去了,之后就断网了就看电影了,结果早上看了看
官方题解:
Let's enumerate all the connected components, store their sizes and for each empty cell store the number of it's component. It can be done with a single dfs. Now the answer for some impassable cell is equal to one plus the sizes of all different adjacent
connected components. Adjacent means the components of cells adjacent to the current impassable cell (in general case each unpassable cell has four adjacent cells).
就是用dfs啊,结果发现傻逼了,时间复杂度估计错误
。
总结:已经在时间复杂度估计上出错好几次了,以后对于不确定的时间复杂度还是认认真真的写下去吧。。。
耗时: 280ms多吧。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#include<set>
using namespace std;
#define CRL(a) memset(a,0,sizeof(a))
typedef __int64 ll;
#define T 1010
#define mod 1000000007
int c[T][T],n,m;
int vis[T][T];
char s[T][T];
int fx[][2]={{1,0},{0,1},{-1,0},{0,-1}};
bool jugde(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&s[x][y]!='*')
return true;
return false;
}
int cnt,cc;
vector< pair<int,int> > ve;
void dfs(int x,int y)
{
for(int i=0;i<4;++i){
int tx = x + fx[i][0];
int ty = y + fx[i][1];
if(jugde(tx,ty)){
ve.push_back(make_pair(tx,ty));
cnt++;
vis[tx][ty]=cc;
dfs(tx,ty);
}
}
}
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
int i,j,k,ma;
while(~scanf("%d%d",&n,&m))
{
cc = 0;
for(i=0;i<n;++i){
for(j=0;j<m;++j){
c[i][j] = 1;
vis[i][j]=0;
scanf("\n%c",&s[i][j]);
}
}
for(i=0;i<n;++i){
for(j=0;j<m;++j){
if(!vis[i][j]&&s[i][j]=='.'){
cc++;
vis[i][j] = cc;
cnt = 1;
dfs(i,j);
for(int f=0;f<ve.size();++f)
{
c[ve[f].first][ve[f].second] = cnt;
}
ve.clear();
c[i][j] = cnt;
}
}
}
for(i=0;i<n;++i){
for(j=0;j<m;++j){
if(s[i][j]!='.'){
k = 0;
int Down=i+1,Up=i-1,Left=j-1,Right=j+1;
int f1=0,f2=0,f3=0;
//上
if(Up>=0&&s[Up][j]=='.')k+=c[Up][j],f1=1;
//下
if(
(
Down<n&&s[Down][j]=='.'&&
(!f1||vis[Down][j]!=vis[Up][j])
)
)k+=c[Down][j],f2=1;
//左
if(
(
Left>=0&&s[i][Left]=='.'&&
(!f1||vis[Up][j]!=vis[i][Left])&&
(!f2||vis[Down][j]!=vis[i][Left])
)
)k+=c[i][Left],f3=1;
//右
if(
(
Right<m&&s[i][Right]=='.'&&
(!f1||vis[Up][j]!=vis[i][Right])&&
(!f2||vis[Down][j]!=vis[i][Right])&&
(!f3||vis[i][Left]!=vis[i][Right])
)
)k+=c[i][Right];
printf("%d",(k+1)%10);
}
else {
printf(".");
}
}
printf("\n");
}
}
return 0;
}
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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 test(s)
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).
这道题一开始就用dfs写了一半了,之后就估计了一下时间复杂度,发现dfs会超时就没写下去了,之后就断网了就看电影了,结果早上看了看
官方题解:
Let's enumerate all the connected components, store their sizes and for each empty cell store the number of it's component. It can be done with a single dfs. Now the answer for some impassable cell is equal to one plus the sizes of all different adjacent
connected components. Adjacent means the components of cells adjacent to the current impassable cell (in general case each unpassable cell has four adjacent cells).
就是用dfs啊,结果发现傻逼了,时间复杂度估计错误
。
总结:已经在时间复杂度估计上出错好几次了,以后对于不确定的时间复杂度还是认认真真的写下去吧。。。
耗时: 280ms多吧。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#include<set>
using namespace std;
#define CRL(a) memset(a,0,sizeof(a))
typedef __int64 ll;
#define T 1010
#define mod 1000000007
int c[T][T],n,m;
int vis[T][T];
char s[T][T];
int fx[][2]={{1,0},{0,1},{-1,0},{0,-1}};
bool jugde(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y]&&s[x][y]!='*')
return true;
return false;
}
int cnt,cc;
vector< pair<int,int> > ve;
void dfs(int x,int y)
{
for(int i=0;i<4;++i){
int tx = x + fx[i][0];
int ty = y + fx[i][1];
if(jugde(tx,ty)){
ve.push_back(make_pair(tx,ty));
cnt++;
vis[tx][ty]=cc;
dfs(tx,ty);
}
}
}
int main()
{
#ifdef zsc
freopen("input.txt","r",stdin);
#endif
int i,j,k,ma;
while(~scanf("%d%d",&n,&m))
{
cc = 0;
for(i=0;i<n;++i){
for(j=0;j<m;++j){
c[i][j] = 1;
vis[i][j]=0;
scanf("\n%c",&s[i][j]);
}
}
for(i=0;i<n;++i){
for(j=0;j<m;++j){
if(!vis[i][j]&&s[i][j]=='.'){
cc++;
vis[i][j] = cc;
cnt = 1;
dfs(i,j);
for(int f=0;f<ve.size();++f)
{
c[ve[f].first][ve[f].second] = cnt;
}
ve.clear();
c[i][j] = cnt;
}
}
}
for(i=0;i<n;++i){
for(j=0;j<m;++j){
if(s[i][j]!='.'){
k = 0;
int Down=i+1,Up=i-1,Left=j-1,Right=j+1;
int f1=0,f2=0,f3=0;
//上
if(Up>=0&&s[Up][j]=='.')k+=c[Up][j],f1=1;
//下
if(
(
Down<n&&s[Down][j]=='.'&&
(!f1||vis[Down][j]!=vis[Up][j])
)
)k+=c[Down][j],f2=1;
//左
if(
(
Left>=0&&s[i][Left]=='.'&&
(!f1||vis[Up][j]!=vis[i][Left])&&
(!f2||vis[Down][j]!=vis[i][Left])
)
)k+=c[i][Left],f3=1;
//右
if(
(
Right<m&&s[i][Right]=='.'&&
(!f1||vis[Up][j]!=vis[i][Right])&&
(!f2||vis[Down][j]!=vis[i][Right])&&
(!f3||vis[i][Left]!=vis[i][Right])
)
)k+=c[i][Right];
printf("%d",(k+1)%10);
}
else {
printf(".");
}
}
printf("\n");
}
}
return 0;
}
相关文章推荐
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- win2003分布式文件系统(dfs)配置方法[图文详解]
- win2003分布式文件系统及其部署 图文教程
- Hadoop2.6+jdk8的安装部署(1)——使用jar包安装部署【详细】
- Hadoop FS Shell
- DFS使用方法总结
- FastDFS注意事项
- 无忧技术带您预览DFS(分布式文件系统)管理控制台
- C 语言实现迷宫 DFS算法
- 一幅图弄清DFT与DTFT,DFS的关系
- HDFS---Namenode
- HDFS ---- Services startup
- POJ1523 SPF dfs
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- poj1731 Orders dfs
- Surrounded Regions
- Binary Tree Zigzag Level Order Traversal,Restore IP Addresses,Word Search,Simplify Path