房屋群
来源:互联网 发布:acm网络赛 编辑:程序博客网 时间:2024/04/25 16:53
//返回村庄的总数import java.util.LinkedList;import java.util.Queue;public class Test { public static void main(String[] args) { int[][] map = {{0, 1, 0,1}, {0, 1, 0, 0}, {0, 1, 1, 0}, {1, 0, 0, 1}}; System.out.println(solve(map)); } static int solve(int[][] grid) { int cnt = 0; int row = grid.length; int col = grid[0].length; used = new int[row][col]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (grid[i][j] == 1 && used[i][j] == 0) { bfs(grid, i, j, row, col); cnt++; } } } return cnt; } static class Node { int x; int y; public Node(int x, int y) { this.x = x; this.y = y; } } static int[][] used; static int[][] d = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; static void bfs(int[][] map, int x, int y, int n, int m) { Queue<Node> q = new LinkedList<Node>(); Node first = new Node(x, y); q.add(first); used[x][y] = 1; while (!q.isEmpty()) { Node head = q.poll(); int xx = head.x; int yy = head.y; for (int i = 0; i < 4; i++) { int px = xx + d[i][0]; int py = yy + d[i][1]; if (px < 0 || px >= n || py < 0 || py >= m || used[px][py] == 1 || map[px][py] == 0) continue; q.add(new Node(px, py)); used[px][py] = 1; } } }}//返回最大的村庄数。。。操,这个笔试的时候没有ac,差一点import java.util.LinkedList;import java.util.Queue;public class Test { public static void main(String[] args) { int[][] map = {{1, 1, 0,0}, {0, 1, 0, 0}, {0, 1, 1, 0}, {1, 0, 0, 1}}; System.out.println(solve(map)); } static int solve(int[][] grid) { int max=0; int cnt = 0; int row = grid.length; int col = grid[0].length; used = new int[row][col]; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { if (grid[i][j] == 1 && used[i][j] == 0) { int cou= bfs(grid, i, j, row, col,1); if(max<=cou){ max=cou; } } } } return max; } static class Node { int x; int y; public Node(int x, int y) { this.x = x; this.y = y; } } static int[][] used; static int[][] d = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; static int bfs(int[][] map, int x, int y, int n, int m,int count) { Queue<Node> q = new LinkedList<Node>(); Node first = new Node(x, y); q.add(first); used[x][y] = 1; while (!q.isEmpty()) { Node head = q.poll(); int xx = head.x; int yy = head.y; for (int i = 0; i < 4; i++) { int px = xx + d[i][0]; int py = yy + d[i][1]; if (px < 0 || px >= n || py < 0 || py >= m || used[px][py] == 1 || map[px][py] == 0) continue; q.add(new Node(px, py)); used[px][py] = 1; count++; } } return count; }}
0 0
- 房屋群
- 百度笔试-房屋群
- 百度笔试-房屋群
- 房屋建造
- 房屋验收
- 大修房屋
- 房屋文学
- 房屋质量
- 打劫房屋
- 房屋染色
- 打劫房屋
- 房屋网站
- 打劫房屋
- 房屋图片
- 房屋染色
- 房屋染色
- 房屋建造
- 打劫房屋 II和打劫房屋 III
- UGI GUI API
- 项目管理十大知识领域和47个过程
- 浅谈CSS在前端优化中一些值得注意的关键点
- Android 浅谈 Activity (下)
- Unreal 制作国家象棋的视频教程
- 房屋群
- 返回指定的字符串首次出现的位置
- 字符串的包含问题
- Ubuntu安装搜狗输入法
- 15章实例9
- 测试驱动开发
- (IOS开发)使用afn往服务器传图片
- makefile 的编写
- 整理了一些安卓的activity启动常量