当前位置:网站首页>Find the number of "blocks" in the matrix (BFS)
Find the number of "blocks" in the matrix (BFS)
2022-04-23 05:34:00 【Han Tiao】
Title Description
Give a mxn Matrix , The elements in the matrix are 0 or 1. Weigh the position (x,y) Instead of four positions up, down, left and right (x,y+1)、(x,y-1)、(x+1,y)、(x-1,y) It's adjacent . If there are several in the matrix 1 It's adjacent ( It doesn't have to be adjacent ), So call these 1 Make up a “ block ”. Find the matrix in a given matrix “ block ” The number of .
【 sample input 】
6 7
0 1 1 1 0 0 1
0 0 1 0 0 0 0
0 0 0 0 1 0 0
0 0 0 1 1 1 0
1 1 1 0 1 0 0
1 1 1 1 0 0 0
【 sample output 】
4
Their thinking
Use breadth first search to traverse a “ block ”, And mark it as 1, After marking, it will not be traversed again , How many times do you traverse , Just a few “ block ”.
Code implementation
import java.util.*;
public class Main {
// The incremental
static int n;
static int m;
static int[] X={
0,0,-1,1};// On Next Left Right
static int[] Y={
-1,1,0,0};// On Next Left Right
static int[][] matrix;// matrix
static boolean[][] visted;// Used to mark whether a point is accessed
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
n=scan.nextInt();//n That's ok
m= scan.nextInt();//m Column
visted=new boolean[n][m];
matrix=new int[n][m];
// Input matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
matrix[i][j]= scan.nextInt();
}
}
// Search each point in the matrix in turn ( Of course, the condition is that it has not been searched before , And this bit is 0)
int count=0;
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
if(!visted[x][y]&&matrix[x][y]==1){
bfs(x,y); // In this question bfs The result is , The next one to meet “1 block ” Marked as accessed
count++;
}
}
}
System.out.println(count);
scan.close();
}
public static void bfs(int x,int y){
node start=new node(x,y);
visted[x][y]=true;
Deque<node> que=new LinkedList<>();
que.offer(start);
while(!que.isEmpty()){
node now=que.poll();
for(int i=0;i<4;i++){
int newx=now.x+X[i];
int newy=now.y+Y[i];
if(check(newx,newy)){
visted[newx][newy]=true;
node next=new node(newx,newy);
que.offer(next);
}
}
}
}
public static boolean check(int x,int y){
// An array
if(x<0||x>=n||y<0||y>=m){
return false;
}
// Has been interviewed
if(visted[x][y]||matrix[x][y]==0){
return false;
}
return true;
}
}
class node{
int x;
int y;
node(){
}
node(int x,int y){
this.x=x;
this.y=y;
}
}
版权声明
本文为[Han Tiao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220534300006.html
边栏推荐
- selenium預先加載cookie的必要性
- How to set the initial value of El input number to null
- 提升Facebook触及率和互动率攻略 | 智能客服帮您抓住用户的心
- [untitled] Notepad content writing area
- Use of uniapp native plug-ins
- 分支与循环语句
- Hongji micro classroom | cyclone RPA's "flexible digital employee" actuator
- [machine learning] scikit learn introduction
- Knowledge of egg testing -- mock, Supertest, coffee
- Error handling mechanism of the strongest egg framework in history
猜你喜欢
Pol / select / EPO
Arithmetic and logical operations
Nécessité de précharger les cookies dans le sélénium
After adding qmenu to qtoolbutton and QPushButton, remove the triangle icon in the lower right corner
selenium预先加载cookie的必要性
跨域CORS的情缘~
3d slicer中拉直体的生成
Use of qwbengneview and qwebchannel.
弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化
deep learning object detection
随机推荐
Call the interface to get the time
转置卷积(Transposed Convolution)
Parameter analysis of open3d material setting
After NPM was upgraded, there was a lot of panic
Some pits used by uni
JS time format conversion
Redis in node -- ioredis
Relative reference and absolute reference of Excel
What financial products will benefit during May Day?
Self incrementing sequence creation of MySQL
The prefix of static of egg can be modified, including boots
CPT 104_ TTL 09
Create process memory management copy_ Mm - processes and threads (IX)
Pol / select / EPO
X86 assembly syntax: at & T and Intel
STL function library
合约锁仓漏洞
Cmake basic tutorial (39) pkgconfig
(11) Vscode code formatting configuration
Solve the problem of JS calculation accuracy