Hard coding a two dimensional array error? - java

import java.util.Scanner;
public class SudokuPermuter {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Sudoku Permuter.\n");
int [] [] sudoku = new int[] [] {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
{ 9, 6, 0, 0, 0, 0, 0, 8, 4 },
{ 0, 0, 0, 2, 1, 6, 0, 0, 0 },
{ 2, 0, 0, 0, 0, 9, 6, 0, 0 },
{ 0, 1, 0, 3, 5, 7, 0, 0, 8 },
{ 8, 4, 0, 0, 0, 0, 0, 7, 5 },
{ 0, 2, 6, 0, 0, 0, 1, 3, 0 },
{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }
}
for (int row = 0; row < 10; row++) {
for (int column = 0; column < 10; column++) {
System.out.print(sudoku[row] [column] + " ");
}
System.out.println();
}
}
}
I'm trying to enter a sudoku puzzle to be printed out, and it says there's a semicolon expected after the last line { 0, 9, 0, 7, 0, 1, 0, 4, 0 }
But when I put the semicolon in, it takes the bracket as ending the whole String args method. I don't know why.

You need the semicolon after the second closing bracket (to end the assignment statement):
int [] [] sudoku = new int[] [] {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
{ 9, 6, 0, 0, 0, 0, 0, 8, 4 },
{ 0, 0, 0, 2, 1, 6, 0, 0, 0 },
{ 2, 0, 0, 0, 0, 9, 6, 0, 0 },
{ 0, 1, 0, 3, 5, 7, 0, 0, 8 },
{ 8, 4, 0, 0, 0, 0, 0, 7, 5 },
{ 0, 2, 6, 0, 0, 0, 1, 3, 0 },
{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }
} ;

int [] [] sudoku = new int[] [] {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
{ 9, 6, 0, 0, 0, 0, 0, 8, 4 },
{ 0, 0, 0, 2, 1, 6, 0, 0, 0 },
{ 2, 0, 0, 0, 0, 9, 6, 0, 0 },
{ 0, 1, 0, 3, 5, 7, 0, 0, 8 },
{ 8, 4, 0, 0, 0, 0, 0, 7, 5 },
{ 0, 2, 6, 0, 0, 0, 1, 3, 0 },
{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }
};

No need for the constructor. It can be instantiated like this...
int[][] sudoku = {
{ 0, 8, 0, 4, 0, 2, 0, 6, 0 },
{ 0, 3, 4, 0, 0, 0, 9, 1, 0 },
{ 9, 6, 0, 0, 0, 0, 0, 8, 4 },
{ 0, 0, 0, 2, 1, 6, 0, 0, 0 },
{ 2, 0, 0, 0, 0, 9, 6, 0, 0 },
{ 0, 1, 0, 3, 5, 7, 0, 0, 8 },
{ 8, 4, 0, 0, 0, 0, 0, 7, 5 },
{ 0, 2, 6, 0, 0, 0, 1, 3, 0 },
{ 0, 9, 0, 7, 0, 1, 0, 4, 0 }
} ;

also your Array throwing Exception ArrayIndexOutOfBoundsException
you need to make your array bigger or change the loops to 9 inested of 10

I think you should altogether choose a more convenient way of representing your
Sudoku when filling a table by hand : something like an array of
string, that you split later, is for instance more convenient to fill-in.
After the (very simple) conversion, you have your array.
(Well i used Javascript notation here, conversion should not be hard.)
var oneSudokuStrings = [];
var sl=0;
oneSudokuStrings [sl++] = " O 8 0 4 0 2 0 6 0 ";
oneSudokuStrings [sl++] = " 0 3 4 0 0 0 9 1 0 ";
oneSudokuStrings [sl++] = " 9 6 0 0 0 0 0 8 4 ";
oneSudokuStrings [sl++] = " 0 0 0 2 1 6 0 0 0 ";
oneSudokuStrings [sl++] = " 2 0 0 0 0 9 6 0 0 ";
oneSudokuStrings [sl++] = " 0 1 0 3 5 7 0 0 8 ";
oneSudokuStrings [sl++] = " 8 4 0 0 0 0 0 7 5 ";
oneSudokuStrings [sl++] = " 0 2 6 0 0 0 1 3 0 ";
oneSudokuStrings [sl++] = " 0 9 0 7 0 1 0 4 0 ";
var oneSudoku = [];
for (var i=0; i<oneSudokuStrings.length; i++) {
oneSudoku[i]=oneSudokuStrings[i].match(/[^ ]+/g);
}

Related

Sudoku solver using recursion and backtracking

I am trying to implement a Sudoku solver using Java. This is the code I've written as of now. If I try to run it, it goes on to an endless loop that keeps on printing the first row of the Sudoku board, and that too with an incorrect solution. I guess I'm implementing backtracking the incorrect way over here. I think I am printing the final and wrong as well, as only the first row is printed every time. Can someone please help me fix my code and tell me as to where I am going wrong?
public static void display(int[][] board) {
for(int[] arr : board) {
System.out.println(Arrays.toString(arr));
return;
}
}
public static boolean isSafe(int[][] board, int row, int col, int i) {
//check row
for(int a=0; a<board.length; a++) {
if(board[a][col]==i) {
return false;
}
}
//check col
for(int b=0; b<board.length; b++) {
if(board[row][b]==i) {
return false;
}
}
//check cell
int strow = row-(row%3);
int stcol = col-(col%3);
for(int x=strow; x<strow+3; x++) {
for(int y=stcol; y<stcol+3; y++) {
if(board[x][y]==i) {
return false;
}
}
}
return true;
}
public static void sudoku(int[][] board, int row, int col) {
if(row==board.length) {
display(board);
System.out.println();
return; //modify this to print ans
}
if(col==board.length) {
sudoku(board, row+1, 0);
return;
}
if(board[row][col]==0) {
for(int i=1; i<=9; i++) {
if(isSafe(board, row, col, i)) {
board[row][col]=i;
sudoku(board, row, col+1);
board[row][col]=0;
}
}
}
sudoku(board, row, col+1);
}
public static void main(String args[]) {
int[][] board=
{ {3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0} };
sudoku(board, 0, 0);
}
A bactracking algorithm can be applied here and the problem happens in your sudoku method.
First of all we can just pass the board and we don't want to pass the row and col.
We can just pass the board and the just traverse through each and every cell.
Only consider those cells that are 0's.
We don't want to consider any other cells as 0"s are the cells we are interested in.
Now if we see a cell which is 0, we try to find all the different possibilites from 1 to 9 which can fit in that cell and apply the 'isSafe()` logic which will just do the same check.
And we backtrack and continue with our checking.
public static void display(int[][] board) {
for(int[] arr : board) {
System.out.println(Arrays.toString(arr));
}
}
public static boolean isSafe(int[][] board, int row, int col, int i) {
//check row
for(int a=0; a<board.length; a++) {
if(board[a][col]==i) {
return false;
}
}
//check col
for(int b=0; b<board.length; b++) {
if(board[row][b]==i) {
return false;
}
}
//check cell
int strow = row-(row%3);
int stcol = col-(col%3);
for(int x=strow; x<strow+3; x++) {
for(int y=stcol; y<stcol+3; y++) {
if(board[x][y]==i) {
return false;
}
}
}
return true;
}
public static boolean sudoku(int [][] board) {
for (int i=0; i<9; i++) {
for (int j=0; j<9; j++) {
int current = board[i][j];
if (current == 0) {
for (int ch = 1; ch <= 9; ch++) {
if (isSafe(board, i, j, ch)) {
board[i][j] = ch;
if (sudoku(board)) {
return true;
}
board[i][j] = 0;
}
}
return false;
}
}
}
return true;
}
public static void main(String args[]) {
int[][] board=
{ {3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0} };
sudoku(board);
display(board);
}
Here is the output after the change
[3, 1, 6, 5, 7, 8, 4, 9, 2]
[5, 2, 9, 1, 3, 4, 7, 6, 8]
[4, 8, 7, 6, 2, 9, 5, 3, 1]
[2, 6, 3, 4, 1, 5, 9, 8, 7]
[9, 7, 4, 8, 6, 3, 1, 2, 5]
[8, 5, 1, 7, 9, 2, 6, 4, 3]
[1, 3, 8, 9, 4, 7, 2, 5, 6]
[6, 9, 2, 3, 5, 1, 8, 7, 4]
[7, 4, 5, 2, 8, 6, 3, 1, 9]
Note : I changed the return type to true or false inorder to make sure that a particular cell can be filled with a number say x. If its possible we return true and continue with the next cell which is 0, other wise we backtrack and check for other possibilities.
Updates :
The only change you are missing is an else block at the very end because even if the cell is 0 or any other number you are doing recursion sudoku(board, row, col+1);. So just enclose that statement in the else block and will give the desired output.
Code change without changing the return type:
public static void display(int[][] board) {
for(int[] arr : board) {
System.out.println(Arrays.toString(arr));
}
}
public static boolean isSafe(int[][] board, int row, int col, int i) {
//check row
for(int a=0; a<board.length; a++) {
if(board[a][col]==i) {
return false;
}
}
//check col
for(int b=0; b<board.length; b++) {
if(board[row][b]==i) {
return false;
}
}
//check cell
int strow = row-(row%3);
int stcol = col-(col%3);
for(int x=strow; x<strow+3; x++) {
for(int y=stcol; y<stcol+3; y++) {
if(board[x][y]==i) {
return false;
}
}
}
return true;
}
public static void sudoku(int[][] board, int row, int col) {
if(row==board.length) {
display(board);
System.out.println();
return; //modify this to print ans
}
if(col==board.length) {
sudoku(board, row+1, 0);
return;
}
if(board[row][col]==0) {
for(int i=1; i<=9; i++) {
if(isSafe(board, row, col, i)) {
board[row][col]=i;
sudoku(board, row, col+1);
board[row][col]=0;
}
}
}
else
sudoku(board, row, col+1);
}
public static void main(String args[]) {
int[][] board=
{ {3, 0, 6, 5, 0, 8, 4, 0, 0},
{5, 2, 0, 0, 0, 0, 0, 0, 0},
{0, 8, 7, 0, 0, 0, 0, 3, 1},
{0, 0, 3, 0, 1, 0, 0, 8, 0},
{9, 0, 0, 8, 6, 3, 0, 0, 5},
{0, 5, 0, 0, 9, 0, 6, 0, 0},
{1, 3, 0, 0, 0, 0, 2, 5, 0},
{0, 0, 0, 0, 0, 0, 0, 7, 4},
{0, 0, 5, 2, 0, 6, 3, 0, 0} };
sudoku(board, 0, 0);
}
Output :
[3, 1, 6, 5, 7, 8, 4, 9, 2]
[5, 2, 9, 1, 3, 4, 7, 6, 8]
[4, 8, 7, 6, 2, 9, 5, 3, 1]
[2, 6, 3, 4, 1, 5, 9, 8, 7]
[9, 7, 4, 8, 6, 3, 1, 2, 5]
[8, 5, 1, 7, 9, 2, 6, 4, 3]
[1, 3, 8, 9, 4, 7, 2, 5, 6]
[6, 9, 2, 3, 5, 1, 8, 7, 4]
[7, 4, 5, 2, 8, 6, 3, 1, 9]

Clipping Edges in a bitboard

I am using bitboards to generate attack tables for rooks, bishops, and queens. During my research of magic bitboards, it seems I need to clip the edges of all of my attack tables. I store my bitboards in a long, with square a1 being the LSB and square h8 being the MSB, moving row by row. Here is an example of an attack table for a rook on square c2:
[0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
[0, 0, 1, 0, 0, 0, 0, 0]
[1, 1, 0, 1, 1, 1, 1, 1]
[0, 0, 1, 0, 0, 0, 0, 0]
My problem is that I need to make all the ones on the edge of the bitboard 0s. At first I tried &ing each attack table with a binary long representing a border clip that looks like this:
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
Now this works for most squares in the center, but for attack tables on the edges it clips it in error. SO for a rook on h8:
Initial attack table:
[1, 1, 1, 1, 1, 1, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
Expected Result:
[0, 1, 1, 1, 1, 1, 1, 0]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 1]
[0, 0, 0, 0, 0, 0, 0, 0]
Actual Result:
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0]
How should i approach this so that it only clips the end of each attack ray? I realize my bit logic is off. Thanks.
Instead of masking out the bits at the border using a constant mask, use a dynamically generated mask.
Example, the rook (♜) is on field F6, so we generate a mask where F1, F8, A6, and H6 are set. Then we use that mask to turn of those bits in your attack pattern.
0 0 0 0 0 1 0 0 8
0 0 0 0 0 0 0 0 7
1 0 0 0 0 ♜ 0 1 6
0 0 0 0 0 0 0 0 5
0 0 0 0 0 0 0 0 4
0 0 0 0 0 0 0 0 3
0 0 0 0 0 0 0 0 2
0 0 0 0 0 1 0 0 1
A B C D E F G H
In this solution I assume that you encode the 64 squares row-wise inside a long. The square A1 is the LSB and the square H8 is the MSB. Therefore above example would be represented by 2305984846213677088L = 0x2000810000000020L = 0b0000010000000000000100000010000000000000000000000000000000000100000L
/**
* #param x 0-based index for the columns A to H (0=A_, 1=B_, ...)
* #param y 0-based index for the rows 1 to 8 (0=_1, 1=_2, ...)
*/
long rookEndpoints(int x, int y) {
long topBtm = 1L << x;
return (0b10000001L << (8 * y)) | (topBtm << (8 * 7)) | topBtm;
}
...
attackPattern &= ~rookEndpoints(x, y)

How to delete last element in array and put new in front?

I have a question concerning Java. I started up new to Java and my google search brought many results but non was the final help.
I created a class to track historical information. I have different values for different days and need to update them un a regular basis. I want to keep track of the last 30 days and created an array with 30 elements. When I call my 'shift' function I want to drop the last n elements and put zeros in front. Here is a minial example for 5 days:
public class Testclass {
private int[] histInfo;
public Element()
{
this.histInfo = new int[5];
}
public void shift_histInfo(long m)
{
//do magic
}
}
What I want shift to do is
INPUT:
histInfo = [50,21,1,45,901]
OPERATION:
shift_histInfo(2);
RESULT:
histInfo = [0,0,50,21,1]
I am thankfull for every kind of help you can support as well for thought-provoking impulses if you think that there is a way more elegant or efficient way.
Best :-)
Unless there are very tight performance constraints using the standard Collection classes will get the job done. Have a look at java.util.LinkedList.
As a programming exercise you might consider creating a ring buffer. The idea being to avoid copying the array on every insertion.
Keep a oldestIndex value.
When writing simply replace item[oldestIndex] and increment oldestIndex.
To iterate you start at oldestIndex and use an increment method to deal with wrapping round to the start of the array.
int nextIndex(int current) {
return (current + 1) % arrayLength;
}
Writing a nice encapsulating class to hide all this would be a good exercise.
You can try this :
public static void shift_histInfo(long m)
{
int[] myIntArray = {50,21,1,45,901};
int[] myIntArray2 = {50,21,1,45,901};
for (int j=0 ;j< myIntArray.length ; j++){
int temp = (int) (j+m);
if (temp >= myIntArray.length){
temp = temp - myIntArray.length;
myIntArray2[temp] = 0;
} else {
myIntArray2[temp] = myIntArray[j];
}
}
for (int j=0 ;j< myIntArray2.length ; j++){
System.out.println(myIntArray2[j]);
}
}
Output :
when shift_histInfo(2) ,
[0,0,50,21,1]
int[] array={1,2,3,4,5,6};
int removelength=2;
int e=1;
while(e<=removelength) {
for(int i=1;i<array.length;i++)
array[array.length-i]=array[array.length-i-1];
e++;
}
for(int i=0;i<removelength;i++) {
array[i]=0;
}
for(int g:array)
{
System.out.print(g);
}
For constraints that you wanted, although I did initialise the data in the same method instead of Element(). I don't know why the parameter is of type long so I left it and made an int local variable.
All it does is copy the index value over to the new array starting at m then increments/iterates until the end of the array.
You can also make the method return type int[] and then simply return changedInfo array. Instead of histInfo = changedInfo.clone();
private int[] histInfo;
public void shift_histInfo(long m) {
int n = (int) m;
this.histInfo = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15};
int length = this.histInfo.length;
int[] changedInfo = new int[length];
if (length - n >= 0) System.arraycopy(histInfo, 0, changedInfo, n + 0, length - n); //Edit: shortened to one line.
histInfo = changedInfo.clone();
System.out.println("Remove: " + n + " - " + Arrays.toString(changedInfo) + "\n");
}
public static void main(String[] args) {
Main main = new Main();
main.shift_histInfo(0);
main.shift_histInfo(30);
main.shift_histInfo(1);
main.shift_histInfo(15);
main.shift_histInfo(29);
}
println:
Remove: 0 - [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Remove: 30 - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Remove: 1 - [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Remove: 15 - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
Remove: 29 - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]

Convert Binary Array to Char Java

I have this binary array:
int[] bitArray = {
0,1,0,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,
0,1,1,1,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,1,0,0,1,1,
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,1,0,0,
0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,0,1,1,0,1,0,1,1,
0,1,1,1,1,0,0,1,};
It makes the phrase: The sun is in the sky
How would you convert the int binary array to a char?
Every eight bits make up a char. You could just loop over the bits and accumulate every eight together:
int[] bitArray = {0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 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, 1, 0, 0, 1, 1, 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, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1,};
char[] chars = new char[bitArray.length / 8];
for (int i = 0; i < chars.length; ++i) {
int c = 0;
for (int j = i * 8; j < (i + 1) * 8; ++j) {
c = c << 1;
c += bitArray[j];
}
chars[i] = (char)c;
}
String s = new String(chars);
System.out.println(s);
Loop over every byte (8 bits) of the array, create a string of those bits, and then convert those bits to an integer using Integer.parseInt(x,2) and then cast that to a character and added to the result.
public static void main (String[] args) throws java.lang.Exception
{
int[] bitArray = {0,1,0,1,0,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,0,1,1,1,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,1,0,0,1,1,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,1,0,0,0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1,0,0,1,0,0,0,0,0,0,1,1,1,0,0,1,1,0,1,1,0,1,0,1,1,0,1,1,1,1,0,0,1,};
int CHAR_SIZE = 8;
String T = "";
String result ="";
for (int i=0; i<bitArray.length; i+= CHAR_SIZE)
{
for (int j=0; j<CHAR_SIZE; j++)
T += Integer.toString(bitArray[i+j]);
result += Character.toString((char)Integer.parseInt(T, 2));
T="";
}
System.out.println(result);
}
output
The sun is in the sky
ASCII chars are one byte. One byte is eight bits. Separate the array elements in segments of 8 (0-7), (8-15), ...
Store these segments in another array. Use the following constructor to finish.
public String(byte[] bytes,
Charset charset)
Remember to specify utf8 as your charset. This causes the chars to be treated as one-byte ASCII chars.

How to implement dfs using recursion?

I'm trying to implement DFS with recursion using the following code,
public static void dfs(int i, int[][] mat, boolean [] visited){
visited[i] = true; // Mark node as "visited"
System.out.print(i + "\t");
for ( int j = 0; j < visited.length; j++ ){
if ( mat[i][j] ==1 && !visited[j] ){
dfs(j, mat, visited); // Visit node
}
}
}
I have a matrix and an array for tracking visited nodes,
// adjacency matrix for uni-directional graph
int [][] arr = {
// 1 2 3 4 5 6 7 8 9 10
{ 0, 1, 1, 1, 0, 0, 0, 0, 0, 0}, // 1
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0}, // 2
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 3
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0}, // 4
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0}, // 5
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 6
{ 0, 0, 0, 0, 0, 0, 0, 1, 1, 0}, // 7
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, // 8
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // 9
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} // 10
};
boolean [] visited = new boolean[10];
for (int i =0; i< visited.length; ){
visited[i++] = false;
}
I'm making the call as following,
dfs(1, arr, visited);
This return
// 1 6 7 8 9
which is not correct. It should return : [1 2 7 8 9 10 3 4 5 6]
The graph is as following,
How can I improve my code ?
Your code is perfectly correct, just call is incorrect.
You're calling the dfs on the 1st node, but root is at 0th node.
So if you just replace
dfs(1, arr, visited);
with
dfs(0, arr, visited);
it would print the correct order of indices, which means every element would be one less than your required result as Java array index starts at 0.
Also there's no need to initialize a primitive array as Java primitive arrays are already initialized and default value of boolean is false.
Following is the code after modifications
public class Dfs {
public static void main(String[] args) {
int[][] arr = {
// 1 2 3 4 5 6 7 8 9 10
{ 0, 1, 1, 1, 0, 0, 0, 0, 0, 0 }, // 1
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, // 2
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 3
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 }, // 4
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 }, // 5
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 6
{ 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, // 7
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 8
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, // 9
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } // 10
};
boolean [] visited = new boolean[10];
dfs(0, arr, visited);
}
public static void dfs(int i, int[][] mat, boolean[] visited) {
if(!visited[i]) {
visited[i] = true; // Mark node as "visited"
System.out.print( (i+1) + " ");
for (int j = 0; j < mat[i].length; j++) {
if (mat[i][j] == 1 && !visited[j]) {
dfs(j, mat, visited); // Visit node
}
}
}
}
}
Output
1 2 7 8 9 10 3 4 5 6

Categories