In this program, a randomly generated 2D array is populated with 1's or 0's and I attempt to find a path of 1's (no diagonal movement). I have got the program to work for smaller dimensions of the 2D array, but when the dimensions are too large, the error Exception in thread "main" java.lang.StackOverflowError occurs.
import java.util.Scanner;
import java.util.Random;
public class Daft {
private int counter = 0;
public static void main(String[] args) {
Daft punk = new Daft();
punk.run();
}
public void run() {
int ans;
int[][] array;
int[][] solvedPath;
do {
counter = 1;
array = populate(defineArray(firstDimension(),secondDimension()));
solvedPath = findPath(array);
System.out.println("Times before solvable: " + counter);
print(solvedPath);
ans = continuity();
}while(ans != 0);
}
public int[][] findPath(int[][] array) {
int r = 0, c = 0;
while(true) {
array[0][0] = 7;
if(c == 0 && r == array.length-1) { //reached the bottom left, checks right
if(array[r][c+1] == 1) {
array[r][c+1] = 7;
c+=1;
} else {
array[r][c] = 7;
break;
}
} else if(c == array[0].length-1 && r == array.length-1) { //reached the bottom right, checks left
if(array[r][c-1] == 1) {
array[r][c-1] = 7;
} else {
array[r][c] = 7;
break;
}
} else if(r == array.length-1) { //reached the bottom, checks left/right
if(array[r][c+1] == 1 && array[r][c-1] == 1) {
counter++;
newPath(array);
break;
} else if(array[r][c+1] == 1) { //checks right
array[r][c+1] = 7;
c+=1;
} else if(array[r][c-1] == 1) { //checks left
array[r][c-1] = 7;
c-=1;
} else { //end of path
array[r][c] = 7;
break;
}
} else if(c == 0) { //reached the left, checks right/bottom
if(array[r][c+1] == 1 && array[r+1][c] == 1) { //checks if path is unique
counter++;
newPath(array);
break;
} else if(array[r][c+1] == 1) {
array[r][c+1] = 7;
c+=1;
} else if(array[r+1][c] == 1) {
array[r+1][c] = 7;
r+=1;
} else {
counter++; //path has ended, not solvable
newPath(array);
break;
}
} else if(c == array[0].length-1) { //reached the right, checks left/bottom
if(array[r+1][c] == 1 && array[r][c-1] == 1) { //checks if path is unique
counter++;
newPath(array);
break;
} else if(array[r+1][c] == 1) {
array[r+1][c] = 7;
r+=1;
} else if(array[r][c-1] == 1) {
array[r][c-1] = 7;
c-=1;
} else {
counter++; //path has ended, not solvable
newPath(array);
break;
}
} else if(array[r][c+1] == 1 && array[r+1][c] == 1) { //checks if path is unique
counter++;
newPath(array);
break;
} else if(array[r][c-1] == 1 && array[r+1][c] == 1) { //checks if path is unique
counter++;
newPath(array);
break;
} else if(array[r][c+1] == 1) { //checks right
array[r][c+1] = 7;
c+=1;
} else if(array[r+1][c] == 1) { //checks bottom
array[r+1][c] = 7;
r+=1;
} else if(array[r][c-1] == 1) { //checks left
array[r][c-1] = 7;
c-=1;
} else {
counter++;
newPath(array);
break;
}
}
return array;
}
public int firstDimension() {
Scanner in = new Scanner(System.in);
System.out.println("Size of the first dimension:");
return in.nextInt();
}
public int secondDimension() {
Scanner in = new Scanner(System.in);
System.out.println("Size of the second dimension:");
return in.nextInt();
}
public int[][] defineArray(int firstDimension, int secondDimension) {
return new int[firstDimension][secondDimension];
}
public int[][] populate(int[][] array) {
Random rand = new Random();
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++) {
array[i][j] = rand.nextInt(2);
}
}
return array;
}
public int continuity() {
Scanner in = new Scanner(System.in);
System.out.println("Enter a number to continue (0 to quit):");
return in.nextInt();
}
public void print(int[][] array) {
for(int[] ints : array) {
for(int anInt : ints) {
System.out.print(anInt + " ");
}
System.out.println();
}
System.out.println();
}
public void newPath(int[][] array) {
findPath(populate(array));
}
}
I am trying to solve this CSES problem: Grid Paths. You are given a string of length 48, and you have to find the amount of paths such that you traverse all of the grid and end up at the lower left corner.
I believe I have pruned the search to the best of my ability, as according to this book: CP Handbook (Look in the pruning the search category), the best optimization for this type of problem is to prevent your path from closing yourself off, and I have already implemented this. The time limits for this specific problem are tight, and although I have basically solved this problem, I am still failing 1-2 test cases because my solution takes around 1.01 seconds instead of being below the 1 second time limit.
Finally, I just wanted to know if there were any cool micro-optimizations I could use to marginally enhance the speed of my java code, so I could actually pass all of the test cases for this problem.
import java.io.*;
public class GridPaths {
public static class FastIO {
InputStream dis;
byte[] buffer = new byte[1 << 17];
int pointer = 0;
public FastIO(String fileName) throws Exception {
dis = new FileInputStream(fileName);
}
public FastIO(InputStream is) {
dis = is;
}
int nextInt() throws Exception {
int ret = 0;
byte b;
do {
b = nextByte();
} while (b <= ' ');
boolean negative = false;
if (b == '-') {
negative = true;
b = nextByte();
}
while (b >= '0' && b <= '9') {
ret = 10 * ret + b - '0';
b = nextByte();
}
return (negative) ? -ret : ret;
}
long nextLong() throws Exception {
long ret = 0;
byte b;
do {
b = nextByte();
} while (b <= ' ');
boolean negative = false;
if (b == '-') {
negative = true;
b = nextByte();
}
while (b >= '0' && b <= '9') {
ret = 10 * ret + b - '0';
b = nextByte();
}
return (negative) ? -ret : ret;
}
Integer[] readArray(int n) throws Exception {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
byte nextByte() throws Exception {
if (pointer == buffer.length) {
dis.read(buffer, 0, buffer.length);
pointer = 0;
}
return buffer[pointer++];
}
String next() throws Exception {
StringBuilder ret = new StringBuilder();
byte b;
do {
b = nextByte();
} while (b <= ' ');
while (b > ' ') {
ret.appendCodePoint(b);
b = nextByte();
}
return ret.toString();
}
}
static char[] board;
static boolean[][] visited = new boolean[7][7];
static int ans = 0;
public static boolean works(int i, int j) {
//makes sure that current spot is on the 7x7 grid and is not visited
return (i >= 0 && i<=6 && j>=0 && j<=6 && !visited[i][j]);
}
public static void solve(int i, int j, int steps) {
if (i == 6 && j == 0) {
if (steps == 48) ans++; //all spots of the grid have to be visited in order to be counted as part of the answer
return;
}
visited[i][j] = true;
//you are given ? characters in the input string, and those mean that you have to try out all 4 combinations (U,D,L,R)
if (board[steps] == '?' || board[steps] == 'L') {
//second condition of the second if statement checks if the spot directly ahead of the current spot is blocked, and if it is, the left and right spots cannot both be unvisited or else you will not continue searching
if (works(i,j-1) && !(!works(i,j-2) && works(i+1,j-1) && works(i-1,j-1))) {
solve(i, j - 1, steps + 1);
}
}
if (board[steps] == '?' || board[steps] == 'R') {
if (works(i,j+1) && !(!works(i,j+2) && works(i+1,j+1) && works(i-1,j+1))) {
solve(i, j + 1, steps + 1);
}
}
if (board[steps] == '?' || board[steps] == 'U') {
if (works(i-1,j) && !(!works(i-2,j) && works(i-1,j+1) && works(i-1,j-1))) {
solve(i - 1, j, steps + 1);
}
}
if (board[steps] == '?' || board[steps] == 'D') {
if (works(i+1,j) && !(!works(i+2,j) && works(i+1,j+1) && works(i+1,j-1))) {
solve(i + 1, j, steps + 1);
}
}
visited[i][j] = false;
}
public static void main(String[] args) throws Exception {
FastIO in = new FastIO(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
board = in.next().toCharArray();
solve(0,0,0);
out.println(ans);
out.close();
}
}
Note: I am already using one of the fastest, if not the fastest, ways to receive input in Java, so I do not believe I can actually improve upon that.
I've been playing around with this. In addition to using a standard mechanism for reading the input file (which I suggested in a comment), you can gain a little time in the search alg itself by doing two things:
Break the case board[steps] == '?' off from the other cases. So check for board[steps] == '?' first, and just try all four directions in that case. Otherwise (the else case for if (board[steps] == '?'), just check for U/D/L/R. Since for most steps, the character will be '?', you save having to make the U/D/L/R tests most of the time.
Look up the character to be tested once, with c = board[steps],and then use c in each test instead of board[steps].
Doing these two things saved about 5% it seems. I was doing 100 reps of the solve and timing with System.currentTimeMillis(). I know there are more accurate ways of timing, but this was good enough to see a definite improvement even though the times jumped around quite a bit trial to trial. The best I ever saw in each case was 3600 millis for 100 iterations as originally written vs 3400 millis with the improvements.
My guess is that it's mostly the first change that matters. I'd expect the compiler to be doing the second already, but I didn't try the two optimizations independently.
I also solved this problem in java (AC), and here is how I did it
public static char[] defaultPath;
public static boolean[][] isUsed;
public static int counter = 0;
public static void solve(int indexChar, int indexRow, int indexColumn) {
if (indexRow == 8 && indexColumn == 2) {
if (indexChar == 48) {
counter++;
}
}else {
// Find correct way: 'D', 'U', 'L', 'R'
char correctWay = '?';
// (1) (1)
// 0 1 or 1 0
// 1 1
if ((isUsed[indexRow+1][indexColumn+1] || isUsed[indexRow+1][indexColumn-1])
&& isUsed[indexRow+2][indexColumn] && !isUsed[indexRow+1][indexColumn]) {
correctWay = 'D';
}
// 1 1
// 0 1 or 1 0
// (1) (1)
else if ((isUsed[indexRow-1][indexColumn+1] || isUsed[indexRow-1][indexColumn-1])
&& !isUsed[indexRow-1][indexColumn] && isUsed[indexRow-2][indexColumn]) {
correctWay = 'U';
}
// 1 0 (1) or 1
// 1 1 0 (1)
else if ((isUsed[indexRow+1][indexColumn-1] || isUsed[indexRow-1][indexColumn-1])
&& !isUsed[indexRow][indexColumn-1] && isUsed[indexRow][indexColumn-2]) {
correctWay = 'L';
}
//(1) 0 1 or 1
// 1 (1) 0 1
else if ((isUsed[indexRow+1][indexColumn+1] || isUsed[indexRow-1][indexColumn+1])
&& !isUsed[indexRow][indexColumn+1] && isUsed[indexRow][indexColumn+2]) {
correctWay = 'R';
}
// Check input path (default path)
char c = defaultPath[indexChar];
if (c == '?') {
if (correctWay == '?') {
// 'D'
if (!isUsed[indexRow+1][indexColumn]) {
isUsed[indexRow+1][indexColumn] = true;
solve(indexChar+1, indexRow+1, indexColumn);
isUsed[indexRow+1][indexColumn] = false;
}
// 'U'
if (!isUsed[indexRow-1][indexColumn]) {
isUsed[indexRow-1][indexColumn] = true;
solve(indexChar+1, indexRow-1, indexColumn);
isUsed[indexRow-1][indexColumn] = false;
}
// 'L'
if (!isUsed[indexRow][indexColumn-1]) {
isUsed[indexRow][indexColumn-1] = true;
solve(indexChar+1, indexRow, indexColumn-1);
isUsed[indexRow][indexColumn-1] = false;
}
// 'R'
if (!isUsed[indexRow][indexColumn+1]) {
isUsed[indexRow][indexColumn+1] = true;
solve(indexChar+1, indexRow, indexColumn+1);
isUsed[indexRow][indexColumn+1] = false;
}
}else {
if (correctWay == 'D') {
isUsed[indexRow+1][indexColumn] = true;
solve(indexChar+1, indexRow+1, indexColumn);
isUsed[indexRow+1][indexColumn] = false;
}else if (correctWay == 'U') {
isUsed[indexRow-1][indexColumn] = true;
solve(indexChar+1, indexRow-1, indexColumn);
isUsed[indexRow-1][indexColumn] = false;
}else if (correctWay == 'L') {
isUsed[indexRow][indexColumn-1] = true;
solve(indexChar+1, indexRow, indexColumn-1);
isUsed[indexRow][indexColumn-1] = false;
}else if (correctWay == 'R') {
isUsed[indexRow][indexColumn+1] = true;
solve(indexChar+1, indexRow, indexColumn+1);
isUsed[indexRow][indexColumn+1] = false;
}
}
}else {
if (c == correctWay || correctWay == '?') {
if (c == 'D' && !isUsed[indexRow+1][indexColumn]) {
isUsed[indexRow+1][indexColumn] = true;
solve(indexChar+1, indexRow+1, indexColumn);
isUsed[indexRow+1][indexColumn] = false;
}else if (c == 'U' && !isUsed[indexRow-1][indexColumn]) {
isUsed[indexRow-1][indexColumn] = true;
solve(indexChar+1, indexRow-1, indexColumn);
isUsed[indexRow-1][indexColumn] = false;
}else if (c == 'L' && !isUsed[indexRow][indexColumn-1]) {
isUsed[indexRow][indexColumn-1] = true;
solve(indexChar+1, indexRow, indexColumn-1);
isUsed[indexRow][indexColumn-1] = false;
}else if (c == 'R' && !isUsed[indexRow][indexColumn+1]) {
isUsed[indexRow][indexColumn+1] = true;
solve(indexChar+1, indexRow, indexColumn+1);
isUsed[indexRow][indexColumn+1] = false;
}
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
defaultPath = scanner.next().toCharArray();
isUsed = new boolean[11][11];
for (int i = 0; i < 11; i++) {
isUsed[0][i] = true;
isUsed[1][i] = true;
isUsed[9][i] = true;
isUsed[10][i] = true;
isUsed[i][0] = true;
isUsed[i][1] = true;
isUsed[i][9] = true;
isUsed[i][10] = true;
}
isUsed[2][2] = true;
isUsed[8][1] = false;
isUsed[9][2] = false;
solve(0, 2, 2);
System.out.println(counter);
scanner.close();
}
As a homework, I was asked to write a program which would convert a denary number to hexadecimal. What I've created kinda works but as the output it gives me reversed number and I have no idea how to solve it (it is my first program).
public static void main(String[] args) {
System.out.println("Give a denary number: ");
Scanner sc = new Scanner(System.in);
int dec1 = sc.nextInt();
String dec = Integer.toString(dec1);
int zmienna;
for(int i = 0; i < dec.length(); i++) {
zmienna = dec1 % 16;
dec1 = dec1 / 16;
if(zmienna == 10) {
System.out.print("A");
}
else if (zmienna == 11) {
System.out.print("B");
}
else if (zmienna == 12) {
System.out.print("C");
}
else if (zmienna == 13) {
System.out.print("D");
}
else if (zmienna == 14) {
System.out.print("E");
}
else if (zmienna == 15) {
System.out.print("F");
}
else if (zmienna == 0 & i == dec.length() - 1) {
System.out.print("");
}
else {
System.out.print(zmienna);
}
}
}
I just change a little in your program i just tried to solve your problem and it works. I just added a StringBuilder and append characters and at the last reverse it.
code
import java.util.Scanner;
public class A {
public static void main(String[] args) {
System.out.println("Give a denary number: ");
Scanner sc = new Scanner(System.in);
int dec1 = sc.nextInt();
StringBuilder sb =new StringBuilder();
String dec = Integer.toString(dec1);
int zmienna;
for(int i = 0; i < dec.length(); i++) {
zmienna = dec1 % 16;
dec1 = dec1 / 16;
if(zmienna == 10) {
//System.out.print("A");
sb.append("A");
}
else if (zmienna == 11) {
//System.out.print("B");
sb.append("B");
}
else if (zmienna == 12) {
//System.out.print("C");
sb.append("C");
}
else if (zmienna == 13) {
//System.out.print("D");
sb.append("D");
}
else if (zmienna == 14) {
//System.out.print("E");
sb.append("E");
}
else if (zmienna == 15) {
// System.out.print("F");
sb.append("F");
}
else if (zmienna == 0 & i == dec.length() - 1) {
System.out.print("");
}
else {
//System.out.print(zmienna);
sb.append(zmienna);
}
}
System.out.println(sb.reverse());
}
}
I used Luhn algorithm but, for some reason all of the card numbers I try it always displays that the card number is valid even if its not.
This is my code so far I believe the issue is with the toString method but I have no idea on how to fix it. Please help!
public class CreditCard
{
private String number;
private String result;
private int sum = 0;
private int n = 0;
private boolean value = true;
private StringBuilder builder = new StringBuilder();
private String word = builder.toString();
public CreditCard(String number)
{
this.number = number;
}
public String toString()
{
for(int x = 0; x < number.length(); x++)
{
char c = number.charAt(x);
if(Character.isDigit(c)){
builder.append(c);
}
}
String result = builder.toString() + " was issued by " + getIssuer();
if(isValid())
result += " and is valid.";
else
result += " and is not valid.";
return result;
}
public String getIssuer()
{
if(builder.length() == 13 && builder.substring(0, 1).equals("4") )
result = "VISA";
else if(builder.length() == 14)
{
if(builder.substring(0, 2).equals("36") || builder.substring(0, 2).equals("38")
|| builder.substring(0, 3).equals("300") || builder.substring(0, 3).equals("301")
|| builder.substring(0, 3).equals("302") || builder.substring(0, 3).equals("303")
|| builder.substring(0, 3).equals("304") || builder.substring(0, 3).equals("305"))
result = "Diner's Club";
else
result = "Unknown";
}
else if(builder.length() == 15)
{
if(builder.substring(0, 2).equals("34") || builder.substring(0, 2).equals("37"))
result = "American Express";
else
result = "Unknown";
}
else if(builder.length() == 16)
{
if(builder.substring(0, 2).equals("51") || builder.substring(0, 2).equals("52")
|| builder.substring(0, 2).equals("53") || builder.substring(0, 2).equals("54")
|| builder.substring(0, 2).equals("55"))
{
result = "MasterCard";
}
else if(builder.substring(0, 4).equals("6011"))
{
result = "Discover";
}
else if(builder.substring(0, 1).equals("4"))
{
result = "VISA";
}
else
result = "Unknown";
}
else
result = "Unknown";
return result;
}
public boolean isValid()
{
if(word.length() % 2 == 0){
for(int i = 0; i < word.length(); i++)
{
char c = word.charAt(i);
int num = Character.getNumericValue(c);
if(i % 2 == 0)
{
n = num * 2;
if(n > 9)
{
n -=9;
}
sum +=n;
}
else
sum+= num;
}
}
else
{
for(int x = 0; x < word.length(); x++)
{
char c = word.charAt(x);
int num = Character.getNumericValue(c);
if(x % 2 != 0)
{
n = num * 2;
if(n > 9)
{
n -=9;
}
sum +=n;
}
else
sum += num;
}
}
if(sum % 10 == 0)
value = true;
else
value = false;
return value;
}
}
First of all you need to revise your method, I would put an arguments into getIssuer and isValid method. These method are public and they could be called from outside of the class and they return something, which is not correct.
If not possible, then they have to use number instance variable.
The code returns wrong results, because you are not setting the value of word variable, it is null.
private byte isTargetEast(IRobot robot) {
byte resultE = 0;
if (robot.getLocation().x > robot.getTargetLocation().x) {
resultE = -1;
} else if (robot.getLocation().x < robot.getTargetLocation().x) {
resultE = 1;
} else {
resultE = 0; }
return resultE;
// returning 1 for 'yes', -1 for 'no' and 0 for 'same latitude'.
}
gives errors 'erro: illegal start of expression' and 'error ';' expected, 6 errors total, 2 illegal starts, 4semicolons, all on the private byte line
The syntax of the if-condition is totally wrong. What you want to do? You are mixing assign and condition code.
You can do assign like this:
if (chanceDir >= 0.5) {
Dir = IRobot.AHEAD;
Vir = IRobot.BEHIND;
Hir = IRobot.AHEAD;
Sir = IRobot.BEHIND;
} else {
Dir = IRobot.RIGHT;
Vir = IRobot.LEFT;
Hir = IRobot.LEFT;
Sir = IRobot.RIGHT;
}
if (oppchanceDir >= 0.5) {
Dir = IRobot.BEHIND;
Vir = IRobot.AHEAD;
Hir = IRobot.BEHIND;
Sir = IRobot.AHEAD;
}else {
Dir = IRobot.LEFT;
Vir = IRobot.RIGHT;
Hir = IRobot.RIGHT;
Sir = IRobot.LEFT;
}
Or you can do check conditions like that:
if (chanceDir >= 0.5 && ((Dir == IRobot.AHEAD) && (Vir == IRobot.BEHIND) && (Hir == IRobot.AHEAD) && (Sir == IRobot.BEHIND))) {
// do something here
}else if((Dir == IRobot.RIGHT) && (Vir == IRobot.LEFT) && (Hir == IRobot.LEFT) && (Sir == IRobot.RIGHT)){
// do something here
}else{
// do something here
}
if (oppchanceDir >= 0.5 && ((Dir == IRobot.BEHIND) && (Vir == IRobot.AHEAD) && (Hir == IRobot.BEHIND) && (Sir == IRobot.AHEAD))) {
// do something here
} else if ((Dir == IRobot.LEFT) && (Vir == IRobot.RIGHT) && (Hir == IRobot.RIGHT) && (Sir == IRobot.LEFT)) {
// do something here
}else{
// do something here
}