当前位置:   article > 正文

多入口BFS(上下左右)模板_bfs 上下左右 csdn

bfs 上下左右 csdn

public static void bfs(char[][] grid, boolean[][] visit, int i, int j) {
		Queue<int[]> queue = new LinkedList<int[]>();

		queue.add(new int[] { i, j });

		int[][] directions = new int[][] { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };

		while (queue.size() > 0) {
			int size = queue.size();
			int[] val = queue.poll();

			for (int[] dir : directions) {
				int row = val[0] + dir[0];
				int col = val[1] + dir[1];

				boolean index = (row < grid.length) && (col < grid[0].length) && (row >= 0) && (col >= 0);

				if (index && !visit[row][col] && grid[row][col] == '1') {
					visit[row][col] = true;
					queue.add(new int[] { row, col });
				}
			}
		}

		result += 1;
	}
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/477065
推荐阅读
相关标签
  

闽ICP备14008679号