Java实现-岛屿的个数

public class Solution {
    /**
     * @param grid a boolean 2D matrix
     * @return an integer
     */
    public int numIslands(boolean[][] grids) {
        // Write your code here
        if(grids.length==0){
			return 0;
		}
		int [][]grid=new int[grids.length][grids[0].length];
		for(int i=0;i<grids.length;i++){
			for(int j=0;j<grids[0].length;j++){
				if(grids[i][j]){
					grid[i][j]=1;
				}else{
					grid[i][j]=0;
				}
			}
		}
		ArrayList<Point> list=new ArrayList<Point>();
		int count=0;
        for(int i=0;i<grid.length;i++){
        	for(int j=0;j<grid[0].length;j++){
        		Point p=new Point(grid[i][j], 0,1);
        		if(grid[i][j]==1){
        			if(list.isEmpty()){
        				p.where=++count;
        				list.add(p);
        			}else{
        				if(i>0){
        					Point temp=list.get(list.size()-grid[0].length);
        					if(temp.where>0){
        						p.where=temp.where;
        					}
        				}
        				if(j>0){
        					Point temp=list.get(list.size()-1);
        					if(temp.where>0){
        						if(p.where>0){
        							if(p.where>temp.where){
        								for(int k=0;k<list.size();k++){
        									if(list.get(k).where==p.where){
        										list.get(k).where=temp.where;
        									}
        								}
        								count--;
        								p.where=temp.where;
        							}else if(p.where<temp.where){
        								for(int k=0;k<list.size();k++){
        									if(list.get(k).where==temp.where){
        										list.get(k).where=p.where;
        									}
        								}
        								p.where=p.where;
        								count--;
        							}else{
        								p.where=temp.where;
        							}
        						}else{
        							p.where=temp.where;
        						}
        					}
        				}
        				if(p.where>0){
        					list.add(p);
//        					System.out.println("***************"+i+" "+j+"   "+p.where);
        				}else{
        					p.where=++count;
//        					System.out.println("----------"+i+" "+j+"   "+p.where);
        					list.add(p);
        				}
        			}
        		}else{
        			list.add(p);
        		}
        	}
        }
		return count;

    }
}
class Point{
	int value;//值
	int where;//属于哪一个类0
	int visited;//是否访问过
	public Point(int value,int where,int visited){
		this.value=value;
		this.where=where;
		this.visited=visited;
	}
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	public int getWhere() {
		return where;
	}
	public void setWhere(int where) {
		this.where = where;
	}
	public int getVisited() {
		return visited;
	}
	public void setVisited(int visited) {
		this.visited = visited;
	}
	
}



生命游戏是一个经典的二维细胞自动机模型,因为它展现了自组织、演化、易扩展等特点,并可以用于生物、计算机科学、数学等多个领域。 以下是使用C语言实现生命游戏的代码: #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROWS 20 #define COLS 50 void init_board(int board[ROWS][COLS]) { int i, j; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { board[i][j] = rand() % 2; } } } void show_board(int board[ROWS][COLS]) { int i, j; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { if (board[i][j] == 1) { printf("#"); } else { printf("."); } } printf("\n"); } } int count_neighbors(int board[ROWS][COLS], int row, int col) { int count = 0, i, j; for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { if (i == 0 && j == 0) continue; if (row+i < 0 || row+i >= ROWS) continue; if (col+j < 0 || col+j >= COLS) continue; count += board[row+i][col+j]; } } return count; } void update_board(int board[ROWS][COLS]) { int new_board[ROWS][COLS]; int i, j, count; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { count = count_neighbors(board, i, j); if (board[i][j] == 1) { if (count < 2 || count > 3) { new_board[i][j] = 0; } else { new_board[i][j] = 1; } } else { if (count == 3) { new_board[i][j] = 1; } else { new_board[i][j] = 0; } } } } for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { board[i][j] = new_board[i][j]; } } } int main() { int board[ROWS][COLS]; srand(time(NULL)); init_board(board); while (1) { show_board(board); update_board(board); printf("\033[2J\033[H"); } return 0; } 该代码使用随机数初始化了一个20行50列的细胞模型图,并重复显示,模拟生命游戏的进化过程。 注意:本AI只提供技术分享,不参与任何违法活动,请用户合法使用技术。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Narasimha_Karumanchi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值