I have created a program that takes in a test case unfinished Sudoku puzzle and is supposed to use multi-threading to quickly solve it. I have run into a problem where it only works 1/3 times, and when it does "work", it returns a puzzle that breaks rules. Such as, two fives in the same column. I am wondering if the threads are acting too fast and not communicating? What are things I can change or problems I am not seeing?
Here is my SudokuSolver class. It contains two methods: cellChecker and fillPuzzle. The cell checker checks individual cells and returns false when their filled or true if the cell is empty (contains a 0). It also contains the code for the multi-threading, where I suspect the problem is.
fillPuzzle simply creates the puzzle if needed, and uses cellChecker to correctly solve the sudoku puzzle.
public boolean cellChecker(int puzzle[][], int row, int col, int number) {
//Set the cellCheck boolean to true for a default case
cellCheck = true;
//variables that give us the start of the row and column respectively
//If it doesn't get divided by 3 and then multiplied by 3, the threads
//get an out of bounds exception and gets real mad. found this math
//from stackoverflow.
int rowStart = (row / 3) * 3;
int colStart = (col / 3) * 3;
//The threads are created to do the 9x9 grid in 3x3 sections,
//so it can get done quicker and cut down the large task at hand.
//checks via col
Thread thread1 = new Thread(new Runnable() {
public void run() {
//for loop that checks the col
for (int i = 0; i < 9; i++) {
if (puzzle[row][i] == number) {
cellCheck = false;
}
}
}
});
thread1.start();
//another thread to take on it's 3x3 part via row
Thread thread2 = new Thread(new Runnable() {
public void run() {
//for loop that checks the row
for (int i = 0; i < 9; i++) {
if (puzzle[i][col] == number) {
cellCheck = false;
}
}
}
});
thread2.start();
//another thread to take on it's 3x3 part via cell by cell
Thread thread3 = new Thread(new Runnable() {
public void run() {
//for loop that checks the specific cell
for (int rowCell = rowStart; rowCell < rowStart + 3; rowCell++) {
for (int colCell = 0; colCell < colStart + 3; colCell++) {
if (puzzle[rowCell][colCell] == number) {
cellCheck = false;
}
}
}
}
});
thread3.start();
//returns the boolean whether the box is empty or not. If it is false,
//it is filled with the number it needs. if it's true, it still needs
//a number
return cellCheck;
}
public boolean fillPuzzle(int puzzle[][], int row, int col) {
if (row < 9) {
//if the 9x9 puzzle is not created and filled with 0's, create it
if (puzzle[row][col] != 0) {
if (col < 8) {
//returns created 9x9 puzzle
return fillPuzzle(puzzle, row, col + 1);
} else if (row < 8) {
//returns created 9x9 puzzle
return fillPuzzle(puzzle, row + 1, 0);
}
//already created, return true!
return true;
} else {
//if the puzzle is created, go through and check for the number
//up until or equal to 9 because sudoku goes from 1-9
//once it finds the number that belongs in the cell, it
//puts it in that cell
for (int i = 1; i <= 9; i++) {
if (cellChecker(puzzle, row, col, i)) {
puzzle[row][col] = i;
//If at the end of col, add a row to finish the table
if (col == 8) {
if (fillPuzzle(puzzle, row + 1, 0)) {
return true;
}
} else {
//add 1 to the col if it doesn't == 8 because
//that means there's not enough cols
if (fillPuzzle(puzzle, row, col + 1)) {
return true;
}
}
//all else fails, set it to 0 and we'll check it later
puzzle[row][col] = 0;
}
}
return false;
}
}
return true;
}
and this is my menu class where the puzzle test case occurs
public class Menu {
private SudokuSolver solver = new SudokuSolver();
//This is a test case sudoku puzzle I found off the internet to use as
//an example
private int sudokuPuzzle[][] = {{8, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 3, 6, 0, 0, 0, 0, 0},
{0, 7, 0, 0, 9, 0, 2, 0, 0},
{0, 5, 0, 0, 0, 7, 0, 0, 0},
{0, 0, 0, 0, 4, 5, 7, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 3, 0},
{0, 0, 1, 0, 0, 0, 0, 6, 8},
{0, 0, 8, 5, 0, 0, 0, 1, 0},
{0, 9, 0, 0, 0, 0, 4, 0, 0}};
public void start() {
//set to the method fillPuzzle. It tries to solve the
//sudoku puzzle and if it does, it is set to true. If it doesn't
//it says no thank you.
boolean filledPuzzle = solver.fillPuzzle(sudokuPuzzle, 0, 0);
//if it filled the puzzle, it prints it out.
if (filledPuzzle) {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
System.out.print(sudokuPuzzle[row][col] + " ");
}
System.out.println();
}
//if it can't, it let's the person know.
} else {
System.out.println("Solution not found");
}
}
}
Finally, this may sound dumb but I'm not 100% sure how implements Runnable works, but it did give me an empty run method in my sudokusolver class and gets angry if I remove it. I was wondering if something needs to be put there for it to work correctly and efficiently?
#Override
public void run() {
}
Related
I have a 2D Array:
{0, 0, 1, 1, 1},
{0, 0, 1, 1, 1},
{0, 0, 1, 1, 1},
{0, 1, 0, 0, 1}
and I am trying to see which row has the closest 1 to the left, in this case it is index 3. So we have to return 3 to console, but I don't know how to go about comparing the values, etc.
This is what I have tried so far:
int array[][] = {
{0, 0, 1, 1, 1},
{0, 0, 1, 1, 1},
{0, 0, 1, 1, 1},
{0, 1, 0, 0, 1}
};
int count = 0;
Map<Integer, Integer> countMap = new HashMap<>();
for(int i = 0; i < array.length; i++){
for(int j = 0; j < array[i].length; j++){
if(array[i][j] != 1){
count++;
}else{
System.out.println("index of i: " + i + ", index of j:" + j + ", count: " + count);
countMap.put(j, count);
count = 0;
break;
}
}
}
System.out.println();
You can try to iterate in a column wise manner and return result as soon as you encounter the first '1'. This way you don't need to spend extra computation iterating through the whole 2D matrix.
public class ClosestOne {
public static int closestOne(int[][] a) {
for(int i = 0; i < a[0].length; i++) {
for(int j = 0; j < a.length; j++) {
if(a[j][i] == 1) {
return j;
}
}
}
return -1;
}
public static void main(String[] args) {
int array[][] = {
{0, 0, 1, 1, 1},
{0, 0, 1, 1, 1},
{0, 0, 1, 1, 1},
{0, 1, 0, 0, 1}
};
int closestRow = closestOne(array);
if(closestRow == -1) {
System.out.println("'1' is not present in matrix!");
} else {
System.out.println("Closest '1' is in row: " + closestRow );
}
}
}
Using a HashMap is completely over-engineered here. Theoretically you do not need any kind of data structure since you calculate the values row-by-row anyways, but since you were using one, I thought I would use one, too.
Use a simple array instead of the Map, with the length of the original array (i.e. the rows). Fill that array with a number that is higher than any result can be, to be safe I used Integer.MAX_VALUE.
int[] firstIndex = new int[array.length];
//for all i in length of array:
firstIndex[i] = Integer.MAX_VALUE;
No iterate though your your 2d array like you are and overwrite the firstIndex if you find something that is better than the current value.
//for every row
//for ever column (col)
if (array[row][col] == 1 && col < firstIndex[row]) {
firstIndex[row] = col;
}
Then in the end look for the minimum value in that array.
int min = firstIndex[0];
for (int row = 1; row < firstIndex.length; row++) {
min = Integer.min(min, firstIndex[row]);
}
System.out.println(min);
It looks like you have already worked out the count to the first 1 on each row, you just need to decide which is the lowest after completing your loop. You don't need a map to do that, just check if the current row is better than the "best so far" and if it is then update the best.
There is actually no need for a count variable since j is the same thing, and i is the row.
int best = -1; // Initialize to -1 which is an invalid index
int bestCount = Integer.MAX_VALUE; // Initialize to a very bad best count.
for(int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if (array[i][j] == 1 && j < bestCount) {
best = i; // the row
bestCount = j; // the '1'
break;
}
}
}
System.out.println("Closest 1 to left in row " + best);
I have this island program that just displays the number of possible islands in a matrix.
I have a matrix of 1's and 0's I want to get group of 1's in row wise, column wise and diagonal wise. This program count the number of possibilities and returns 5 as output for below input.
{ 1, 0, 0, 1, 0 },
{ 1, 0, 1, 0, 0 },
{ 0, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1 }
But I need output as island sizes in increasing order as 1 2 2 4
How to achieve this?
Explanation: a) last row first column has single island, b) first row 1st column, 2nd row 1st column and has island of size 2. c) 3rd row last column, last row last column has island of size 2. d) 1st row 4th column, remaining rows 3rd column has size of 4.
public class Islands {
// No of rows and columns
static final int ROW = 4, COL = 5;
// A function to check if a given cell (row, col) can
// be included in DFS
static boolean isSafe(int M[][], int row, int col, boolean visited[][]) {
// row number is in range, column number is in range
// and value is 1 and not yet visited
return (row >= 0) && (row < ROW) && (col >= 0) && (col < COL) && (M[row][col] == 1 && !visited[row][col]);
}
// A utility function to do DFS for a 2D boolean matrix.
// It only considers the 8 neighbors as adjacent vertices
static void DFS(int M[][], int row, int col, boolean visited[][]) {
// These arrays are used to get row and column numbers
// of 8 neighbors of a given cell
int rowNbr[] = new int[] { -1, -1, -1, 0, 0, 1, 1, 1 };
int colNbr[] = new int[] { -1, 0, 1, -1, 1, -1, 0, 1 };
// Mark this cell as visited
visited[row][col] = true;
// Recur for all connected neighbours
for (int k = 0; k < 8; ++k)
if (isSafe(M, row + rowNbr[k], col + colNbr[k], visited))
DFS(M, row + rowNbr[k], col + colNbr[k], visited);
}
// The main function that returns count of islands in a given
// boolean 2D matrix
static int countIslands(int M[][]) {
// Make a bool array to mark visited cells.
// Initially all cells are unvisited
boolean visited[][] = new boolean[ROW][COL];
// Initialize count as 0 and travese through the all cells
// of given matrix
int count = 0;
for (int i = 0; i < ROW; ++i)
for (int j = 0; j < COL; ++j)
if (M[i][j] == 1 && !visited[i][j]) // If a cell with
{ // value 1 is not
// visited yet, then new island found, Visit all
// cells in this island and increment island count
DFS(M, i, j, visited);
++count;
}
return count;
}
// Driver method
public static void main(String[] args) throws java.lang.Exception {
int M[][] = new int[][] { { 1, 0, 0, 1, 0 }, { 1, 0, 1, 0, 0 }, { 0, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1 } };
System.out.println("Number of islands is: " + countIslands(M));
}
}
Make following changes to solve it:
Add static variable size to count size for each island.
Replace count by listSizes to store the size of each island. New island's size is added to this list.
Invoke the method (renamed from countIslands to sizeIslands) to get final listSizes.
Sort the list before printing it out.
Final code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Islands {
// No of rows and columns
static final int ROW = 4, COL = 5;
static int size;
// A function to check if a given cell (row, col) can
// be included in DFS
static boolean isSafe(int M[][], int row, int col, boolean visited[][]) {
// row number is in range, column number is in range
// and value is 1 and not yet visited
return (row >= 0) && (row < ROW) && (col >= 0) && (col < COL) && (M[row][col] == 1 && !visited[row][col]);
}
// A utility function to do DFS for a 2D boolean matrix.
// It only considers the 8 neighbors as adjacent vertices
static void DFS(int M[][], int row, int col, boolean visited[][]) {
// These arrays are used to get row and column numbers
// of 8 neighbors of a given cell
int rowNbr[] = new int[] { -1, -1, -1, 0, 0, 1, 1, 1 };
int colNbr[] = new int[] { -1, 0, 1, -1, 1, -1, 0, 1 };
// Mark this cell as visited
visited[row][col] = true;
size++;
// Recur for all connected neighbours
for (int k = 0; k < 8; ++k)
if (isSafe(M, row + rowNbr[k], col + colNbr[k], visited))
DFS(M, row + rowNbr[k], col + colNbr[k], visited);
}
// The main function that returns size of islands in a given
// boolean 2D matrix
static List<Integer> sizeIslands(int M[][]) {
// Make a bool array to mark visited cells.
// Initially all cells are unvisited
boolean visited[][] = new boolean[ROW][COL];
// Initialize empty list and travese through the all cells
// of given matrix
List<Integer> listSizes = new ArrayList<Integer>();
for (int i = 0; i < ROW; ++i)
for (int j = 0; j < COL; ++j)
if (M[i][j] == 1 && !visited[i][j]) // If a cell with
{ // value 1 is not
// visited yet, then new island found, Visit all
// cells in this island and increment island count
size = 0;
DFS(M, i, j, visited);
listSizes.add(size);
}
return listSizes;
}
// Driver method
public static void main(String[] args) throws java.lang.Exception {
int M[][] = new int[][] { { 1, 0, 0, 1, 0 }, { 1, 0, 1, 0, 0 }, { 0, 0, 1, 0, 1 }, { 1, 0, 1, 0, 1 } };
List<Integer> list = sizeIslands(M);
Collections.sort(list);
System.out.println("Sizes of islands are: " + list);
}
When i tried to check the rows of the Sudoku for duplicate elements, nothing is returned as result. Don't know whether the code is right or not. If the duplicate element found in a row, then need to print repetition found. Help me to reach the solution with using the method checkRow().
public class Sudoku {
public static void main(String... args) throws Exception
{
Scanner scanner = new Scanner(System.in);
int[][] sudokuPuzzle = {
{8, 1, 0, 0, 0, 0, 0, 3, 9},
{0, 0, 0, 9, 0, 1, 0, 0, 0},
{3, 0, 5, 0, 0, 0, 4, 0, 1},
{0, 0, 9, 8, 0, 2, 7, 0, 0},
{0, 0, 0, 5, 0, 6, 0, 0, 0},
{0, 0, 4, 3, 0, 7, 1, 0, 0},
{1, 0, 8, 0, 0, 0, 9, 0, 2},
{0, 0, 0, 6, 0, 4, 0, 0, 0},
{2, 4, 0, 0, 0, 0, 0, 6, 5}
};
printSudoku(sudokuPuzzle);
int count = countCellsToFill(sudokuPuzzle);
System.out.println("Number of cells have to filled: " + count);
}
public static void printSudoku(int[][] sudokuPuzzle)
{
for (int i = 0; i < sudokuPuzzle.length; i++)
{
if (i == 3 || i == 6)
System.out.println("------------------------");
for (int j = 0; j < sudokuPuzzle[i].length; j++)
{
System.out.format("%-2s", sudokuPuzzle[i][j]);
if (j == 2 || j == 5 )
System.out.print(" | ");
}
System.out.println();
}
}
public static int countCellsToFill(int[][] sudokuPuzzle){
int count=0;
for(int[] sudokuPuzzle1 : sudokuPuzzle){
for(int a: sudokuPuzzle1){
if(a == 0){
count++;
}
}
}
return count;
}
public boolean checkRow(int[] sudokuPuzzle){
for (int row = 0; row < sudokuPuzzle.length; row++)
{
int num = sudokuPuzzle[row];
if (num == sudokuPuzzle[row])
{
return true;
}
}
return false;
}
}
Take a moment and read your code:
public boolean checkRow(int[] sudokuPuzzle){
for (int row = 0; row < sudokuPuzzle.length; row++)
{
int num = sudokuPuzzle[row];
if (num == sudokuPuzzle[row])
{
return true;
}
}
return false;
}
What you are doing here is picking up the first number, "8" in this case, because it is on your position "0". Then, you check if that "8" is on your position "0", which of course, will be always true.
If you wanna check numbers per row, I suggest you to analize it with, maybe, a double for. So you can do your 9 lines, 9 numbers per line, and gotta have in mind don't compare the number you just read with himself.
By the way, if you are doing a Sudoku, you should check if that number appears in the first square as well. Sodukus are composed by 9 squares, you can not repeat numbers in rows, nor in columns, nor in squares.
That is an easy problem, struggle a bit!
Modified your checkRow method to check for duplicates -
public boolean checkRow(int[] sudokuPuzzle) {
for (int row = 0; row < sudokuPuzzle.length; row++) {
for (int i = 0; i < sudokuPuzzle.length; i++) {
if (i == row) {
continue;
}
if (sudokuPuzzle[i] == sudokuPuzzle[row]) {
return true;
}
}
}
return false;
}
I have an array something like the following.
int[][] myArray =
{{1, 2, 3, 0, 0, 1}
{1, 0, 4, 4, 0, 1}
{1, 2, 4, 3, 4, 0}
{2, 2, 0, 0, 2, 2}
{3, 0, 0, 3, 0, 0}
{4, 2, 3, 0, 0, 0}}
It would say that one had won because of the 1s in the three 1s in the first column. Two would not win because they are not in a "row".
I want to do some sort of win checking so that it finds three of the same number in a row, diagonal or column. Sort of like tic-tac-toe but with a larger grid. Before I used a messy set of if statements and goto statements. (It was written in Basic.) I have tried using a system where it found the directions from the last placed piece, in which there is a number of the same, but it didn't work properly. How can I do this in an easy and maintainable way?
Tried code:
private static boolean checkBoardCombinations(int[][] board, int inputX, int inputY) {
int currentPlayer = board[inputX-1][inputY-1];
boolean[][] directions = new boolean[3][3];
for(int y = 0; y >= -2; y--){
for(int x = 0; x >= -2; x--){
if(inputX+x >= 0 && inputX+x <= 7 && inputY+y >= 0 && inputY+y <= 7
&& (board[inputX+x][inputY+y] == currentPlayer)){
//System.out.printf("X: %s Y: %s", inputX+x, inputY+y);
directions[x+2][y+2] = true;
}
else{
directions[x+2][y+2] = false;
}
//System.out.printf("X: %s Y: %s B: %s,", inputX+x, inputY+y, directions[x+2][y+2]);
}
//System.out.println();
}
/*
for(int x = 0; x <= 2; x++){
for(int y = 0; y <= 2; y++){
System.out.print(directions[x][y] + " ");
}
System.out.println();
}
*/
return false;
}
Supposing the number of players are known, you can iterate over all the players one by one, and check if any player is forming a connection of required length or not.
Such code would look like following:
private int[][] grid; // Your array of size ROWS x COLUMNS
private final int ROWS = 6, COLUMNS = 6;
private final int CONSECUTIVE_CONNECTION_REQUIRED = 3;
// Returns true if given playerType is forming a connection, else false.
public boolean checkGrid(int playerType)
{
// Check downward
for (int i = 0; i <= ROWS - CONSECUTIVE_CONNECTION_REQUIRED; i++)
{
for (int j = 0; j < COLUMNS; j++)
{
int counter = 0;
for (int k = i; k < CONSECUTIVE_CONNECTION_REQUIRED + i; k++)
{
if (grid[k][j] == playerType)
counter++;
}
if (counter == CONSECUTIVE_CONNECTION_REQUIRED)
return true;
}
}
// Check across
for (int i = 0; i <= COLUMNS - CONSECUTIVE_CONNECTION_REQUIRED; i++)
{
for (int j = 0; j < ROWS; j++)
{
int counter = 0;
for (int k = i; k < CONSECUTIVE_CONNECTION_REQUIRED + i; k++)
{
if (grid[j][k] == playerType)
counter++;
}
if (counter == CONSECUTIVE_CONNECTION_REQUIRED)
return true;
}
}
// Check left to right diagonally
for (int i = 0; i <= ROWS - CONSECUTIVE_CONNECTION_REQUIRED; i++)
{
for (int j = 0; j <= COLUMNS - CONSECUTIVE_CONNECTION_REQUIRED; j++)
{
int counter = 0;
for (int k = i, m = j; k < CONSECUTIVE_CONNECTION_REQUIRED + i; k++, m++)
{
if (grid[k][m] == playerType)
counter++;
}
if (counter == CONSECUTIVE_CONNECTION_REQUIRED)
return true;
}
}
// Check right to left diagonally
for (int i = 0; i <= ROWS - CONSECUTIVE_CONNECTION_REQUIRED; i++)
{
for (int j = COLUMNS - 1; j >= COLUMNS - CONSECUTIVE_CONNECTION_REQUIRED; j--)
{
int counter = 0;
for (int k = i, m = j; k < CONSECUTIVE_CONNECTION_REQUIRED + i; k++, m--)
{
if (grid[k][m] == playerType)
counter++;
}
if (counter == CONSECUTIVE_CONNECTION_REQUIRED)
return true;
}
}
return false;
}
Where playerType is 0, 1, 2, 3 and so on...
You can use checkGrid() method like following:
for(int i = MIN_PLAYER_NUMBER; i <= MAX_PLAYER_NUMBER; i++)
{
if(checkGrid(i))
{
// Player i is forming the connection!!!
}
}
But if you don't want to iterate over your grid this many times, then drop your two dimensional array, and use a graph with adjacency list representation. Write a proper API for that which lets you make changes in your particular representation easily, and you can then find if any player is making a connection of particular length in the graph or not, in less iterations.
Although you already accepted an answer I thought to also submit you my answer for diversity :)
public static void main (String[] args)
{
int[][] myArray =
{{1, 2, 3, 0, 0, 1},
{1, 0, 4, 4, 0, 1},
{1, 2, 4, 3, 4, 0},
{2, 2, 0, 0, 2, 2},
{3, 0, 0, 3, 0, 0},
{4, 2, 3, 0, 0, 0}};
System.out.println(testForWinner(myArray));
}
/**
* Returns -1 if no winner
*/
static int testForWinner(int[][] ar) {
for(int i=0; i<ar.length; i++) {
for(int j=0; j<ar[i].length; j++) {
if(checkNext(ar, i, j, 0, 1, 1)) { //Check the element in the next column
return ar[i][j];
}
for(int k=-1; k<=1; k++) { //Check the three adjacent elements in the next row
if(checkNext(ar, i, j, 1, k, 1)) {
return ar[i][j];
}
}
}
}
return -1;
}
/**
* Step number `step` starting at `ar[i][j]` in direction `(di, dj)`.
* If we made 3 steps we have a winner
*/
static boolean checkNext(int[][] ar, int i, int j, int di, int dj, int step) {
if(step==3) {
return true;
}
if(i+di<0 || i+di>ar.length-1 || j+dj<0 || j+dj>ar[i].length-1) {
return false;
}
if(ar[i+di][j+dj]==ar[i][j]) {
return checkNext(ar, i+di, j+dj, di, dj, step+1);
}
return false;
}
See it in action: http://ideone.com/Ou2sRh
So I have a matrix of N×M. At a given position I have a value which represents a color. If there is nothing at that point the value is -1. What I need to do is after I add a new point, to check all his neighbours with the same color value and if there are more than 2, set them all to -1.
If what I said doesn't make sense what I'm trying to do is an algorithm which I use to destroy all the same color bubbles from my screen, where the bubbles are memorized in a matrix where -1 means no bubble and {0,1,2,...} represent that there is a bubble with a specific color.
Also, if you have any suggestions I'd be grateful. Thanks.
This is what I tried and failed:
public class Testing {
static private int[][] gameMatrix=
{{3, 3, 4, 1, 1, 2, 2, 2, 0, 0},
{1, 4, 1, 4, 2, 2, 1, 3, 0, 0},
{2, 2, 4, 4, 3, 1, 2, 4, 0, 0},
{0, 4, 2, 3, 4, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
};
static int Rows=6;
static int Cols=10;
static int count;
static boolean[][] visited=new boolean[15][15];
static int NOCOLOR = -1;
static int color = 1;
public static void dfs(int r, int c, int color, boolean set)
{
for(int dr = -1; dr <= 1; dr++)
for(int dc = -1; dc <= 1; dc++)
if(!(dr == 0 && dc == 0) && ok(r+dr, c+dc))
{
int nr = r+dr;
int nc = c+dc;
// if it is the same color and we haven't visited this location before
if(gameMatrix[nr][nc] == color && !visited[nr][nc])
{
visited[nr][nc] = true;
count++;
dfs(nr, nc, color, set);
if(set)
{
gameMatrix[nr][nc] = NOCOLOR;
}
}
}
}
static boolean ok(int r, int c)
{
return r >= 0 && r < Rows && c >= 0 && c < Cols;
}
static void showMatrix(){
for(int i = 0; i < gameMatrix.length; i++) {
System.out.print("[");
for(int j = 0; j < gameMatrix[0].length; j++) {
System.out.print(" " + gameMatrix[i][j]);
}
System.out.println(" ]");
}
System.out.println();
}
static void putValue(int value,int row,int col){
gameMatrix[row][col]=value;
}
public static void main(String[] args){
System.out.println("Initial Matrix:");
putValue(1, 4, 1);
putValue(1, 5, 1);
putValue(1, 4, 2);
showMatrix();
for(int n = 0; n < Rows; n++)
for(int m = 0; m < Cols; m++)
visited[n][m] = false;
//reset count
count = 0;
dfs(5,1,color,false);
//if there are more than 2 set the color to NOCOLOR
for(int n = 0; n < Rows; n++)
for(int m = 0; m < Cols; m++)
visited[n][m] = false;
if(count > 2)
{
dfs(5,1,color,true);
}
System.out.println("Matrix after dfs:");
showMatrix();
}
}
One issue you are encountering is that you do not check the upper row and leftest col:
static boolean ok(int r, int c)
{
return r > 0 && r < Rows && c > 0 && c < Cols;
}
you should check for r >= 0, c>= 0
Second issue is you are using dfs() twice, but the visited field is static - it is all set to true before the second run of dfs() you need to initialize it back to false in all fields before the second run, or the algorithm will terminate instantly without doing anything [since all the nodes are already in visited - and the algorithm will decide not to re-explore these nodes.].
Your code counts diagonal cells as neighbours too. If you want only left/right/top/bottom cells than you can check
if(!(dr == 0 && dc == 0) && ok(r+dr, c+dc) && dr * dc == 0)
You also need to count first cell. You don't count cell you start from.
I think you are after a flood-fill algorithm (perhaps with the slightly odd constraint that there must be at least two neighbours of the same colour)? I'm not sure why depth-first-search is appropriate here.