I've already written the code for this, but it didn't work. If it had worked, the run time complexity would have been very high.
for (int collumnInput=0; collumnInput < 3; collumnInput++)
{
for (int rowInput = 0; rowInput < 3; rowInput++)
{
try
{
puzzleArray[collumnInput][rowInput] = scan.nextInt();
if ((puzzleArray[collumnInput][rowInput] > 8) || (puzzleArray[collumnInput][rowInput] < 0))
{
System.out.println("Invalid 8-puzzle entered!");
System.exit(0);
}
for (int collumnCheck = 0; collumnCheck < collumnInput; collumnCheck++)//code to check for duplicates starts here.
{
for (int rowCheck = 0; rowCheck < rowInput; rowCheck++)
{
if (puzzleArray[collumnCheck][rowCheck]==puzzleArray[collumnInput][rowInput])
{
System.out.println("Invalid 8-puzzle entered!");
System.exit(0);
}
}
}
}
catch (java.util.InputMismatchException exception)
{
System.out.println("Invalid 8-puzzle entered!");
System.exit(0);
}
}
}
scan.close();
First of all, the code here runs, but doesn't detect duplicates in the array, so how do I fix that? The second thing is, is there a more resource efficient method of doing this? I have seen people use clone and copy methods, but I don't know if those are actually more resource efficient. Thanks.