矩阵相关

本文介绍了一种基于广度优先搜索的 Flood Fill 算法实现,该算法用于将二维矩阵中指定起始点及其周围相同颜色的像素转换为另一种颜色。通过具体的 Java 实现展示了算法的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a screen with all pixels having one of two colors. Now I will click on a random pixel.
Then that pixel & all the adjacent pixels with same color should change the color to the second color.

adjacent = vertically or horizontally above or blow.

package com.zhuyu_deng.test;

public class Test
{

	private static void printMatrix(char[][] a)
	{
		for (int i = 0; i < a.length; ++i)
		{
			for (int j = 0; j < a[i].length; ++j)
			{
				System.out.print(a[i][j] + "  ");
			}
			System.out.println();
		}
	}
//	关于FloodFill算法的解释
//	以下就是源程序 事实上就是广度搜索
//	但是必须要建立一个所谓的方向数组
	private static void floodFill(char[][] a, int x, int y)
	{
		class Node
		{
			public Node(int x, int y)
			{
				this.x = x;
				this.y = y;
			}
			int x;
			int y;
		}

		int px[] = new int[] {0, 0, -1, 1};
		int py[] = new int[] {-1, 1, 0, 0};
		char orgColor = a[x][y];
		char revColor = a[x][y] == 'W' ? 'B' : 'W';
		int size = 0;
	
		Node stack[] = new Node[a.length * a[0].length];
		stack[size] = new Node(x, y);
		while (size >= 0)
		{
			Node cur = stack[size];
			size--;
			a[cur.x][cur.y] = revColor;
			
			for (int i = 0; i < 4; ++i)
			{
				int xx = cur.x + px[i];
				int yy = cur.y + py[i];
				if (xx >= 0 && xx < a.length && yy >= 0 && yy < a[0].length)
				{
					if (a[xx][yy] == orgColor)
						stack[++size] = new Node(xx, yy);
				}
			}
			
		}
		
	}

	public static void main(String args[])
	{
		char a[][] = new char[][] { { 'W', 'B', 'W', 'W', 'B', 'W' },
				{ 'B', 'B', 'W', 'W', 'B', 'W' },
				{ 'W', 'B', 'B', 'B', 'W', 'B' },
				{ 'W', 'B', 'W', 'W', 'B', 'B' } };
		
		
		printMatrix(a);
		floodFill(a, 2, 2);

		System.out.println();
		printMatrix(a);
		
	}
}



Edit: Question seem to be not clear to some ppl. Giving an ex:

Given below & clicked on 2nd row, 2nd col
W B W W B W
B B W W B W
W B B B W B
W B W W B B

Covert to
W W W W B W
W W W W B W
W W W W W B
W W W W B B


package com.zhuyu_deng.test;

import java.util.Stack;

public class Test
{

	private static void printMatrix(char[][] a)
	{
		for (int i = 0; i < a.length; ++i)
		{
			for (int j = 0; j < a[i].length; ++j)
			{
				System.out.print(a[i][j] + "  ");
			}
			System.out.println();
		}
	}
	
	private static void floodFill(char[][] a, int x, int y)
	{
		class Node
		{
			public Node(int x, int y)
			{
				this.x = x;
				this.y = y;
			}

			int x;
			int y;
		}

		char orgColor = a[x][y];
		char revColor = a[x][y] == 'W' ? 'B' : 'W';
		int size = 0;
	
		Node stack[] = new Node[a.length * a[0].length];
		stack[size] = new Node(x, y);
		while (size >= 0)
		{
			Node cur = stack[size];
			size--;
			a[cur.x][cur.y] = revColor;
			if (cur.x - 1 >= 0 && orgColor == a[cur.x - 1][cur.y])
			{
				stack[++size] = (new Node(cur.x-1, cur.y));
			}
			if (cur.x + 1 < a.length && orgColor == a[cur.x + 1][cur.y])
			{
				stack[++size] = (new Node(cur.x+1, cur.y));
			}
			if (cur.y - 1 >= 0 && orgColor == a[cur.x][cur.y - 1])
			{
				stack[++size] = (new Node(cur.x, cur.y-1));
			}
			if (cur.y + 1 < a[0].length && orgColor == a[cur.x][cur.y + 1])
			{
				stack[++size] = (new Node(cur.x, cur.y + 1));
			}
		}
		
	}

	public static void main(String args[])
	{
		// int[] a = {-2,11,-4,13,-5,-2};
		int[][] b = { { 0, -2, -7, 0 }, { 9, 2, -6, 2 }, { -4, 1, -4, 1 },
				{ -1, 8, 0, -2 } };
		int[][] matrix = { { 2, 3, 4, 1 }, { 1, 1, 3, 9 }, { 2, 2, 3, 1 },
				{ 2, 2, 3, 1 } };
		char a[][] = new char[][] { { 'W', 'B', 'W', 'W', 'B', 'W' },
				{ 'B', 'B', 'W', 'W', 'B', 'W' },
				{ 'W', 'B', 'B', 'B', 'W', 'B' },
				{ 'W', 'B', 'W', 'W', 'B', 'B' } };
		
		
		printMatrix(a);

		floodFill(a, 2, 2);

		System.out.println();

		printMatrix(a);
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值