i have to check the validity of a sudoku as a homework. the rows and cols were no big deal, but i'm stuck at checking the boxes.
i want to loop through the 3x3 sub-boxes and check them for duplicates, with copying the numbers into a smaller 3x3 array, flattening it and then checking it for duplicates.
my while loop doesn't seem to make sense tho, the first iteration works but the second one writes wrong values into my new array. i just don't know where to add my boxcount.
i feel like a noob, if someone can help i'd be thankful
private static boolean isValidSudokuSolution(int[][] sudokuField) {
// TODO: Implementieren Sie hier Ihre Lösung für die Methode
//rows
int row = 0;
while (row < sudokuField.length) {
for (int i = 0; i < sudokuField[row].length; i++) {
for (int j = 0; j < i; j++) {
if (sudokuField[row][i] == sudokuField[row][j]) {
return false;
}
}
}
row++;
}
//cols
int col = 0;
while (col < sudokuField.length) {
for (int i = 0; i < sudokuField[col].length; i++) {
for (int j = 0; j < i; j++) {
if (sudokuField[i][col] == sudokuField[j][col]) {
return false;
}
}
}
col++;
}
//box
int boxCount = 0;
while (boxCount < sudokuField.length) {
//create box array
int[][] box = new int[3][3];
for (int i = 0; i < box.length; i++) {
for (int j = 0; j < box[i].length; j++) {
box[i][j] = sudokuField[i + boxCount][j];
}
}
//flatten box
int[] flattenedBox = new int[sSize];
int counter = 0;
for (int i = 0; i < box.length; i++) {
for (int j = 0; j < box[i].length; j++) {
int num = box[i][j];
flattenedBox[counter + j] = num;
}
counter += 3;
}
//look for duplicates
for (int i = 0; i < flattenedBox.length; i++) {
for (int j = 0; j < i; j++) {
if (flattenedBox[i] == flattenedBox[j]) {
return false;
}
}
}
boxCount += 3;
}
return true;
}
Related
I want to reverse a boolean array. free is the array. Here's my code:
public boolean[][] free = new boolean[6][6];
free = !free;
I get an error saying the operator ! is undefined. What should I do?
EDIT
It is not empty. I just want to inverse the values.
public boolean[][] free = new boolean[6][6];
void initFree(State s) {
for (int i = 0; i < nbcars; i++){
if (horiz[i]){
for (int j = 0; j < (len[i]-1); j++)
free[moveon[i]][s.pos[i]+j]=true;
}
if (!horiz[i]){
for (int j = 0; j < (len[i]-1); j++)
free[s.pos[i]+j][moveon[i]]=true;
}
}
free = !free;
}
You get operator ! is undefined simply because Java doesn't define ! operator on a two dimensional boolean array.
One way you could achieve what you want is by iterating over each value and then use the ! operator.
for(int i=0; i<free.length; i++)
{
for(int j=0; j<free[i].length; j++)
{
free[i][j] != free[i][j];
}
}
Here's a simple example you can modify for your uses. I included an invert method.
public class StackOverflowExample {
public static void main(String[] args) {
int ROW = 10, COL = 10;
boolean[][] ATwoDBoolArray = new boolean[ROW][COL];
PutValuesInArray(ATwoDBoolArray);
printArray(ATwoDBoolArray);
invertArray(ATwoDBoolArray);
printArray(ATwoDBoolArray);
}
private static boolean[][] PutValuesInArray(boolean array[][]) {
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = false;
}
}
return array;
}
private static void printArray(boolean[][] array){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.println(array[i][j]);
}
}
}
private static boolean[][] invertArray(boolean array[][]){
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
if(array[i][j]){
array[i][j] = false;
}else{
array[i][j] = true;
}
}
}
return array;
}
}
This method inverts all the values in a given 2-dimensional boolean array.
static void invertBooleanArray(boolean[][] arr) {
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[0].length; j++)
arr[i][j] = !arr[i][j];
}
So I am trying to figure out how to swap an ith row with the jth column. I have searched everywhere and still cant find a solution similar to what im looking for. (Pretty much switching the first row with the second column) I assume that i am going to have to create a temp to hold the value then swap it, but i can only do it for swapping out a row with another row or column with another column. At this point i am just completely lost. Any suggestions would be appreciated.
what it might look like for better clarification
Input:
2 3 4
1 2 3
4 5 6
Output:
3 2 5
1 3 3
4 4 6
Code :
private int n;
private int [][] Matrix = new int[n][n];
public void switchRowColumn(int i, int j)
{
for(int i=0; i< Matrix.length; i++)
{
I found this working, see if it works for you ...
public class Swap{
public static void main(String[]args){
int [][] array = {
{2,3,4},
{1,2,3},
{4,5,6}
};
swap(array, 0 , 1);
}
public static void swap ( int [][] array, int row, int col){
int [] temprow = new int [array[0].length];
int [] tempcol = new int [array.length];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if(i == row){
temprow = array[i]; // TEMP ROW
}
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if(j == col){
tempcol[i] = array[i][j];
}
}
}
for (int i = 0; i < tempcol.length; i++) {
System.out.print(tempcol[i]); /// COLS
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if(i == row){
array[i] = tempcol;
}
}
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
if(j == col){
array[i][j] = temprow[i];
}
}
}
System.out.println();
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
}
I'm having a very big problem, and it is because I need to remove any duplicate number in a random X x Y array. I tried to do it by writing four "fors" one inside the other, but it didn't work. Can someone help me?
Let's say that there's 34 three times in the array, so the first one will remain 34 and the others will be set to 0, and the other 0's will remain 0.
for (int linha = 0; linha < tamanhoLinhas; linha++) {
for (int coluna = 0; coluna < tamanhoColunas; coluna++) {
matriz[linha][coluna] = rand.nextInt(100);
}
}
for (int linha = 0; linha < tamanhoLinhas; linha++) {
for (int coluna = 0; coluna < tamanhoColunas; coluna++) {
int numeroAtual = matriz[linha][coluna];
for (int linha2 = 0; linha2 < tamanhoLinhas; linha2++) {
for (int coluna2 = 0; coluna2 < tamanhoColunas; coluna2++) {
if (numeroAtual == matriz[linha][coluna]){
matriz[linha][coluna] = 0;
}
}
}
}
}
for (int i = 0; i < tamanhoLinhas; i++) {
for (int j = 0; j < tamanhoColunas; j++) {
System.out.print(matriz[i][j] + " ");
}
System.out.println();
}
}
for (int linha = 0; linha < tamanhoLinhas; linha++) {
for (int coluna = 0; coluna < tamanhoColunas; coluna++) {
int numeroAtual = matriz[linha][coluna];
for (int linha2 = 0; linha2 < tamanhoLinhas; linha2++) {
for (int coluna2 = 0; coluna2 < tamanhoColunas; coluna2++) {
if (numeroAtual == matriz[linha][coluna]){
matriz[linha][coluna] = 0;
}
}
}
}
}
This code results in setting every element of the array to zero. As an example, let's say
linha == 3, coluna == 3
upon entering the third for loop, and numeroAtual == 22. The inner loops will scan the entire array and at some point will get to [3,3], which will be equal to numeroAtual; this will cause the value of matriz[3,3] to be set to zero.
That will repeat for every element in the matrix.
Well, I did it, but it's a little different now.
Initially, the program would modify all of the repeated numbers, but now the user chooses only one, and the program creates a random two dimensional array and find the repeated numbers and switch them for 0.
Here is how I did it:
public void questao7(int numeroSelecionado) {
int tamanhoLinhas = (rand.nextInt(9) + 1);
int tamanhoColunas = (rand.nextInt(9) + 1);
int matriz[][] = new int[tamanhoLinhas][tamanhoColunas];
boolean duplicado = false;
for (int linha = 0; linha < tamanhoLinhas; linha++) {
for (int coluna = 0; coluna < tamanhoColunas; coluna++) {
matriz[linha][coluna] = rand.nextInt(100);
}
}
System.out.println("Matriz original: ");
for (int i = 0; i < tamanhoLinhas; i++) {
for (int j = 0; j < tamanhoColunas; j++) {
System.out.print(matriz[i][j] + " ");
}
System.out.println();
}
for (int linha = 0; linha < tamanhoLinhas; linha++) {
for (int coluna = 0; coluna < tamanhoColunas; coluna++) {
if (!duplicado) {
if (matriz[linha][coluna] == numeroSelecionado){
duplicado = true;
}
} else if (matriz[linha][coluna] == numeroSelecionado) {
matriz[linha][coluna] = 0;
}
}
}
System.out.println("Matriz modificada: ");
for (int i = 0; i < tamanhoLinhas; i++) {
for (int j = 0; j < tamanhoColunas; j++) {
System.out.print(matriz[i][j] + " ");
}
System.out.println();
}
}
Examples of input:
3
4
Examples of output (assume that spaces = new lines.)
QQQH
QQHH
QHHH
QQQQH
QQQHH
QQHHH
QHHHH
So far, the fragment of code that attempts to print this is (Assume that all variables are pre-defined):
public int getSize()
{
for (int i = size; i > 0; i--){
for (int j = 1; j < size; j++){
out.print("Q");
out.print("H");
}
out.println("");
}
return 0;
}
It just prints: (assume that spaces = new lines.)
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
QHQHQHQHQH
For input of 5. I'm not quite sure how to make it print only the number of times of its respective integer value. Can someone explain?
You could break the inner loop it two, like this:
for (int i = size; i > 0; i--) {
for (int j = 0; j < i; j++) {
out.print("Q");
}
for (int j = i; j < size + 1; j++) {
out.print("H");
}
out.println();
}
Output:
QQQH
QQHH
QHHH
QQQQH
QQQHH
QQHHH
QHHHH
Or if you don't want to break the loop, you can use the ternary operator:
for (int i = size; i > 0; i--) {
for (int j = 0; j < size + 1; j++) {
out.print(j < i ? 'Q' : 'H');
}
out.println();
}
Try this
for (int i = 0; i < size; i++) {
for (int j = 1; j <= size-i; j++) {
System.out.print("Q");
}
for (int k = 0; k <= i; k++) {
System.out.print("H");
}
System.out.println("");
}
try this code block instead:
int j=0;
for (int i = size; i > 0; i--)
{
j=0;
while(j < i)
{
out.print("Q");
j++;
}
j=i;
while(j < size+ 1)
{
out.print("H");
j++;
}
out.println();
}
Tested with sample inputs. Working fine
public int getSize() {
for (int i = 1; i < size+1; i++) {
for (int j = 0; j < size+1; j++) {
int Qtimes = size-i;
if(j <= Qtimes) {
System.out.print("Q");
} else{
System.out.print("H");
}
}
System.out.println("");
}
return 0;
}
This works if the input is 4 - for example -change it to any number
public int getSize()
{
int cnt = 0;
int i,j,k = 0;
for ( i = 4; i > 0; i--){
for ( j = 0; j < i; j++){
System.out.print("Q");
}
cnt ++;
for( k = 0 ; k <cnt ; k++) {
System.out.print("H");
}
System.out.println("");
}
return 0;
}
output is
QQQQH
QQQHH
QQHHH
QHHHH
Everytime I call set() it resets all the values in the array to false except for what ever the int row int col is because i set that to true before the method ends. Why is this happening I thought I was making a copy of the array B and then setting the values that are in A to the values in B? Or am I mistaken here.
public void set(int row, int col) throws IndexOutOfBoundsException {
if (row >capacityr) {
boolean B[][] = new boolean[row+1][capacityc+1];
for (int k = 0; k < capacityr; k++)
for (int j = 0; j < capacityc; j++)
B[k][j] = a[k][j];
capacityr=row;
a = B;
}
if (col >capacityc) {
boolean C[][] = new boolean[capacityr+1][col+1];
for (int k = 0; k <capacityr; k++)
for (int j = 0; j < capacityc; j++)
C[k][j] = a[k][j];
capacityc=col;
a = C;
}
a[row][col] = true;
pT++;
}
It should be easier to use an ArrayList but I think this would fix your problem.
public void set(int row, int col) throws IndexOutOfBoundsException {
if(row > capacityr) {
if(col > capacityc) {
//both row and col are too big
boolean temp[][] = new boolean[row+1][col+1];
//copy a
for(int i = 0; i <= capacityr; i++) {
for(int j = 0; j <= capacityc; j++) {
temp[i][j] = a[i][j];
}
}
//set all the new elements to false
for(int i = capacityr+1; i <= row; i++) {
for(int j = capacityc+1; j <= col; j++) {
temp[i][j] = false;
}
}
//set row and col and a to temp
temp[row][col] = true;
a = temp;
//update capacity
capacityr = row;
capacityc = col;
}
else {
//just row is too big
boolean temp[][] = new boolean[row+1][capacityc+1];
for(int i = 0; i <= capacityr; i++) {
for(int j = 0; j <= capacityc; j++) {
temp[i][j] = a[i][j];
}
}
for(int i = capacityr+1; i <= row; i++) {
temp[i][capacityc] = false;
}
temp[row][col] = true;
a = temp;
capacityr = row;
}
}
else {
if(col > capacityc) {
//just col is too big
boolean temp[][] = new boolean[capacityr+1][col+1];
for(int i = 0; i <= capacityr; i++) {
for(int j = 0; j <= capacityc; j++) {
temp[i][j] = a[i][j];
}
}
for(int j = capacityc+1; j <= col; j++) {
temp[capacityr][j] = false;
}
temp[row][col] = true;
a = temp;
capacityc = col;
}
else {
//neither are too big
a[row][col] = true;
}
}
}