Solving maze using recursion (2 disrections) in java - java

i kind of need help on this problem. I want to use recursion to solve a NxN binary matrix. The problem is i think my recursion implementation is somehow not correct. In this problem I'm only allowed to go right and down only. I have checked issafe() method and everything seems to return true or false according to the 1=true and 0=false. if i run the run program, nothing displays. any help would be really appreciated.
public class Main {
public static void main(String[] args) {
int maze[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{0, 1, 0, 0},
{1, 1, 1, 2}
};
Maze rat = new Maze();
rat.solveMaze(maze, 0, 0);
}
}
public class Maze {
int maze[][];
int mazeSize;
int EXIT=2;
public Maze() {
mazeSize=4;
maze = new int[mazeSize][mazeSize];
}
// check is its safe to traverse
public Boolean isSafe(int x, int y, int maze[][]){
if (x>=0 && x<mazeSize && y>=0 && y<mazeSize && maze[x][y]==1){
return true;
}
else return false;
}
boolean solveMaze(int maze[][],int x,int y){
int solmaze[][]= { {0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}};
if(maze[x][y]==EXIT){
solmaze[x][y]=1;
printmaze(solmaze);
return true;
}
if(isSafe(x, y,maze) && maze[x][y]==1){
solmaze[x][y]=1;
return true;
}
if(isSafe(x, y,maze)==true && solveMaze(maze,x+1,y)==true){// down
solmaze[x][y]=1;
}
if(isSafe(x, y,maze)==true && solveMaze(maze,x,y+1)==true){//right
solmaze[x][y]=1;
}
solmaze[x][y]=0;
return false;
}
void printmaze(int maze[][]){//print maze
for(int i=0;i<maze.length;i++){
for(int j=0;j<maze.length;j++){
System.out.print(maze[i][j]);
}
System.out.println();
}
}
}

I believe that this is the solution you are looking for:
public class Main2 {
public static void main(String[] args) {
int maze[][] = {{1, 0, 0, 0},
{1, 1, 0, 1},
{0, 1, 0, 0},
{1, 1, 1, 2}
};
Maze rat = new Maze();
rat.solveAndPrintMaze(maze, 0, 0);
}
}
public class Maze {
int maze[][];
int mazeSize;
int EXIT=2;
public Maze() {
mazeSize=4;
maze = new int[mazeSize][mazeSize];
}
// check is its safe to traverse
public Boolean isSafe(int x, int y, int maze[][]){
if (x>=0 && x<mazeSize && y>=0 && y<mazeSize && maze[x][y]>=1){
return true;
}
else return false;
}
int solmaze[][]= {
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}};
boolean solveMaze(int maze[][],int x,int y){
if(maze[x][y]==EXIT){
solmaze[x][y]=1;
// printmaze(solmaze);
return true;
}
// if(isSafe(x, y,maze) && maze[x][y]==1){
// solmaze[x][y]=1;
// return true;
// }
if(isSafe(x+1, y,maze)==true && solveMaze(maze,x+1,y)==true){// down
solmaze[x][y]=1;
return true;
}
if(isSafe(x, y+1,maze)==true && solveMaze(maze,x,y+1)==true){//right
solmaze[x][y]=1;
return true;
}
solmaze[x][y]=0;
return false;
}
void printmaze(int maze[][]){//print maze
for(int i=0;i<maze.length;i++){
for(int j=0;j<maze.length;j++){
System.out.print(maze[i][j]);
}
System.out.println();
}
}
void solveAndPrintMaze(int maze[][],int x,int y) {
solveMaze(maze, x, y);
printmaze(solmaze);
}
}

On your first call to solveMaze, the second if is true ((0,0) is safe & there is a 1 there), so you return true without having printed anything.
Maybe if you explained what this is intended to do, one could help fix it (which may well be by removing it).

You're not actually attempting recursion here. To initiate recursion in the way that you're attempting, you would have to call your solveMaze method from within itself.
I was wrong about that. Correct answer is given by Scott below.

Related

representing a matrix with column/row labels

I would like to create a 2d Array with Java, or a Matrix with a int numbers.
I've already did that..but I still don't know how to assign labels to the rows/columns.
I would like to be able to access any number inside the matrix based on the row/columns
This is my java code
Gson gson = new Gson();
int[][] data = {{78, 0, 0, 0, 0}, {0, 54, 0, 0, 0}, {0, 0, 12, 0, 0}, {0, 0, 0, 74, 0}, {0, 0, 0, 0, 11}};
String json = gson.toJson(data);
// Convert JSON string into multidimensional array of int.
int[][] dataHeatMap = gson.fromJson(json, int[][].class);
for (int[] i : dataHeatMap) {
for (int j : i) {
System.out.print(j + " ");
}
System.out.println("");
}
return json;
You can use Enums:
public enum ROW {a, b, c, d, e}
public enum COL {f, g, h, i, j}
data[ROW.a.ordinal()][COL.f.ordinal()] = 3;
Use ENUM types which does represend the special index of your 2dim Array. Give them a field called value/name/... and create them with the index of them in the array. Afterwards you can easy call them with getting their letter-value which does represent the array index.
It´s very readable and ENUM.<VALUE> does not represent an INT value. So this is the way how you can do it.
public enum ROW {
A(0), B(1), C(2), D(3), E(4);
private final int value;
ROW(int value) { this.value = value; }
public int getValue() { return value; }
}
public enum COL {
F(0), G(1), H(2), I(3), J(4);
private final int value;
COL(int value) { this.value = value; }
public int getValue() { return value; }
}
public static void main(String []args){
int[][] matrix = {{78, 0, 0, 0, 0}, {0, 54, 0, 0, 0}, {0, 0, 12, 0, 0}, {0, 0, 0, 74, 0}, {0, 0, 0, 0, 11}};
System.out.println("Value: " + matrix[ROW.A.getValue()][COL.F.getValue()]);
}
I would prefer the way above because you see what directly happens and can assign any value you want. But you just can use ENUM.ordinal(), too.
Then data[ROW.a.ordinal()][...] will return 0 for ROW because it´s listed first. b will return 1,... It just depends on the way they are listed/created on the ENUM.

hashi a puzzle game, how to output the puzzle [duplicate]

I am making a tile based game in Android Studio. I want the map to output circles (islands) if it loops past a 1, and the circle should be in the correct grid coordinates however when I run it nothing happens at all.
int gameBoard[][] = {{1, 0, 1, 0, 0}, {0, 2, 0, 0, 2}, {2, 0, 3, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 2, 0, 2}};
public void onDraw(Canvas canvas) {
for (int i = 0; i < 4; i++) {
for (int R = 0; R < 4; R++) {
if (gameBoard[i][R] == 1) {
Paint Blue = new Paint();
Blue.setColor(Color.BLUE);
canvas.drawCircle(i, R, 10, Blue);
}
}
}
}
Along with color, you also need to set a few other things:
paint.setStyle(Paint.Style.STROKE);
or
paint.setStyle(Paint.Style.FILL);
It's possible that your onDraw method isn't being called. If adding the paint style doesn't fix your problem entirely, you should probably verify that onDraw is being called with a log at the beginning of the method.

Java Linear Regression

I need to find the best fitting regression line for a set of points.
For example for this matrix:
int b [][] = { { 3, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 1, 2, 3, 1, 0, 1, 0, 0, 0 },
{ 0, 1, 2, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 3, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 3, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 2, 3, 1 },
{ 0, 0, 0, 0, 0, 1, 1, 1, 2 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 1 } };
Every number represents the amount of data points (weight I suppose) at that location (where rows are the X axis and Columns are for the Y).
I have attempted to use the SimpleRegression class from the apache mathematics library and am having some issues.
First, it doesn't appear to support weights. Second I believe that I'm doing something wrong, even for a matrix that is nothing but 1's on the main diagonal the slope/intercept results make no sense.
public static void main(String[] args) {
double a[][] = new double[9][9];
for (int i = 0; i < 9; i++)
a[i][i] = 1;
SimpleRegression r = new SimpleRegression(true);
r.addData(a);
System.out.println("Slope = " + r.getSlope());
System.out.println("Intercept = " + r.getIntercept());
}
This gives me results that are incorrect. I would assume that this matrix represents the function f(x) = x yet the slope I'm getting is -0.12499..
Could anyone point me at what I'm doing wrong?
I have a feeling I'm not only misusing the code but also the mathematics.
As the comments say, addData() expects a 2xN matrix of x y positions or individual x y positions. The following example returns a slope of 1 for a diagonal matrix as expected:
public static void main(String[] args) {
double a[][] = new double[9][9];
for (int i = 0; i < 9; i++)
a[i][i] = 1;
SimpleRegression r = new SimpleRegression(true);
addData(r, a);
System.out.println("Slope = " + r.getSlope());
System.out.println("Intercept = " + r.getIntercept());
}
public static void addData(SimpleRegression r, double[][] data) {
for(int x=0; x<data.length; x++) {
for(int y=0; y<data[0].length; y++) {
for(int i=0; i<data[x][y]; i++) {
r.addData(x, y);
}
}
}
}
The example assumes that index 0 corresponds to a position of 0, index 1 corresponds to a position of 1 and so on. If this is not the case you need to add a function to transform index to position.

tiled map in java from array

I am attempting to iterate through a 2D array of integers to generate a tiled map using Java's Graphics2D.
int[][] mapArray = {{1, 1, 1, 1, 1, 1, 1, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 0, 0, 0, 0, 0, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1}};
public void draw(Graphics2D g2d){
for(int y = 0; y < mapArray.length; y++){
for(int x = 0; x < mapArray[0].length; x++){
if(mapArray[x][y] == 1){
ImageIcon ic = new ImageIcon("/Textures/stone.jpg");
g2d.drawImage(ic.getImage(), x, y, null);
}
else if(mapArray[x][y] == 0){
ImageIcon ic = new ImageIcon("/Textures/water.jpg");
g2d.drawImage(ic.getImage(), x, y, null);
}
I just can't seem to wrap my head around the logic of iterating a 2D array. Ideally, each 0 would represent a water tile while each 1 would represent a stone tile. Every time I run this I get a NullPointerException.
x and y are wrong way around
public void draw(Graphics2D g2d){
for(int y = 0; y < mapArray.length; y++){
for(int x = 0; x < mapArray[y].length; x++){ //you want to use y here not 0
if(mapArray[y][x] == 1){ //first box is outer array second is inner one
ImageIcon ic = new ImageIcon("/Textures/stone.jpg");
g2d.drawImage(ic.getImage(), x, y, null);
} else if(mapArray[y][x] == 0){
ImageIcon ic = new ImageIcon("/Textures/water.jpg");
g2d.drawImage(ic.getImage(), x, y, null);
}
}
}
}
I could see potentially two big issues in your code, in your code "y" represents rows and "x" represents columns but in your if statement you are picking [column][row] and while having a dry run you are probabily counting [row][column] and secondly you are always counting columns that are present in first row. if your data structure is always nXn in such case it will work but in any other case you would have different results and you might encounter ArrayIndexOutofBound exception.

Steganography program giving weird results

I am developing a steganography program for a computer programming class. It appears to gives random ascii symbols. The output is supposed to be BINARY. The encode message method was given to us by my teacher. We just have to program the decode part.
import java.awt.*;
class HideMessage {
public void encodeMessage(Picture stegoObject, int[] binaryArray) {
Pixel pixelTarget = new Pixel(stegoObject, 0, 0);
Pixel[] pixelArray = stegoObject.getPixels();
Color pixelColor = null;
int redValue = 0;
for (int x = 0; x < binaryArray.length; x++) {
redValue = binaryArray[x];
pixelTarget = pixelArray[x];
pixelTarget.setRed(redValue);
}
pixelTarget = pixelArray[binaryArray.length];
pixelTarget.setRed(255);
System.out.println("FinishedPic");
stegoObject.write("SecretMessage.bmp");
stegoObject.explore();
}
public void decodeMessage(Picture decodepic) {
int redValue = 0;
Pixel targetPixel = null;
Color pixelColor = null;
int sum = 0;
for (int x = 0; redValue < 2; x++) {
//inside nested loop to traverse the image from left to right
for (int count = 1; count < 9; count++) {
targetPixel =
decodepic.getPixel(count + (8 * x), 0);
//gets the x,y coordinate of the target pixel
pixelColor = targetPixel.getColor();
//gets the color of the target pixel
redValue = pixelColor.getRed();
if (redValue == 1) {
if (count == 1) {
sum = sum + 128;
}
if (count == 2) {
sum = sum + 64;
}
if (count == 3) {
sum = sum + 32;
}
if (count == 4) {
sum = sum + 16;
}
if (count == 5) {
sum = sum + 8;
}
if (count == 6) {
sum = sum + 4;
}
if (count == 7) {
sum = sum + 2;
}
if (count == 8) {
sum = sum + 1;
}
}
System.out.println(sum);
}
System.out.println((char)sum);
sum = 0;
} //end of the inner for loop
}
}
public class HideMessageTester {
public static void main(String[] args) {
int[] bitArray =
{ 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1,
0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0,
1, 1, 1, 1, 0, 0, 1 };
//int[] bitArray =
{ 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1,
0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1,
1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1,
0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1,
0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0,
0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1,
0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1,
1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0,
0, 1, 0, 0, 0, 0, 1};
Picture stegoObject = new Picture("Earth.bmp");
HideMessage stego = new HideMessage();
stego.encodeMessage(stegoObject, bitArray);
Picture decodeObject = new Picture("SecretMessage.bmp");
System.out.println("Now Decoding");
stego.decodeMessage(decodeObject);
}
}
First, some general pieces of advice: I think your program is overly complicated because the functions are commingling their responsibilities:
Picture stegoObject = new Picture("Earth.bmp");
HideMessage stego = new HideMessage();
stego.encodeMessage(stegoObject, bitArray);
Picture decodeObject = new Picture("SecretMessage.bmp");
System.out.println("Now Decoding");
stego.decodeMessage(decodeObject);
I was very surprised to see SecretMessage.bmp; it wasn't at all obvious that were trying to decode the object you had just created. Sure, upon reading the encodeMessage() method it was easy enough to determine where it came from, but I think this flow would have been easier:
/* encode */
Picture pic_to_steg = new Picture("foo.bmp");
HideMessage stego = new HideMessage();
Picture secret = stego.encodeMessage(pic_to_steg, bitArray);
secret.write("SecretMessage.bmp");
/* decode */
Picture pic_with_message = new Picture("SecretMessage.bmp");
int[] hidden = stego.decodeMessage(pic_with_message);
/* output `hidden` and compare against `bitArray` */
In other words: leave the file IO entirely up to the main flow of the program. Perhaps your routines will be called from a network server in the future, and the pictures will never be saved to disk. That modification will be far easier if the routines operate on Pictures and return amended Pictures and int[].
Can you test your encodeMessage() method in isolation? Perhaps look at the differences in what it does between an input file and an output file. This section looks troublesome:
public void encodeMessage(Picture stegoObject, int[] binaryArray) {
Pixel pixelTarget = new Pixel(stegoObject, 0, 0);
Pixel[] pixelArray = stegoObject.getPixels();
Color pixelColor = null;
int redValue = 0;
for (int x = 0; x < binaryArray.length; x++) {
redValue = binaryArray[x];
pixelTarget = pixelArray[x];
pixelTarget.setRed(redValue);
}
pixelTarget = pixelArray[binaryArray.length];
pixelTarget.setRed(255);
Is the pixelArray really a reference into the image that can be updated through simple assignment? I'd really expect the design to look more like this pseudo-code:
pixel p = image.getPixel(x, y);
p.setred(binaryArray[i]);
image.setPixel(x, y, p);
The decoding has some strange loops:
for (int x = 0; redValue < 2; x++) {
//inside nested loop to traverse the image from left to right
for (int count = 1; count < 9; count++) {
This loop might work exactly as you designed it, but upon a first reading, it feels very wrong: You start with x=0, you increment x each time through the loop, but you use redValue < 2 as your loop termination rule.
I would so much rather see the loop written like this:
int x = 0;
while (redValue < 2) {
/* stuff */
x++;
}
(It isn't identical; x is still valid outside the loop, which can be dangerous. However, I think this is much more clear.)
There are cases where the termination clause of a for loop isn't related to the setup or increment clauses -- in my experience, they are very rare.
In this case though, it feels like a mistake; the condition redValue < 2 a loop invariant, but the inner loop assumes it will only happen on pixels that are multiples of 8, which is an assumption that is not enforced in the encodeMessage() method.
Trying to compute an integer value from your redValues as you read them is needlessly complicating your decode routine. I suggest removing the inner loop and return an array exactly like the array passed into the encodeMessage() routine. This will be (a) easier (b) easier to debug (c) easier to test (d) a thousand times easier to handle writing bit arrays that aren't evenly divisible by 8.
Then write a second method that turns the bit array output into the sum, or ASCII characters, or EBCDIC characters, or RSA key parameters, or whatever it is that's being encoded. Don't try to do too much at once. Writing a separate method to decode the array of bits will be (a) easier (b) easier to debug (c) easier to test (d) thousand time easier to handle arbitrary output modifications.
I hope these hints help.

Categories

Resources