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);
}
}