ITI0011:uniqueLines
Siin on koodinäide, kuidas võib lahendada teisipäeval, 16. septembril kell 08:00 toimunud praksis tehtud tunniülesannet:
<source lang="java"> /**
* Whether the given one-dimensional array which represents 3x3 table * has unique numbers in all its rows and columns. * @author Ago * */
public class T8 {
public static void main(String[] args) { System.out.println(uniqueLines(new int[]{1, 2, 3, 3, 1, 2, 2, 3, 1})); System.out.println(uniqueLines(new int[]{1, 2, 3, 3, 1, 2, 7, 2, 1})); System.out.println(uniqueLinesGeneral(new int[]{1, 2, 3, 3, 1, 2, 2, 3, 1})); System.out.println(uniqueLinesGeneral(new int[]{1, 2, 3, 3, 1, 2, 7, 2, 1})); System.out.println(uniqueLinesGeneral(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})); }
/**
* Given an array of numbers, which represents 3x3 table
* whether all the rows and columns consist of unique numbers.
* @param board One-dimensional array which represents 3x3 table
* @return True
, if all the columns and rows consist of unique numbers
* (not a single row or column consist of two or more of the same numbers)
* False
, if at least one row or column has at least two elements with
* the same value.
*/
public static boolean uniqueLines(int[] board) {
/*
* Table with indices:
* 0 1 2
* 3 4 5
* 6 7 8
*/
// let's just check all 3 rows and columns within one for loop for (int i = 0; i < 3; i++) { // i * 3 will give us the first element in every row: // i = 0: 0 * 3 = 0 // i = 1: 1 * 3 = 3 // i = 2: 2 * 3 = 6
// i * 3 + 1 => second element in the row // i * 3 + 2 => third element in the row
// if first element in a row == second el in a row if (board[i * 3] == board[i * 3 + 1] || // OR second element == third element board[i * 3 + 1] == board[i * 3 + 2] || // OR first element == third element board[i * 3] == board[i * 3 + 2]) { // then we have a duplicate, let's return false // as there is no point to check further return false; }
// i gives us the first element in every column: // 0: first column // 1: second column // 2: third column
// i + 3 => second element in the column // i + 6 => third element in the column
// if the first element in column == second el in a col if (board[i] == board[i + 3] || // OR second element == third element board[i + 3] == board[i + 6] || // OR first element == third element board[i] == board[i + 6]) { // we have a duplicate, return false here return false; } } // if we reach here, there is no duplicate // so we can return true return true; }
/**
* General version of uniqueLines which accepts squares with
* different size than 3.
* @param board One-dimensional array which represents N x N
* table.
* @return true
if every row and column consists of
* only unique numbers, false
otherwise.
* If the input array does not present a square, false
* is returned.
* @see T8#uniqueLines(int[])
*/
public static boolean uniqueLinesGeneral(int[] board) {
int size = (int) Math.sqrt(board.length);
// let's check whether we have a full square
if (size * size != board.length) {
System.out.println("Not a square!");
return false;
}
// below, rows and columns are checked within the same loop
// it is basically the same as having separate for loops for rows
// and separate loops for columns
for (int axis1 = 0; axis1 < size; axis1++) {
for (int axis2 = 0; axis2 < size; axis2++) {
for (int comp = 0; comp < axis2; comp++) {
if (board[axis1 * size + axis2] == board[axis1 * size + comp]) {
// axis1 = row
// axis2 = column
// comp = compare current column value to previous columns' values
return false;
}
if (board[axis2 * size + axis1] == board[comp * size + axis1]) {
// axis1 = column
// axis2 = row
// comp = compare current row value to previous rows' values
return false;
}
}
}
}
// if some row/col had duplicates, we won't make this far
// (return ends the execution of this method)
// so, if we reach here, there are no duplicates
return true;
}
}
</source>