思路:
要解决这个问题,我们需要遍历矩阵中的每个元素,并使用广度优先搜索(BFS)来找到所有相连的细胞。每当找到一个新的细胞(即一个数字1到9),我们就从该细胞开始进行BFS,标记所有相连的细胞,并计数。
c++代码:
#include<iostream>
#include<istream>
#include<ostream>
#include<string>
#include<algorithm>
#include<bitset>
#include<cmath>
#include<queue>
#include<map>
#include<iomanip>
using namespace std;
int n,m,cnt = 0,nex[4][2] = {{-1,0},{0,1},{1,0},{0,-1}}; //矩阵大小,计数,四个方位:上下左右
char a[114][114]; //矩阵
bool vis[114][114]; //标记
struct node{ //结构体
int x,y;
};
void bfs(int x,int y){ //广搜
queue<node> q; //定义队列
node h,t; //定义队头,队尾
vis[x][y] = 1;
h.x = x;h.y = y;
q.push(h); //入队
while(!q.empty()){ //如果队列不为空
h = q.front(); //把队头干掉
for(int i = 0;i < 4;i++){ //循环四次,遍历四个方向
int tx = h.x + nex[i][0];
int ty = h.y + nex[i][1]; //变方向
if(tx < 1 || tx > n || ty < 1 || ty > m) continue; //如果出界了,就重新找下一个
if(a[tx][ty] == '0' || vis[tx][ty] == 1) continue; //如果这块不是细胞,或已经被标记过了,也要重新找下一个
t.x = tx;t.y = ty;
vis[tx][ty] = 1; //标记这里,以免下次重复找到
q.push(t); //再次入队
}
q.pop();
}
}
int main(){
cin>>n>>m; //输入矩阵大小
for(int i = 1;i <= n;i++){ //行
for(int j = 1;j <= m;j++){ //列
cin>>a[i][j]; //输入矩阵
}
}
for(int i = 1;i <= n;i++){ //行
for(int j = 1;j <= m;j++){ //列
if(a[i][j] != '0' && vis[i][j] == 0){ //如果是细胞
bfs(i,j); //调用广搜函数
cnt++; //计数加一
}
}
}
cout<<cnt; //输出有几个西八
return 0; //结束
}
Pascal代码:
program CountCells;
uses
crt;
const
MAXN = 100;
MAXM = 100;
type
TNode = record
x, y: Integer;
end;
var
a: array[1..MAXN, 1..MAXM] of Integer;
visited: array[1..MAXN, 1..MAXM] of Boolean;
n, m, i, j, cellCount: Integer;
q: array[1..MAXN * MAXM] of TNode;
front, rear: Integer;
h, t: TNode;
nex: array[1..4, 1..2] of Integer = ((-1, 0), (0, 1), (1, 0), (0, -1));
procedure bfs(xx, yy: Integer);
begin
front := 1;
rear := 1;
t.x := xx;
t.y := yy;
q[rear] := t;
visited[xx, yy] := True;
while front <= rear do
begin
h := q[front];
Inc(front);
for i := 1 to 4 do
begin
t.x := h.x + nex[i, 1];
t.y := h.y + nex[i, 2];
if (t.x >= 1) and (t.x <= n) and (t.y >= 1) and (t.y <= m) and (a[t.x, t.y] > 0) and not visited[t.x, t.y] then
begin
visited[t.x, t.y] := True;
Inc(rear);
q[rear] := t;
end;
end;
end;
end;
begin
Readln(n, m);
for i := 1 to n do
begin
for j := 1 to m do
begin
Read(a[i, j]);
end;
Readln;
end;
cellCount := 0;
for i := 1 to n do
begin
for j := 1 to m do
begin
if (a[i, j] > 0) and not visited[i, j] then
begin
bfs(i, j);
Inc(cellCount);
end;
end;
end;
Writeln(cellCount);
end.
Python代码:
from collections import deque
# 定义节点类
class Node:
def __init__(self, x, y):
self.x = x
self.y = y
# 定义四个方向的移动
nex = [(-1, 0), (0, 1), (1, 0), (0, -1)]
# 读取输入
n, m = map(int, input().split())
a = [list(map(int, input().strip())) for _ in range(n)]
visited = [[False] * m for _ in range(n)]
# 广度优先搜索函数
def bfs(xx, yy):
q = deque()
t = Node(xx, yy)
q.append(t)
visited[xx][yy] = True
while q:
h = q.popleft()
for dx, dy in nex:
tx, ty = h.x + dx, h.y + dy
if 0 <= tx < n and 0 <= ty < m and a[tx][ty] > 0 and not visited[tx][ty]:
visited[tx][ty] = True
q.append(Node(tx, ty))
# 细胞计数器
cell_count = 0
# 遍历矩阵
for i in range(n):
for j in range(m):
if a[i][j] > 0 and not visited[i][j]:
bfs(i, j)
cell_count += 1
# 输出细胞个数
print(cell_count)
Pascal和Python现打的,WA了不怪我