【DidaOJ】[1144]Old Wang Builds a Park

Problem Description

Loading...

Problem Analysis

A very simple search problem
using BFS

cpp
 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
34
#include<stdio.h>
int map[120][120];
int cnt;
int n,m;
void bfs(int i,int j) {
	if(map[i][j]==1||i<0||j<0||i>=n||j>=m)
		return ;
	map[i][j]=1;
	bfs(i+1,j);
	bfs(i-1,j);
	bfs(i,j+1);
	bfs(i,j-1);
}
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		scanf("%d %d",&n,&m);
		for(int i=0; i<n; i++)
			for(int j=0; j<m; j++)
				scanf("%d",&map[i][j]);
		cnt=0;
		for(int i=0; i<n; i++) {
			for(int j=0; j<m; j++) {
				if(map[i][j]==0) {
					cnt++;
					bfs(i,j);
				}
			}
		}
		printf("%d\n",cnt);
	}
	return 0;
}

Problem link: 【DidaOJ】[1144]Old Wang Builds a Park

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...