Here's the code:
public static int maxPathLengthHelper(int[][] paths, int x, int y){
int maxLength = 0;
if(x > 0 && paths[x-1][y] == 1){
int currentLength = 1 + maxPathLengthHelper(paths,x-1,y);
if(currentLength > maxLength){
maxLength = currentLength;
}
}
if(y > 0 && paths[x][y-1] == 1){
int currentLength = 1 + maxPathLengthHelper(paths,x,y-1);
if(currentLength > maxLength){
maxLength = currentLength;
}
}
if(x < paths.length - 1 && paths[x+1][y] == 1){
int currentLength = 1 + maxPathLengthHelper(paths,x+1,y);
if(currentLength > maxLength){
maxLength = currentLength;
}
}
if(y < paths[0].length - 1 && paths[x][y+1] == 1){
int currentLength = 1 + maxPathLengthHelper(paths,x,y+1);
if(currentLength > maxLength){
maxLength = currentLength;
}
}
return maxLength;
}
In the if statements where the y value is changed, a Stack Overflow error is caused, yet there is no error in the parts where the x value is changed. I was wondering why this was; if both were wrong, I would change the whole thing, but it's just in the second and fourth if statements that Stack Overflow errors are caused by the recursive call. The first and third if statements have no issues, and I have absolutely no idea what is different about them.
This is because when your code moves from 0,0 to 1,0 it again checks for 0,0 as first if condition is satisfied
public static int[][] visitedNodes;
public static void main(String args[]){
// when you call the recursive method, also initiate the visitedNodes
visitedNodes = new int[totalX][totalY];
for(int i = 0; i < totalX; i++)
for(int j = 0; j < totalY; i++)
visitedNodes[i][j] = 0;
maxPathLengthHelper(myPathList,0,0);
}
public static int maxPathLengthHelper(int[][] paths, int x, int y){
int maxLength = 0;
visitedNodes[x][y] = 1;
if(x > 0 && visitedNodes[x-1][y] == 0 && paths[x-1][y] == 1){
int currentLength = 1 + maxPathLengthHelper(paths,x-1,y);
if(currentLength > maxLength){
maxLength = currentLength;
}
}
if(y > 0 && visitedNodes[x][y-1] == 0 && paths[x][y-1] == 1){
int currentLength = 1 + maxPathLengthHelper(paths,x,y-1);
if(currentLength > maxLength){
maxLength = currentLength;
}
}
if(x < paths.length - 1 && visitedNodes[x+1][y] == 0 && paths[x+1][y] == 1){
int currentLength = 1 + maxPathLengthHelper(paths,x+1,y);
if(currentLength > maxLength){
maxLength = currentLength;
}
}
if(y < paths[0].length - 1 && visitedNodes[x][y+1] == 0 && paths[x][y+1] == 1){
int currentLength = 1 + maxPathLengthHelper(paths,x,y+1);
if(currentLength > maxLength){
maxLength = currentLength;
}
}
return maxLength;
}
Related
I am implementing the matlab 'bwmorph(img, 'thin')' algorithm in Java ImageJ. I've searched all over the net pretty much and found some similar implementations that work better, but I can't find the issue in my code. Any ideas?
My code:
public void run(ImageProcessor ip) {
MakeBinary(ip);
int sum2 = processThin(ip);
int sum = -1;
while (sum2 != sum) {
sum = sum2;
sum2 = processThin(ip);
}
}
public int processThin(ImageProcessor ipOriginal) {
int sum = 0;
// first iteration
ImageProcessor ip = ipOriginal.duplicate();
for (int i = 1; i < ip.getWidth() -1; i++)
for (int j = 1; j < ip.getHeight() -1; j++) {
int[] neighbors = selectNeighbors(ip, i, j);
if (G1(neighbors) == 1 && G2(neighbors) >= 2 && G2(neighbors) <= 3 && G3(neighbors) == 0)
ip.putPixel(i,j, 0);
}
// second iteration
for (int i = 1; i < ip.getWidth() -1; i++)
for (int j = 1; j < ip.getHeight()-1; j++) {
int[] neighbors = selectNeighbors(ip, i, j);
if (G1(neighbors) == 1 && G2(neighbors) >= 2 && G2(neighbors) <= 3 && G3prime(neighbors) == 0)
ip.putPixel(i,j, 0);
}
for(int i = 0; i < ip.getWidth(); i++)
for(int j = 0; j < ip.getHeight(); j++) {
if (ip.getPixel(i,j) != 0) sum++;
ipOriginal.putPixel(i, j, ip.getPixel(i, j));
}
return sum;
}
private int G1(int[] input) {
int xh = 0;
for (int i = 1; i <= 4; i++) {
if (input[2 * i - 1] == 0 && (input[2 * i] == 1 || (2 * i + 1 <= 8 ? input[2 * i + 1] == 1 : input[1] == 1)))
xh += 1;
}
return xh;
}
private int G2(int[] input) {
int n1 = 0, n2 = 0;
n1 = toInt(toBool(input[4]) || toBool(input[3])) + toInt(toBool(input[1]) || toBool(input[2])) +
toInt(toBool(input[8]) || toBool(input[7])) + toInt(toBool(input[6]) || toBool(input[5]));
n2 = toInt(toBool(input[2]) || toBool(input[3])) + toInt(toBool(input[1]) || toBool(input[8])) +
toInt(toBool(input[6]) || toBool(input[7])) + toInt(toBool(input[4]) || toBool(input[5]));
return Math.min(n1,n2);
}
private int G3 (int[] input){
return toInt((toBool(input[2]) || toBool(input[3]) || !toBool(input[8])) && toBool(input[1]));
}
private int G3prime (int[] input){
return toInt((toBool(input[6]) || toBool(input[7]) || !toBool(input[4])) && toBool(input[5]));
}
private boolean toBool(int i ){
return i == 1;
}
private int toInt(boolean i) {
return i ? 1 : 0;
}
private int[] selectNeighbors(ImageProcessor ip, int i, int j) {
int[] result = new int[9];
result[1] = ip.getPixel(i+1,j);
result[2] = ip.getPixel(i+1,j+1);
result[3] = ip.getPixel(i,j+1);
result[4] = ip.getPixel(i-1,j+1);
result[5] = ip.getPixel(i-1,j);
result[6] = ip.getPixel(i-1,j-1);
result[7] = ip.getPixel(i,j-1);
result[8] = ip.getPixel(i+1,j-1);
for (int x = 0; x < result.length; x++)
if (result[x] != 0) result[x] = 1;
return result;
}
The main issue appears to be with the horizontal lines, but not only that.
Note: I've added the toBool and toInt methods to deal with convenient data types, the code was binary before and the result is the same apparently.
EDIT:
After editing the code and omitting doing modifications between two iterations, I ended up with this result now.
The code looks like this now.
public int processThin(ImageProcessor ip) {
int sum = 0;
// first iteration
int[][] mask = new int[ip.getWidth()][ip.getHeight()];
for (int i = 1; i < ip.getWidth() -1; i++)
for (int j = 1; j < ip.getHeight() -1; j++) {
int[] neighbors = selectNeighbors(ip, i, j);
if (G1(neighbors) == 1 && G2(neighbors) >= 2 && G2(neighbors) <= 3 && G3(neighbors) == 0)
mask[i][j]++;
}
// second iteration
for (int i = 1; i < ip.getWidth() -1; i++)
for (int j = 1; j < ip.getHeight()-1; j++) {
int[] neighbors = selectNeighbors(ip, i, j);
if (G1(neighbors) == 1 && G2(neighbors) >= 2 && G2(neighbors) <= 3 && G3prime(neighbors) == 0)
mask[i][j]++;
}
for(int i = 0; i < ip.getWidth(); i++)
for(int j = 0; j < ip.getHeight(); j++) {
if (mask[i][j] != 0) sum++;
ip.putPixel(i, j, mask[i][j] > 0 ? 0 : ip.getPixel(i,j));
}
return sum;
}
The problem in your original code is that you write into your input image. In the very first iteration, moving left to right, you remove successive pixels because each has, after modifying the previous pixel, a background pixel as neighbor.
There are different ways to implement the thinning operation, but the simplest one that works in-place like your code does requires two passes through the image for each iteration of the thinning:
Go through the image and mark all candidate pixels. These are the pixels that have a background neighbor. Marking a pixel can be as simple as setting the pixel value to a given constant, for example 42 (assuming background is 0 and foreground is 1 or 255 or whatever you decided on).
Go through the image again and for each marked pixel, determine if removing it would change the geometry of the foreground. If not, remove it. In this test, take the marked pixels that haven't been removed yet as foreground.
I am solving n-queen, but i have a problem for some reason the while loop keeps looping without iterating, tempx and tempy doesn't go up by i/j. and the result keeps outputting 0,0
public static boolean isSafe(Board board, int row, int col){
int tempx;
int tempy;
for(int i = -1; i <= 1; i ++){
for(int j = -1; j <= 1; j ++){
try {
tempx = row + i;
tempy = col + j;
while(tempx >= 0 && tempx < board.getRow() && tempy >= 0 && tempy < board.getRow()) {
if(board.getTile(tempx, tempy).isOccupied())
return false;
tempx += i;
tempy += j;
}
} catch(Exception e){}
}
}
return true;
}
EDIT:
Ok i figured it out and it seems to work fine, for anyone that wants to know here it is, please correct me if there is a better way of doing this
public static boolean isSafe(Board board, int row, int col){
int tempx;
int tempy;
for(int i = -1; i <= 1; i ++){
for(int j = -1; j <= 1; j ++){
try {
tempx = row + i;
tempy = col + j;
for(int k = 0; k < board.getRow(); k++){
if(tempx >= 0 && tempx < 8 && tempy >= 0 && tempy < 8) {
if(board.getTile(tempx, tempy).isOccupied() )
return false;
tempx += i;
tempy += j;
}
}
} catch(Exception e){}
}
}
return true;
}
Do you realize that the loop
while(tempx >= 0 || tempx < 8 || tempy >= 0 || tempy < 8) {
is infinite as for every number tempx and tempy it is satisfied?
You probably wanted
while(tempx >= 0 && tempx < 8 && tempy >= 0 && tempy < 8) {
I have been working on a versions of Conway's Game of Life for school and I have run into a problem: cells are becoming dead or alive in the wrong places.
How can I fix that?
if (alive == 3 && aryBOARD[x][y] == 0) { //rule 4
aryCHANGE[x][y] = 1;
}
if (alive > 3 && aryBOARD[x][y] == 1) { //rule 3
aryCHANGE[x][y] = 0;
}
if (alive >= 2 && alive <= 3 && aryBOARD[x][y] == 1) { //rule 2
aryCHANGE[x][y] = 1;
}
if (alive < 2 && aryBOARD[x][y] == 1) { //rule 1
aryCHANGE[x][y] = 0;
}
if (dead > 5) { //rule check
aryCHANGE[x][y] = 0;
}
That is where I assume the problem is but if having the whole code would help:
package gameoflife2;
public class Game {
public static void main(String[] args) {
int[][] aryBOARD = new int[5][5];
int x = 0;
int y = 0;
int dead = 0;
int alive = 0;
int i, j;
// Board numbers
// 00011
// 00001
// 01000
// 01100
// 00000
aryBOARD[0][0] = 0;
aryBOARD[0][1] = 0;
aryBOARD[0][2] = 0;
aryBOARD[0][3] = 1;
aryBOARD[0][4] = 1;
aryBOARD[1][0] = 0;
aryBOARD[1][1] = 0;
aryBOARD[1][2] = 0;
aryBOARD[1][3] = 0;
aryBOARD[1][4] = 1;
aryBOARD[2][0] = 0;
aryBOARD[2][1] = 1;
aryBOARD[2][2] = 0;
aryBOARD[2][3] = 0;
aryBOARD[2][4] = 0;
aryBOARD[3][0] = 0;
aryBOARD[3][1] = 1;
aryBOARD[3][2] = 1;
aryBOARD[3][3] = 0;
aryBOARD[3][4] = 0;
aryBOARD[4][0] = 0;
aryBOARD[4][1] = 0;
aryBOARD[4][2] = 0;
aryBOARD[4][3] = 0;
aryBOARD[4][4] = 0;
// end of array
int[][] aryCHANGE = aryBOARD.clone(); // array change is equal to array
// board
// printing array
int rows = 5;
int colums = 5;
for (i = 0; i < rows; i++) {
for (j = 0; j < colums; j++) {
System.out.print(aryBOARD[i][j] + " ");
}
System.out.println("");
}
System.out.println("---------------------------");
// done printing array
// check for dead or alive cells
for (x = 0; x <= 4; x++) {
for (y = 0; y <= 4; y++) {
alive = 0;
dead = 0;
if ((x + 1 < 4) && (x + 1 > 0)) { // right
if (aryBOARD[x + 1][y] == 0) {
dead++;
} else {
alive++;
}
}
if (((y - 1 < 4) && (y - 1 > 0) && (x + 1 < 4) && (x + 1 > 0))) { // bottom
// right
// corner
if (aryBOARD[x + 1][y - 1] == 0) {
dead++;
} else {
alive++;
}
}
if (((y + 1 < 4) && (y + 1 > 0) && (x + 1 < 4) && (x + 1 > 0))) { // top
// right
// corner
if (aryBOARD[x + 1][y + 1] == 0) {
dead++;
} else {
alive++;
}
}
if ((y + 1 < 4) && (y + 1 > 0)) {// top middle
if (aryBOARD[x][y] == 0) {
dead++;
} else {
alive++;
}
}
if (((y + 1 < 4) && (y + 1 > 0) && (x - 1 < 4) && (x - 1 > 0))) {// top
// left
// corner
if (aryBOARD[x - 1][y + 1] == 0) {
dead++;
} else {
alive++;
}
}
if ((x - 1 < 4) && (x - 1 > 0)) {// left
if (aryBOARD[x - 1][y] == 0) {
dead++;
} else {
alive++;
}
}
if (((y - 1 < 4) && (y - 1 > 0) && (x - 1 < 4) && (x - 1 > 0))) {// bottom
// left
// corner
if (aryBOARD[x - 1][y - 1] == 0) {
dead++;
} else {
alive++;
}
}
// x++
if ((y - 1 < 4) && (y - 1 > 0)) {// bottom middle
if (aryBOARD[x][y - 1] == 0) {
dead++;
} else {
alive++;
}
}
// RULES
// 1 Any live cell with fewer than two live neighbors dies, as if caused
// by under-population.
// 2 Any live cell with two or three live neighbors lives on to the next
// generation.
// 3 Any live cell with more than three live neighbors dies, as if by
// overcrowding.
// 4 Any dead cell with exactly three live neighbors becomes a live
// cell, as if by reproduction.
// test alive and dead
if (alive == 3 && aryBOARD[x][y] == 0) {// rule 4
aryCHANGE[x][y] = 1;
}
if (alive > 3 && aryBOARD[x][y] == 1) {// rule 3
aryCHANGE[x][y] = 0;
}
if (alive >= 2 && alive <= 3 && aryBOARD[x][y] == 1) {// rule 2
aryCHANGE[x][y] = 1;
}
if (alive < 2 && aryBOARD[x][y] == 1) {// rule 1
aryCHANGE[x][y] = 0;
}
if (dead > 5) {// rule check
aryCHANGE[x][y] = 0;
}
}
}
for (i = 0; i < rows; i++) {
for (j = 0; j < colums; j++) {
System.out.print(aryCHANGE[i][j] + " ");
}
System.out.println("");
}
System.out.println("---------------------------");
} // end main
} // end class
You need to think a lot more about structuring your code and breaking things up into smaller chunks. That will help you a lot, especially when you move onto larger projects in the future.
For example write a simple method to count the number of living cells around a given cell.
Now your main loop through just becomes:
for (int x=0;x<width;x++) {
for (int y=0;y<height;y++) {
switch(countLivingAround(x,y)) {
case 0: // Less than 2 always dies
case 1:
grid(x,y) = 0;
break;
case 2: // Do nothing, keep current state
break;
case 3: // Breed
grid(x,y) = 1;
break;
case 4: // Dies from overcrowding
grid(x,y) = 0;
break;
}
}
}
Your count function can be simple, it just adds up the values at [x-1,y],[x,y+1], etc, remember to check for the edges of the board and handle that case correctly though.
In addition to breaking the logic up into different methods you need to either use a debugger to step thru the code or use printlns to display the x and y values your evaluating (See the sample below) You'll see that some of your calculations on the adjoining cells x and y coordinates need work.
for (x = 0; x <= 4; x++) {
for (y = 0; y <= 4; y++) {
alive = 0;
dead = 0;
System.out.println("evaluating cell x=" + x + ", y = " + y);
if ((x + 1 < 4) && (x + 1 > 0)) {// right
if (aryBOARD[x + 1][y] == 0) {
dead++;
}
else {
alive++;
}
System.out.println(" check 1 x=" + (x + 1) + ", y = " + y);
}
I'm developing a game "Minesweeper" for Andoid on Java and I have a problem when opening the cells. How to make sure that I click on the cell opened adjacent empty cells? (How it is done in Miner for Windows).
Introduction: I have an array which i receive from bluetooth socket stream. Array like this :
1 9 1 0
1 1 1 0
0 0 0 0
0 0 0 0
9-is a mine
0-is blank cell
1-nearest mines count
After that i calculate game field
array = Model.getGameField();
int size = array.length;
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
{
((TableRow) table.getChildAt(i)).getChildAt(j).setTag(array[i][j] + "");
}
OnClick function :
if (iWantToSetFlag == 0)
{
tmpBtn = ((Button) v);
if (!(tmpBtn.getTag().equals("9")))
{
OpenButtons(tmpBtn.getId() / 10, tmpBtn.getId() % 10);
recreateTable();
}
else
startLose();
}
else
{
if (((Button) v).getText().equals("M"))
((Button) v).setText("");
else
((Button) v).setText("M");
}
I have a function
private void OpenButtons(int x, int y)
{
array[x][y] = -1;
for (int k = -1; k < 2; k++)
{
for (int k1 = 1; k1 >= -1; k1--)
{
if (x + k >= 0 && x + k < array.length && y - k1 >= 0 && y - k1 < array[x + k].length)
if (array[x + k][y - k1] == 0)
OpenButtons(x + k, y - k1);
}
}
}
which recursively open cells but i have a StackOverFlow error. Help please.
You should be calling your recursion with changed parameters:
if (array[x + k][y - k1] == 0)
OpenButtons(x + k, y - k1);
And of course, as has been mentioned in the comments of the question, you should check for the array bounds yourself instead of just ignoring the exceptions:
if (x + k >= 0 && x + k < array.length &&
y - k1 >= 0 && y - k1 < array[x + k].length) { ...
put before your other if-clause will only check fields which actually exist. Ridding you of your malicious empty try-catch.
Since the recursive algorithm will still cause a StackOverflowException for large fields, an iterative algorithm might be better suited here.
private void OpenButtons(int x, int y) {
Queue<Point> toOpen = new LinkedBlockingQueue<>();
toOpen.add(new Point(x, y));
array[x][y] = -1;
while (!toOpen.isEmpty()) {
Point p = toOpen.poll();
x = p.x;
y = p.y;
for (int k = -1; k < 2; k++) {
for (int k1 = 1; k1 >= -1; k1--) {
if (x + k >= 0 && x + k < array.length && y - k1 >= 0
&& y - k1 < array[x + k].length)
if (array[x + k][y - k1] == 0) {
array[x + k][y - k1] = -1;
toOpen.add(new Point(x + k, y - k1));
}
}
}
}
}
I am a beginner in java.
I have been working on an maze problem trying it solve it by recursion.
I have written the code which seems to work on few inputs and not others.
The input is a maze consisting of 0's and 1's. # is the start and # is the exit.0 is wall and 1's are open.The output will be the hops from # to #.
Though i am solving the problem by recursion,I must be going wrong with the logic.
Please let me know where I am wrong.
Class practisenumwords
import java.util.Scanner;
class practisenumwords {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int r=in.nextInt();
int c=in.nextInt();
maze maz=new maze(r,c); /*input in string copied to array*/
char[] ch;
ch = "00000000111111101111011001101##11100".toCharArray();
int l=0;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++) /*initialising the maze elements*/
{
maz.m[i][j]=new cells();
maz.m[i][j].c=ch[l];
maz.m[i][j].row=i;
maz.m[i][j].col=j;
l++;
}
}
for(int i=0;i<r;i++) /*print the input maze */
{
for(int j=0;j<c;j++)
{
System.out.print(""+maz.m[i][j].c);
}
System.out.println();
}
maz.escape();
maz.find(maz.startx,maz.starty,maz.hops);
}
}
Class cells
class cells {
char c;
int row;
int col;
boolean done=false; /*initially all cells are unvisited*/
}
Class maze
class maze{
maze (int a,int b){
rows=a;
cols=b;
m=new cells[rows][cols];
}
int rows;
int cols;
cells[][] m;
int startx,starty;
int hops=0;
void escape()
{
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
if(m[i][j].c=='#')
{
startx=i;
starty=j;
System.out.println(startx+" "+starty);
}
}
}
}
void find(int x,int y,int h)
{
if ((x+1<rows && m[x+1][y].c=='#' && m[x+1][y].done!=true)
||(x-1>=0 && m[x-1][y].c=='#' && m[x-1][y].done!=true)
||(y+1<cols && m[x][y+1].c=='#' && m[x][y+1].done!=true)
||(y-1>=0 && m[x][y-1].c=='#' && m[x][y-1].done!=true)){
h++;
System.out.println(h);
}
else
{
if(x-1>=0 && m[x-1][y].c=='1' && m[x-1][y].done!=true){ /*north cell*/
m[x][y].done=true;
h++;
find(x-1,y,h);
}
if(x+1<rows && m[x+1][y].c=='1' && m[x+1][y].done!=true){ /*south cell*/
m[x][y].done=true;
h++;
find(x+1,y,h);
}
if(y+1<cols && m[x][y+1].c=='1' && m[x][y+1].done!=true){ /*east cell*/
m[x][y].done=true;
h++;
find(x,y+1,h);
}
if(y-1>=0 && m[x][y-1].c=='1' && m[x][y-1].done!=true){ /*west cell*/
m[x][y].done=true;
h++;
find(x,y-1,h);
}
}
}
}
Now,i get the right output for the inputs as the 1 in program.
000000
001111
111011
110110
01101#
#11100
output- 12 (obtaining right output)
00#000
001111
111011
110110
011011
#11100
output- 7 (obtaining right output)
BUT NOT FOR OTHER INPUTS like
0 0 0 0 # 0
0 1 0 1 1 0
1 1 1 1 0 1
0 1 0 1 0 0
0 0 # 1 1 1
0 1 1 0 0 1
correct output - 6 output obtained -7
Also the output changes with the order in which the adjacent cells are checked.
Honestly, I'd implement your recursive function a little differently:
And there's no need to check whether a bool value is != true, !boolValue is fine.
int find(int x,int y,int h)
{
int result = -1;
if ((x+1<rows && m[x+1][y].c=='#' && !m[x+1][y].done)
||(x-1>=0 && m[x-1][y].c=='#' && !m[x-1][y].done)
||(y+1<cols && m[x][y+1].c=='#' && !m[x][y+1].done)
||(y-1>=0 && m[x][y-1].c=='#' && !m[x][y-1].done)){
return h + 1;
}
else
{
if(x-1>=0 && m[x-1][y].c=='1' && !m[x-1][y].done){ /*north cell*/
m[x][y].done=true;
result = find(x-1,y,h + 1)
if (result > -1) {
return result;
}
m[x][y].done=false;
}
Implement the other three directions the same way, then result should still be -1 if no solution was found.
return result;
}
In a fast reading I notice:
if(...) {
...
h++;
find(x-1,y,h);
}
For each if-block.
Inside second if-block h == h+2 when first if-condition is satisfied and the same goes for third and fourth if-block
Maybe you should write:
if(...) {
...
// h++;
find(x-1,y,h+1);
}
int find(int x, int y, int h) {
if ((x + 1 < rows && m[x + 1][y].c == '#' && m[x + 1][y].done != true)
|| (x - 1 >= 0 && m[x - 1][y].c == '#' && m[x - 1][y].done != true)
|| (y + 1 < cols && m[x][y + 1].c == '#' && m[x][y + 1].done != true)
|| (y - 1 >= 0 && m[x][y - 1].c == '#' && m[x][y - 1].done != true)) {
h++;
finish = true;
return h;
} else {
if (x - 1 >= 0 && m[x - 1][y].c == '1' && m[x - 1][y].done != true
&& !finish) { /* north cell */
m[x][y].done = true;
int temp = find(x - 1, y, h);
if (temp != 0)
h = temp + 1;
return h;
}
if (x + 1 < rows && m[x + 1][y].c == '1'
&& m[x + 1][y].done != true && !finish) { /* south cell */
m[x][y].done = true;
int temp = find(x + 1, y, h);
if (temp != 0)
h = temp + 1;
return h;
}
if (y + 1 < cols && m[x][y + 1].c == '1'
&& m[x][y + 1].done != true && !finish) { /* east cell */
m[x][y].done = true;
int temp = find(x, y + 1, h);
if (temp != 0)
h = temp + 1;
return h;
}
if (y - 1 >= 0 && m[x][y - 1].c == '1' && m[x][y - 1].done != true
&& !finish) { /* west cell */
m[x][y].done = true;
int temp = find(x, y - 1, h);
if (temp != 0) {
h = temp + 1;
}
return h;
}
return 0;
}
}
Also include a boolean finish = false; in your maze class.
and also change the return of the main function
maz.hops = maz.find(maz.startx, maz.starty, maz.hops);
System.out.println(maz.hops);