From d62398e7c42033dc1773a773636218f93c305d85 Mon Sep 17 00:00:00 2001 From: OGGGXYT <142927398+OGGGXYT@users.noreply.github.com> Date: Thu, 6 Mar 2025 21:30:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Update=200099.=E5=B2=9B=E5=B1=BF=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E5=B9=BF=E6=90=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...346\225\260\351\207\217\345\271\277\346\220\234.md" | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index f8c36a00a8..255c045c61 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -195,9 +195,17 @@ import java.util.*; public class Main { public static int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};//下右上左逆时针遍历 + public static class pair{ + public int first; + public int second; + public pair(int x,int y){ + this.first=x; + this.second=y; + } + } public static void bfs(int[][] grid, boolean[][] visited, int x, int y) { - Queue queue = new LinkedList();//定义坐标队列,没有现成的pair类,在下面自定义了 + Queue queue = new LinkedList();//定义坐标队列,没有现成的pair类,在上面自定义了 queue.add(new pair(x, y)); visited[x][y] = true;//遇到入队直接标记为优先, // 否则出队时才标记的话会导致重复访问,比如下方节点会在右下顺序的时候被第二次访问入队 From bf4ee70f7ed3834dfafac82f8d24a0fb3f8a8dde Mon Sep 17 00:00:00 2001 From: OGGGXYT <142927398+OGGGXYT@users.noreply.github.com> Date: Thu, 6 Mar 2025 21:54:06 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=200099.=E5=B2=9B=E5=B1=BF=E7=9A=84?= =?UTF-8?q?=E6=95=B0=E9=87=8F=E5=B9=BF=E6=90=9C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 不使用pair,而是使用两个队列分别存储横纵坐标 --- ...60\351\207\217\345\271\277\346\220\234.md" | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" index 255c045c61..65e5b37db5 100644 --- "a/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" +++ "b/problems/kamacoder/0099.\345\262\233\345\261\277\347\232\204\346\225\260\351\207\217\345\271\277\346\220\234.md" @@ -195,32 +195,27 @@ import java.util.*; public class Main { public static int[][] dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};//下右上左逆时针遍历 - public static class pair{ - public int first; - public int second; - public pair(int x,int y){ - this.first=x; - this.second=y; - } - } - + public static void bfs(int[][] grid, boolean[][] visited, int x, int y) { - Queue queue = new LinkedList();//定义坐标队列,没有现成的pair类,在上面自定义了 - queue.add(new pair(x, y)); + Queue queueX = new LinkedList();//定义两个队列分别存储横纵坐标 + Queue queueY = new LinkedList(); + queueX.add(x); + queueY.add(y); visited[x][y] = true;//遇到入队直接标记为优先, // 否则出队时才标记的话会导致重复访问,比如下方节点会在右下顺序的时候被第二次访问入队 - while (!queue.isEmpty()) { - int curX = queue.peek().first; - int curY = queue.poll().second;//当前横纵坐标 + while (!queueX.isEmpty()) { + int curX = queueX.poll(); + int curY = queueY.poll();//当前横纵坐标,并拿出队列 for (int i = 0; i < 4; i++) { - //顺时针遍历新节点next,下面记录坐标 + //依次遍历新节点next,下面记录坐标 int nextX = curX + dir[i][0]; int nextY = curY + dir[i][1]; if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length) { continue; }//去除越界部分 if (!visited[nextX][nextY] && grid[nextX][nextY] == 1) { - queue.add(new pair(nextX, nextY)); + queueX.add(nextX); + queueY.add(nextY); visited[nextX][nextY] = true;//逻辑同上 } }