首页 > 试题广场 >

挡住洪水

[编程题]挡住洪水
  • 热度指数:2950 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}吴铭市近日洪水肆虐,洪水从地图外部渗入。市政部门在若干方格上砌起围墙,用 `*` 表示。若某片空地区域不与地图边界四联通(上下左右方向),则不会被洪水淹没,视为安全空地。

\hspace{15pt}地图用大小为 x \times y 的字符矩阵描述:`'*'` 表示围墙,`'0'` 表示空地。所有相互四联通的 `'0'` 构成一个区域。请统计所有安全空地格子的数量之和(即所有不与边界四联通的 `'0'` 的总数)。

输入描述:
\hspace{15pt}第一行输入两个整数 x,y\left(1\leqq x,y\leqq 500\right)
\hspace{15pt}接下来 x 行,每行 y 个字符,组成围墙建设图,仅含 `'*'` 与 `'0'`。


输出描述:
\hspace{15pt}输出一个整数,表示所有安全空地格子的总数。
示例1

输入

2 2
**
**

输出

0
头像 牛客242693846号
发表于 2025-07-25 15:56:34
感觉题目有歧义....是统计不会被淹的0的数量不是区域的数量。 from collections import deque def in_bound(x, y, n, m): return 0 <= x < n and 0 <= y < m n, m = map 展开全文
头像 blue小哥
发表于 2025-11-03 09:51:23
#include <bits/stdc++.h> using namespace std; using ll=long long; const int mod=1000100087; int a,b; char s[510][510]; int st[510][510]; void 展开全文
头像 lahm66
发表于 2025-11-03 14:02:50
JavaDFS深度优先遍历将四周的‘0’深度遍历为‘*’,最后统计所有的‘0’即可 import java.util.Scanner; public class Main { //dfs写成了bfs,没有太大影响 public static void bfs(char[][] ch, 展开全文
头像 365cent
发表于 2025-11-03 17:46:55
#include <stdio.h> int a, b; // dimensions: a rows, b columns char *map; #define MAP(i, j) map[(i) * b + (j)] void dfs(int x, int y) { if 展开全文
头像 bluewhalew1
发表于 2025-11-03 19:08:16
#include<bits/stdc++.h> using namespace std; #define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0) #define pb push_back using ll = long lon 展开全文
头像 玄骨
发表于 2025-11-03 22:59:34
#include<bits/stdc++.h> using namespace std; char a[600][600]; void bfs(int x,int y) { if(a[x][y]=='0'){ a[x][y]='*'; bfs(x- 展开全文
头像 牛客754921490号
发表于 2025-12-15 22:48:59
#include <iostream> #include <vector> using namespace std; struct Pt { int y; int x; }; int main() { int xLen,yLen; cin &g 展开全文
头像 TRfirst
发表于 2025-11-03 09:18:53
题目相当于要求统计所有不与在边界的 连通的 的数量,从所有位于边界位置的 开始 后标记即可。 #include <bits/stdc++.h> using namespace std; const int X4[4] = {1, -1, 0, 0}, Y4[4] = {0, 0, 展开全文
头像 草海桐
发表于 2025-09-07 19:32:24
package main /* 先把边界为'0'的地方都BFS标记一次(会被淹没的'0') 然后再次BFS_未被标记的'0',计算安全的'0'的个数 */ import ( "fmt" ) type Node struct{ i,j in 展开全文
头像 冷艳的西红柿刷牛客
发表于 2025-10-28 11:11:18
#include <iostream> #include <string> #include <queue> #include <algorithm> using namespace std; struct Grid { int x; int 展开全文