Helvetic Coding Contest 2016 online mirror D1
Description
"The zombies are lurking outside. Waiting. Moaning. And when they come..."
"When they come?"
"I hope the Wall is high enough."
Zombie attacks have hit the Wall, our line of defense in the North. Its protection is failing, and cracks are showing. In places, gaps have appeared, splitting the wall into multiple segments. We call on you for help. Go forth and explore the wall! Report how many disconnected segments there are.
The wall is a two-dimensional structure made of bricks. Each brick is one unit wide and one unit high. Bricks are stacked on top of each other to form columns that are up to R bricks high. Each brick is placed either on the ground or directly on top of another brick. Consecutive non-empty columns form a wall segment. The entire wall, all the segments and empty columns in-between, is C columns wide.
The first line of the input consists of two space-separated integers R and C, 1 ≤ R, C ≤ 100. The next R lines provide a description of the columns as follows:
- each of the R lines contains a string of length C,
- the c-th character of line r is B if there is a brick in column c and row R - r + 1, and . otherwise.
The number of wall segments in the input configuration.
3 7
.......
.......
.BB.B..
2
4 5
..B..
..B..
B.B.B
BBB.B
2
4 6
..B...
B.B.BB
BBB.BB
BBBBBB
1
1 1
B
1
10 7
.......
.......
.......
.......
.......
.......
.......
.......
...B...
B.BB.B.
3
8 8
........
........
........
........
.B......
.B.....B
.B.....B
.BB...BB
2
In the first sample case, the 2nd and 3rd columns define the first wall segment, and the 5th column defines the second.
判断连通块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <bits/stdc++.h> using namespace std; int n,m; char ma[200][200]; int vis[200][200]; void dfs( int x, int y) { if (x<0||x>=n||y<0||y>=m) return ; if (ma[x][y]== '.' ) return ; if (vis[x][y]) return ; vis[x][y]=1; if (ma[x][y]== 'B' )ma[x][y]= '.' ; dfs(x+1,y); dfs(x-1,y); dfs(x,y+1); dfs(x,y-1); } int main() { while (~ scanf ( "%d%d" ,&n,&m)){ memset (vis,0, sizeof (vis)); for ( int i=0;i<n;i++) scanf ( "%s" ,ma[i]); int ans=0; for ( int i=0;i<n;i++) for ( int k=0;k<m;k++) if (ma[i][k]== 'B' ) ans++,dfs(i,k); printf ( "%d\n" ,ans);} return 0; } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~