Graffiti intelligent autumn stroke test questions, many algorithm questions have been learned in college, but at that time, they didn't fully contact the front end. In addition, they didn't review and return them to the teacher. They really hate food when books are used!
Problem description
Description
An oil exploration company is exploring underground oil field resources as planned, working in a rectangular area. They first divided the area into many small square areas, and then used detection equipment to detect whether there is oil in each small square area. If oil is detected in a small square area, mark it as' @ ', otherwise mark it as' *'. If both adjacent areas are 1, they belong to the same oil belt. An oil belt may contain many small square areas, and your task is to determine how many oil belts there are in a rectangular area. The so-called adjacency means that the two small square areas are '@' up and down, left and right, upper left and lower right or lower left and upper right.
Input
The input data will contain some rectangular region data. The first row of each region data has two positive integers m and N, indicating that the region is composed of mn small squares. If M is 0, it means that all inputs are over; Otherwise, the following M (1 ≤ m ≤ 100) lines of data have n (1 ≤ n ≤ 100) characters, and each character is' @ 'or' ', indicating oil or no oil. The number of '@' in each rectangular region will not exceed 100.
Output
For each rectangular region, the number of output oil belts, and each oil belt value occupies an independent line. The oil band value will not exceed 100.
JavaScript solution
code
function dfs(grid, newGrid, x, y, direction, count) { for (let i = 0; i < direction.length; i++) { const nextX = x + direction[i][0]; // nextX: the x coordinate of the next step const nextY = y + direction[i][1]; // nextY: the y coordinate of the next step if ( nextX >= 0 && nextX < grid.length && nextY >= 0 && nextY < grid[0].length && grid[nextX][nextY] === "@" && !newGrid[nextX][nextY] ) { // Boundary judgment + branch reduction (the next step of traversal is performed after the operation is met) newGrid[nextX][nextY] = count; dfs(grid, newGrid, nextX, nextY, direction, count); } } } function foo(grid) { const row = grid.length; // Gets the number of rows passed into the two-dimensional array const cloum = grid[0].length; // Gets the number of columns passed into the two-dimensional array let count = 0; // number const direction = [ [0, 1], [1, 0], [0, -1], [-1, 0], ]; const newGrid = JSON.parse( JSON.stringify(Array(row).fill(Array(cloum).fill(0))) ); // You must do this (or complete another deep copy function) newGrid[0][0] = 1; for (let i = 0; i < row; i++) { for (let j = 0; j < cloum; j++) { if (grid[i][j] === "@" && !newGrid[i][j]) { // Traverse each node count++; newGrid[i][j] = count; dfs(grid, newGrid, i, j, direction, count); } } } return count; } // test data const grid = [ ["@", "@", "@", "*", "*"], ["@", "@", "@", "*", "@"], ["*", "@", "*", "*", "*"], ["*", "@", "*", "*", "@"], ["*", "@", "@", "*", "*"], ]; console.log(foo(grid));
matters needing attention
When defining the two-dimensional array newGrid, I directly:
const newGrid = Array(row).fill(Array(cloum).fill(0))
However, during the test results, it was found that:
newGrid[0][0] = 1;
console.log(newGrid);
The print result is:
[[1,0,0,0,0],[1,0,0,0,0],[1,0,0,0,0],[1,0,0,0,0],[1,0,0,0,0]]
In other words, the array elements in the two-dimensional array newGrid are the same array, but what we need is that each sub element array is independent
Then it can be solved as follows:
const newGrid = JSON.parse(JSON.stringify(Array(row).fill(Array(cloum).fill(0))));