刷题——surrounded-regions

/*
 * 题目描述

Given a 2D board containing'X'and'O', capture all regions surrounded by'X'.
A region is captured by flipping all'O's into'X's in that surrounded region .
For example,
X X X X
X O O X
X X O X
X O X X

After running your function, the board should be:
X X X X
X X X X
X X X X
X O X X
 */
public class SurroundedRegions {
	public void visit(int row,int col,char[][] board,boolean[][] flags,boolean[][] visited){
		if (board[row][col]=='O' && visited[row][col]==false) {//可以进行游走
			visited[row][col] = true;//访问过之后置标志
			flags[row][col] = true;//与边界相连标志
			//左边
			if (col-1>=0) {
				visit(row,col-1,board,flags,visited);
			}
			//上边
			if (row-1>=0) {
				visit(row-1,col,board,flags,visited);
			}
			//右边
			if (col+1<=board[0].length-1) {
				visit(row,col+1,board,flags,visited);
			}
			//下边
			if (row+1<=board.length-1) {
				visit(row+1,col,board,flags,visited);
			}
		}
	}
	
	public void solve(char[][] board) {
		if (board==null || board.length==0) {
			return;
		}
		int rows = board.length;
		int cols = board[0].length;
        boolean[][] flags = new boolean[rows][cols];
        boolean[][] visited = new boolean[rows][cols];
        //进行游走,第一行与最后一行
        for (int i = 0; i < cols; i++) {
			visit(0,i,board,flags,visited);
			visit(rows-1,i,board,flags,visited);
		}
      //进行游走,第一列与最后一列
        for (int i = 0; i < rows; i++) {
			visit(i,0,board,flags,visited);
			visit(i,cols-1,board,flags,visited);
		}
        for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				if (flags[i][j]) {
					board[i][j]='O';
				}else {
					board[i][j]='X';
				}
				
			}
		}
    }
	public static void main(String[] args) {
		char[][] board ={{'X','X','X','X','X','X','X','X'},
				{'O','O','X','X','O','O','O','X'},
				{'O','X','X','X','O','X','O','X'},
				{'O','O','O','X','O','O','O','X'},
				{'X','X','X','X','X','X','X','X'}};
		/*char[][] board ={{'X','O','X','O','X','O'},
				{'O','X','O','X','O','X'},
				{'X','O','X','O','X','O'},
				{'O','X','O','X','O','X'}};*/
		new SurroundedRegions().solve(board);
		int rows = board.length;
		int cols = board[0].length;
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				System.out.print(board[i][j]+" ");
			}
			System.out.println();
		}
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,您要求的内容需要用英文进回答。 以下是一篇生物传记散文,长度约为200-300字: Born in a small town in the countryside, Jane Doe grew up surrounded by nature and animals. She developed a love for the outdoors and a deep appreciation for the natural world at a young age. Her parents, both teachers, encouraged her interests and provided her with the resources she needed to pursue her passions. As Jane grew older, her love for the environment only grew stronger. She studied environmental science in college and went on to earn a master's degree in ecology. Throughout her academic career, Jane worked tirelessly to learn as much as she could about the world around her and to find ways to protect it. After graduation, Jane took a job with a local non-profit organization that focused on preserving wildlife habitats. She spent years working on the ground, conducting research, and advocating for the protection of endangered species. Her work took her all over the world, from the rainforests of South America to the savannas of Africa. Despite the challenges she faced, Jane never lost sight of her goals. Her tireless efforts and unwavering dedication to the cause earned her recognition and respect within the environmental community. She became a respected voice on environmental issues and an inspiring leader for those who shared her passion for preserving the natural world. Today, Jane continues to work tirelessly to protect the environment and raise awareness about the importance of conservation. Her life's work serves as a testament to the power of one person to make a difference and her legacy will live on for generations to come.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值