I was doing an assessment for job interview. One of the 3 problems that I had to solve in an hour was finding the maximal value in a grid where you traverse it and add 1 to the elements based on the coordinates given. I spent a little to much time on the second problem and only ended up with about 20 minutes for this one. I didn't finish it in time so it's bugging me.
I just want to make sure that the solution to the problem as I remember it is optimized.
The input is a String array of two int values and the dimension of the grid.
To illustrate, if the coordinates given are (3,2) (2,2) (1,3) then
[1][1][0]
[1][1][0]
[1][1][0]
[1][1][0]
[2][2][0]
[2][2][0]
[1][1][0]
[2][2][0]
[3][3][1]
and so on...
I believe the required result was the maximal value that is not in (1,1) and the number of times it exists in the grid.
This is the the solution I came up with. Is there any way to optimize it?
public static List<Integer> twoDimensions(String[] coordinates, int n) {
List<Integer> maxAndCount = new ArrayList<Integer>();
int[][] grid = new int[n][n];
int arrLength = coordinates.length;
int max = Integer.MIN_VALUE;
int count = 1;
for (int i = 0; i < arrLength; i++) {
String[] coors = coordinates[i].split(" ");
int row = Integer.parseInt(coors[0]);
int column = Integer.parseInt(coors[1]);
for (int j = 0; j < row; j++) {
for (int k = 0; k < column; k++) {
grid[j][k] += 1;
System.out.println("grid (" + j + "," + k + "): " + grid[j][k]);
if (!(j == 0 & k == 0) && grid[j][k] > max) {
max = grid[j][k];
count = 1;
} else if (grid[j][k] == max) {
count++;
}
}
}
}
maxAndCount.add(max);
maxAndCount.add(count);
return maxAndCount;
}
public static void main(String[] args) {
String[] coors = { "1 3", "2 4", "4 1", "3 2" };
System.out.println("The Max and count Are:" + twoDimensions(coors, 4).toString());
}
Other solution for exercise is. (erikdhv#gmail.com)
public static long countMax(List<String> upRight) {
// Write your code here
int xl = 1;
int yl = 1;
xl = Integer.parseInt(upRight.get(1).split(" ")[0]);
yl = Integer.parseInt(upRight.get(1).split(" ")[1]);
for (int i=0; i<upRight.size(); i++){
if (xl > Integer.parseInt(upRight.get((int) i).split(" ")[0]) ) xl = Integer.parseInt(upRight.get((int) i).split(" ")[0]);
if (yl > Integer.parseInt(upRight.get((int) i).split(" ")[1])) yl = Integer.parseInt(upRight.get((int) i).split(" ")[1]);
}
return (yl * xl);
}
This fails a few test cases I can think of, the value of XL should be on Index 0 not 1
Answer of erik fails for few test cases, just change int to long for xl and yl and boom... all test cases passed.
public static long twoDimensions(List<String> list, int n) {
long res = 0;
int rowCount =0,colCount=0;
for(int i=0;i<n;i++)
{
int row = list.get(i).charAt(0) - 48;
int col = list.get(i).charAt(2) - 48;
if(row > rowCount)
rowCount = row;
if(col >colCount)
colCount = col;
}
int tempArr[][] = new int[rowCount+1][colCount+1];
for(int i = 0;i<n;i++)
{
int row = list.get(i).charAt(0) - 48;
int col = list.get(i).charAt(2) - 48;
for(int j=row;j>=1;j--)
{
int m = j;
for(int k=1;k<=col;k++)
{
tempArr[m][k]= tempArr[m][k]+1;
if(tempArr[m][k]>res)
res = tempArr[m][k];
}
}
System.out.println("for row: "+row+" col: "+col);
for(int j = 0;j<=rowCount;j++)
{
for(int k=0;k<=colCount;k++)
{
System.out.print(""+tempArr[j][k]);
}
System.out.println("");
}
System.out.println("");
}
return res;
}
Related
Print all the combinations of elements in matrix of size m * n
Sample Example:
1 3 5
2 6 7
Expected Output:
1 , 3 , 5
1 , 3 , 7
1 , 6 , 5
1 , 6 , 7
2 , 3 , 5
2 , 3 , 7
2 , 6 , 5
2 , 6 , 7
Rules:
Every combination starts from left of matrix and proceeds towards right. It may switch rows though.
Every combination should have number of elements equal to number of columns.
A combination can't have an element from the same column present twice.
Number of columns and rows could vary. So solution has to be generic.
import java.util.Scanner;
class Combination {
public static void main(String args[]) {
int row, col, i, j;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix:\n");
row = in.nextInt();
col = in.nextInt();
int first[][] = new int[row][col];
System.out.println("Enter the elements if matric m*n:\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
first[i][j] = in.nextInt();
}
}
System.out.println("Matrix:\n");
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
System.out.print(first[i][j] + "\t");
}
System.out.println();
}
// Final Logic from here...
System.out.println("\nOut Matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
System.out.println(first[i][0] + "," + first[j][1] + ","
+ first[k][2]+"\n");
}
}
}
/* while (i < 2) {
j = 0;
while (j < 2) {
k = 0;
while (k < 2) {
System.out.println(first[i][0] + "," + first[j][1] + ","
+ first[k][2]);
k++;
}
j++;
}
i++;
}*/
in.close();
}
}
Works good for specific input but not able to do it dynamic....Need Help..
Thanks in Advance......
You could use recursion as follows:
...
// Final Logic from here...
System.out.println("\nOut Matrix:\n");
int[] outputRow = new int[col];
print(0, row, col, first, outputRow);
}
private static void print(int j, int row, int col, int[][] first, int[] outputRow) {
for (int i = 0; i < row; i++) {
outputRow[j] = first[i][j];
// recursively continue to populate outputRow until we reach the last column (j == col -1)
if (j < col - 1) {
print(j + 1, row, col, first, outputRow);
}
// we have reached the last column (j == col -1) so now we could print current permutation
if (j == col - 1) {
for (int k = 0; k < col; k++) {
System.out.print(" " + outputRow[k]);
}
System.out.println();
}
}
}
Here we process one column per recursion call starting with j==0.
outputRow stores the current permutation and is updated recursively.
When we recursively reach the last column then it's time to print the current permutation.
This is a possible approach
void printCombos(){
visit(0,-1,"");
}
void visit(int r,int c,String s){
if(c!=a[0].length-1)
for(int i=0;i<a.length;i++)
visit(i,c+1,s+" - "+a[i][c+1]);
else
System.out.println(s);
}
considering the matrix as a tree to deep-visit. Given an imaginary root * these are the edges (*,1) - (*,2) - (1,3) - (1,6) - (2,3) - (2,6) and so on
* --- 1 -- 3 -- 5
\ \/ \/
\ /\ /\
\ 2 -- 6 -- 7
with 5 and 7 being the leaves.
First create one more method :
private static void increasePointerArray(int[] poinerArray, int row)
{
for (int i = poinerArray.length-1; i >= 0; i--) {
if(poinerArray[i] == row-1) {
continue;
}
else {
poinerArray[i] = poinerArray[i] +1;
for (int j = i+1; j < poinerArray.length; j++) {
poinerArray[j] = 0;
}
break;
}
}
}
Now in final logic section put below code :
int[] poinerArray = new int[col];
int[] MaxArray = new int[col];
List<int[]> resultList = new ArrayList<int[]>();
Arrays.fill(poinerArray, 0);
Arrays.fill(MaxArray, row-1);
while(!Arrays.equals(poinerArray, MaxArray)) {
resultList.add(poinerArray.clone());
increasePointerArray(poinerArray, row);
}
resultList.add(poinerArray.clone());
System.out.println("Printing desired result : ");
for (int[] ks : resultList) {
StringBuffer sb = new StringBuffer();
for (j = 0; j < col; j++) {
sb.append(first[ks[j]][j]+"\t");
}
System.out.println(sb.toString());
sb = null;
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class Permutations{
public static String strings="";
public static ArrayList<String> out=new ArrayList<String>();
public static void gen(ArrayList<ArrayList<String>> x,int index){
for(int i=0;i<x.size();i++){
if(i>0){
String[] parts=strings.split(",");
strings="";
for(int k=0;k<parts.length;k++){
if(k==index)
break;
strings=strings+parts[k]+",";
}
}
if(index==x.get(0).size()-1){
strings=strings+(x.get(i).get(index));
out.add(strings);
}
else
strings=strings+(x.get(i).get(index))+",";
if(index+1<=x.get(0).size()-1)
gen(x,index+1);
}
}
public static void main(String[] args) throws IOException{
ArrayList<ArrayList<String>> x=new ArrayList<ArrayList<String>>();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String line;
while(true){
line=br.readLine();
if(line.contentEquals("")) break;
String[] parts=line.split(" ");
x.add(new ArrayList<String>());
for(int i=0;i<parts.length;i++){
x.get(x.size()-1).add(parts[i]);
}
}
gen(x,0);
for(int i=0;i<out.size();i++){
System.out.println(out.get(i));
}
}
}
This code is works. Its very generic and quite simple to understand. I did column wise permutations on the 2D Array.
I have been wondering how to diagonally wrap, from bottom left, a String into a matrix.
For example:
String str = "123456789";
//Output matrix:
// 479
// 258
// 136
//Or if str = "123456789123456";
//Output would be:
// 2
// 73
// 484
// 2595
// 13616
Here is what I have so far:
int index = 0;
for(int i = 0; i < matrix.length; i++)
{
for(int k = matrix.length - 1; k > -1; k--)
{
if(index == word.length())
break;
matrix[k][i] = "" + str.charAt(index);
index++;
}
}
This is reasonably efficient implementation which I believe is relatively easy to understand.
This code loops over successive diagonals, and when the current position is inside the matrix, it assigns the next character from the string.
In the below chart, the question mark positions are on the diagonal but they're not inside the matrix. No character is taken from the input string for these question mark positions.
Diagonal Matrix
4 ?
3 ??
2 479
1 258?
0 136??
The loop goes through rows in ascending order, but the assignment to each row is done in reverse because your matrix is upside down when viewed from the normal Java way of indexing arrays: matrix[size - row - 1] instead of matrix[row].
There is no need for special treatment of below, at and above the diagonal this way.
public static void main(String[] args) throws Exception {
String str = "123456789";
int size = 3;
int[][] matrix = new int[size][size];
{
int index = 0;
for (int diagonal = 0; diagonal < size * 2 - 1; diagonal++) {
int row = diagonal;
int column = 0;
while (row >= 0) {
if (row < size && column < size) {
matrix[size - row - 1][column] = Character.getNumericValue(str.charAt(index++));
}
row--;
column++;
}
}
}
}
It also works for larger sized matrices (4x4, 5x5 etc.) but you can only encode values up to 9 in your string - if you want higher values, it's better to encode them in a comma-separated string and split the string into an array of strings.
Not making any claims about efficiency here, but it should work so long as your string fits into a square matrix:
static char[][] toDiag(String s)
{
int sideLen = (int) Math.sqrt(s.length()); // assume string fits into
// square matrix
char[][] m = new char[sideLen][sideLen];
int index = 0;
//fill lower-left section of array
for (int i = m[0].length - 1; i >= 0; i--)
{
for (int k = 0; k <= m[0].length-1-i; k++)
{
m[i+k][k] = s.charAt(index++);
}
}
//fill upper-right section of array
for (int i = sideLen%2==1?sideLen/2:sideLen/2 -1; i <= m[0].length; i++)
{
for (int k = 0; k <= m[0].length-1-i; k++)
{
m[k][i+k] = s.charAt(index++);
}
}
return m;
}
public static void main(String[] args)
{
String inString = "123456789";
int N = (int) Math.sqrt((double) inString.length());
int out[][] = new int[N][N];
int index=0;
//fills elements below the diagonal
for(int i=0;i<N-1;i++)
for(int j=0;j<=i;j++)
out[N-1-i+j][j] = Character.getNumericValue(inString.charAt(index++));
//fills the diagonal
for(int i=0;i<N;i++)
out[i][i] = Character.getNumericValue(inString.charAt(index++));
//fills elements above the diagonal
for(int i=N-2;i>=0;i--)
for(int j=0;j<=i;j++)
out[j][N-1-i+j] = Character.getNumericValue(inString.charAt(index++));
//prints the output
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
System.out.print(out[i][j] + "\t");
}
System.out.println();
}
}
I'm pretty rusty on my Java skills but I was trying to write a program that prompts the user to enter a string and displays a maximum length increasing ordered subsequence of characters. For example, if the user entered Welcome the program would output Welo. If the user entered WWWWelllcommmeee, the program would still output Welo. I've gotten this much done but it's not doing what it should be and I'm honestly at a loss as to why.
import java.util.ArrayList;
import java.util.Scanner;
public class Stuff {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter a string. ");
String userString = input.next();
ArrayList charList = new ArrayList();
ArrayList finalList = new ArrayList();
int currentLength = 0;
int max = 0;
for(int i = 0; i < userString.length(); i++){
charList.add(userString.charAt(i));
for(int j = i; j < userString.length(); j++){
int k=j+1;
if(k < userString.length() && userString.charAt(k) > userString.charAt(j)){
charList.add(userString.charAt(j));
currentLength++;
}
}
}
if(max < currentLength){
max = currentLength;
finalList.addAll(charList);
}
for (int i = 0; i < finalList.size(); i++){
char item = (char) finalList.get(i);
System.out.print(item);
}
int size1 = charList.size();
int size2 = finalList.size();
System.out.println("");
System.out.println("Size 1 is: " + size1 + " Size 2 is : " + size2);
}
}
My code, if I input Welcome, outputs WWeceeclcccome.
Does anyone have some tips on what I'm doing wrong?
In these cases it tends to help to step away from the keyboard and think about the algorithm you're trying to implement. Try to explain it first in words.
You are constructing a list of individual characters by appending each of the characters in the input string followed by characters to its right that are in correct alphabetical with their successor. For the input "Welcome" this means the accumulated output will be, showing the outer loop in vertical and inner loop in horizontal:
W W e c
e e c
l c
c c
o
m
e
In total: WWeceeclccome
I can't see the logic of this implementation. Here is a faster solution which runs in O(nlogn) time.
import java.util.Scanner;
public class Stuff
{
//return the index of the first element that's not less than the target element
public static int bsearch(char[] arr, int size, int key)
{
int left = 0;
int right = size - 1;
int mid;
while (left <= right)
{
mid = (left + right) / 2;
if(arr[mid] < key)
left = mid + 1;
else
right = mid - 1;
}
return left;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Please enter a string: ");
String userString = input.next();
char[] maxArr = new char[userString.length()];
char[] precedent = new char[userString.length()];
maxArr[0] = userString.charAt(0);
precedent[0] = userString.charAt(0);
int len = 1;
for(int i = 1; i < userString.length(); i++)
{
if(userString.charAt(i) > maxArr[len - 1])
{
maxArr[len] = userString.charAt(i);
precedent[len] = userString.charAt(i);
len++;
}
else
maxArr[bsearch(maxArr, len, userString.charAt(i))] = userString.charAt(i);
}
//System.out.println(len);
for(int i = 0; i < len; i++)
System.out.print(precedent[i]);
}
}
Using Dynamic Programming O(N^2) in lexicography order mean if i/p is abcbcbcd then o/p can be abcccd, abbbcd, abbccd but as per lexicography order o/p will be abbbcd.
public static String longestIncreasingSubsequence(String input1) {
int dp[] = new int[input1.length()];
int i,j,max = 0;
StringBuilder str = new StringBuilder();
/* Initialize LIS values for all indexes */
for ( i = 0; i < input1.length(); i++ )
dp[i] = 1;
/* Compute optimized LIS values in bottom up manner */
for ( i = 1; i < input1.length(); i++ )
for ( j = 0; j < i; j++ )
if (input1.charAt(i) >= input1.charAt(j) && dp[i] < dp[j]+1)
dp[i] = dp[j] + 1;
/* Pick maximum of all LIS values */
for ( i = 0; i < input1.length(); i++ ) {
if ( max < dp[i] ) {
max = dp[i];
if (i + 1 < input1.length() && max == dp[i+1] && input1.charAt(i+1) < input1.charAt(i)) {
str.append(input1.charAt(i+1));
i++;
} else {
str.append(input1.charAt(i));
}
}
}
return str.toString();
}
I know this is probably very simple but I really can't seem to understand how to write the integers vertically. For instance, there is an array that has 4 integers which are 9, 21, 63, and 501, the outcome would be the following
9 2 5 6
1 0 3
1
This is a small step of my program and probably the easiest but I can't understand how to do it :(
Can someone please help me or guide me so I can finish my program
Try this pseudo code
int[] list = new int[] {9,21,63,501};
bool finished = false;
if (list.Count > 0) {
for (var j=0;!finshed; j++) {
finished = true;
for (var i = 0; i<list.Count;i++) {
String val = list[i].ToString();
if (val.length>j) {
write(val.charAt(j));
finished = false;
}
}
}
}
I have created a very modular and easy to follow solution.
Edit: Converted digitAtIndex() to a purely numerical calculation.
Kept the original and called it digitAtStrIndex().
public class IntegerColumns {
public IntegerColumns() {
int[] arr = new int[] {9, 21, 501, 63};
printColumnMajorOrder(arr);
}
public static void main(String[] args) {
new IntegerColumns();
}
// --------------------- Primary Functions --------------------------
// Prints out an Array of Integers, each in a vertical column
public void printColumnMajorOrder(int[] arr) {
int cols = arr.length;
int rows = maxDigits(arr);
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
int d = digitAtIndex(arr[c], r);
System.out.printf("%s\t", d >= 0 ? Integer.toString(d) : " ");
}
System.out.println();
}
}
// Returns the length of an Integer
public int numDigits(int i) {
if (i <= 0) return 0;
return (int)Math.floor(Math.log10(i))+1;
}
// Numeric calculation to find a digit at a specified index
public int digitAtIndex(int num, int index) {
int digits = numDigits(num);
int deg = digits - index - 1;
int pow = (int)Math.pow(10, deg);
return pow > 0 ? (int)(num/pow)%10 : -1;
}
// Returns the number of digits for the longest Integer in an Array
public int maxDigits(int[] arr) {
int max = 0;
for (int i : arr) {
int size = numDigits(i);
if (size > max) max = size;
}
return max;
}
// ---------------------- Extra Functions ---------------------------
// Hybrid of Integer and Substrings - String manipulation = slow
public int digitAtStrIndex(int number, int i) {
String n = Integer.toString(number);
return n.length() > i ? Integer.parseInt(n.substring(i, i+1)) : -1;
}
// Prints the digits of a number vertically
public void printNumberVertical(int num) {
for (int i = 0; i < numDigits(num); i++)
System.out.println(digitAtIndex(num, i));
}
}
`public class VerticalPrintService {
private int[] data;
public VerticalPrintService( int[] intArray ) {
this.data = intArray;
}
public void printVertically(){
int cols = data.length; // # of columns
int rows = getRows(); // # of rows
System.out.println("cols: " + cols);
System.out.println("rows: " + rows);
String[][] matrix = new String[rows][cols];
int rowIndex = 0;
int colIndex = 0;
// populate 2d array
for ( int i : data ) {
String str = String.valueOf(i);
for ( int j = 0; j < str.length(); j++ ) {
matrix[rowIndex][colIndex] = String.valueOf(str.charAt(j));
rowIndex++;
}
colIndex++;
rowIndex = 0;
}
// print
for ( int i = 0; i < rows; i++ ) {
for ( int j = 0; j < cols; j++ ) {
if ( null == matrix[i][j] ) {
System.out.print("\t");
} else {
System.out.print( matrix[i][j] + "\t" );
}
}
System.out.println();
}
}
private int getRows(){
int max = 0;
for ( int i : data ) {
int len = String.valueOf(i).length();
if ( len > max ) {
max = len;
}
}
return max;
}
}`
and in your main method
`public static void main(String[] args) {
int[] array = { 9, 53, 501, 90 };
VerticalPrintService vps = new VerticalPrintService(array);
vps.printVertically();
}`
In my last question seen here: Sudoku - Region testing I asked how to check the 3x3 regions and someone was able to give me a satisfactory answer (although it involved a LOT of tinkering to get it working how I wanted to, since they didn't mention what the class table_t was.)
I finished the project and was able to create a sudoku generator, but it feels like it's contrived. And I feel like I've somehow overcomplicated things by taking a very brute-force approach to generating the puzzles.
Essentially my goal is to create a 9x9 grid with 9- 3x3 regions. Each row / col / region must use the numbers 1-9 only once.
The way that I went about solving this was by using a 2-dimensional array to place numbers at random, 3 rows at a time. Once the 3 rows were done it would check the 3 rows, and 3 regions and each vertical col up to the 3rd position. As it iterated through it would do the same until the array was filled, but due to the fact that I was filling with rand, and checking each row / column / region multiple times it felt very inefficient.
Is there an "easier" way to go about doing this with any type of data construct aside from a 2d array? Is there an easier way to check each 3x3 region that might coincide with checking either vert or horizontal better? From a standpoint of computation I can't see too many ways to do it more efficiently without swelling the size of the code dramatically.
I built a sudoku game a while ago and used the dancing links algorithm by Donald Knuth to generate the puzzles. I found these sites very helpful in learning and implementing the algorithm
http://en.wikipedia.org/wiki/Dancing_Links
http://cgi.cse.unsw.edu.au/~xche635/dlx_sodoku/
http://garethrees.org/2007/06/10/zendoku-generation/
import java.util.Random;
import java.util.Scanner;
public class sudoku {
/**
* #antony
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int p = 1;
Random r = new Random();
int i1=r.nextInt(8);
int firstval = i1;
while (p == 1) {
int x = firstval, v = 1;
int a[][] = new int[9][9];
int b[][] = new int[9][9];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if ((x + j + v) <= 9)
a[i][j] = j + x + v;
else
a[i][j] = j + x + v - 9;
if (a[i][j] == 10)
a[i][j] = 1;
// System.out.print(a[i][j]+" ");
}
x += 3;
if (x >= 9)
x = x - 9;
// System.out.println();
if (i == 2) {
v = 2;
x = firstval;
}
if (i == 5) {
v = 3;
x = firstval;
}
}
int eorh;
Scanner in = new Scanner(System.in);
System.out
.println("hey lets play a game of sudoku:take down the question and replace the 0's with your digits and complete the game by re entering your answer");
System.out.println("enter your option 1.hard 2.easy");
eorh = in.nextInt();
switch (eorh) {
case 1:
b[0][0] = a[0][0];
b[8][8] = a[8][8];
b[0][3] = a[0][3];
b[0][4] = a[0][4];
b[1][2] = a[1][2];
b[1][3] = a[1][3];
b[1][6] = a[1][6];
b[1][7] = a[1][7];
b[2][0] = a[2][0];
b[2][4] = a[2][4];
b[2][8] = a[2][8];
b[3][2] = a[3][2];
b[3][8] = a[3][8];
b[4][2] = a[4][2];
b[4][3] = a[4][3];
b[4][5] = a[4][5];
b[4][6] = a[4][6];
b[5][0] = a[5][0];
b[5][6] = a[5][6];
b[6][0] = a[6][0];
b[6][4] = a[6][4];
b[6][8] = a[6][8];
b[7][1] = a[7][1];
b[7][2] = a[7][2];
b[7][5] = a[7][5];
b[7][6] = a[7][6];
b[8][4] = a[8][4];
b[8][5] = a[8][5];
b[0][0] = a[0][0];
b[8][8] = a[8][8];
break;
case 2:
b[0][3] = a[0][3];
b[0][4] = a[0][4];
b[1][2] = a[1][2];
b[1][3] = a[1][3];
b[1][6] = a[1][6];
b[1][7] = a[1][7];
b[1][8] = a[1][8];
b[2][0] = a[2][0];
b[2][4] = a[2][4];
b[2][8] = a[2][8];
b[3][2] = a[3][2];
b[3][5] = a[3][5];
b[3][8] = a[3][8];
b[4][0] = a[4][0];
b[4][2] = a[4][2];
b[4][3] = a[4][3];
b[4][4] = a[4][4];
b[4][5] = a[4][5];
b[4][6] = a[4][6];
b[5][0] = a[5][0];
b[5][1] = a[5][1];
b[5][4] = a[5][4];
b[5][6] = a[5][6];
b[6][0] = a[6][0];
b[6][4] = a[6][4];
b[6][6] = a[6][6];
b[6][8] = a[6][8];
b[7][0] = a[7][0];
b[7][1] = a[7][1];
b[7][2] = a[7][2];
b[7][5] = a[7][5];
b[7][6] = a[7][6];
b[8][2] = a[8][2];
b[8][4] = a[8][4];
b[8][5] = a[8][5];
break;
default:
System.out.println("entered option is incorrect");
break;
}
for (int y = 0; y < 9; y++) {
for (int z = 0; z < 9; z++) {
System.out.print(b[y][z] + " ");
}
System.out.println("");
}
System.out.println("enter your answer");
int c[][] = new int[9][9];
for (int y = 0; y < 9; y++) {
for (int z = 0; z < 9; z++) {
c[y][z] = in.nextInt();
}
}
for (int y = 0; y < 9; y++) {
for (int z = 0; z < 9; z++)
System.out.print(c[y][z] + " ");
System.out.println();
}
int q = 0;
for (int y = 0; y < 9; y++) {
for (int z = 0; z < 9; z++)
if (a[y][z] == c[y][z])
continue;
else {
q++;
break;
}
}
if (q == 0)
System.out
.println("the answer you have entered is correct well done");
else
System.out.println("oh wrong answer better luck next time");
System.out
.println("do you want to play a different game of sudoku(1/0)");
p = in.nextInt();
firstval=r.nextInt(8);
/*if (firstval > 8)
firstval -= 9;*/
}
}
}
I think you can use a 1D array, in much the same way a 1D array can model a binary tree. For example, to look at the value below a number, add 9 to the index.
I just made this up, but could something like this work?
private boolean makePuzzle(int [] puzzle, int i)
{
for (int x = 0; x< 10 ; x++)
{
if (//x satisfies all three conditions for the current square i)
{
puzzle[i]=x;
if (i==80) return true //terminal condition, x fits in the last square
else
if makePuzzle(puzzle, i++);//find the next x
return true;
}// even though x fit in this square, an x couldn't be
// found for some future square, try again with a new x
}
return false; //no value for x fit in the current square
}
public static void main(String[] args )
{
int[] puzzle = new int[80];
makePuzzle(puzzle,0);
// print out puzzle here
}
Edit: its been a while since I've used arrays in Java, sorry if I screwed up any syntax. Please consider it pseudo code :)
Here is the code as described below in my comment.
public class Sudoku
{
public int[] puzzle = new int[81];
private void makePuzzle(int[] puzzle, int i)
{
for (int x = 1; x< 10 ; x++)
{
puzzle[i]=x;
if(checkConstraints(puzzle))
{
if (i==80)//terminal condition
{
System.out.println(this);//print out the completed puzzle
puzzle[i]=0;
return;
}
else
makePuzzle(puzzle,i+1);//find a number for the next square
}
puzzle[i]=0;//this try didn't work, delete the evidence
}
}
private boolean checkConstraints(int[] puzzle)
{
int test;
//test that rows have unique values
for (int column=0; column<9; column++)
{
for (int row=0; row<9; row++)
{
test=puzzle[row+column*9];
for (int j=0;j<9;j++)
{
if(test!=0&& row!=j&&test==puzzle[j+column*9])
return false;
}
}
}
//test that columns have unique values
for (int column=0; column<9; column++)
{
for(int row=0; row<9; row++)
{
test=puzzle[column+row*9];
for (int j=0;j<9;j++)
{
if(test!=0&&row!=j&&test==puzzle[column+j*9])
return false;
}
}
}
//implement region test here
int[][] regions = new int[9][9];
int[] regionIndex ={0,3,6,27,30,33,54,57,60};
for (int region=0; region<9;region++) //for each region
{
int j =0;
for (int k=regionIndex[region];k<regionIndex[region]+27; k=(k%3==2?k+7:k+1))
{
regions[region][j]=puzzle[k];
j++;
}
}
for (int i=0;i<9;i++)//region counter
{
for (int j=0;j<9;j++)
{
for (int k=0;k<9;k++)
{
if (regions[i][j]!=0&&j!=k&®ions[i][j]==regions[i][k])
return false;
}
}
}
return true;
}
public String toString()
{
String string= "";
for (int i=0; i <9;i++)
{
for (int j = 0; j<9;j++)
{
string = string+puzzle[i*9+j];
}
string =string +"\n";
}
return string;
}
public static void main(String[] args)
{
Sudoku sudoku=new Sudoku();
sudoku.makePuzzle(sudoku.puzzle, 0);
}
}
Try this code:
package com;
public class Suduku{
public static void main(String[] args ){
int k=0;
int fillCount =1;
int subGrid=1;
int N=3;
int[][] a=new int[N*N][N*N];
for (int i=0;i<N*N;i++){
if(k==N){
k=1;
subGrid++;
fillCount=subGrid;
}else{
k++;
if(i!=0)
fillCount=fillCount+N;
}
for(int j=0;j<N*N;j++){
if(fillCount==N*N){
a[i][j]=fillCount;
fillCount=1;
System.out.print(" "+a[i][j]);
}else{
a[i][j]=fillCount++;
System.out.print(" "+a[i][j]);
}
}
System.out.println();
}
}
}