Finding exit from a maze - java

I wrote this code for a programming assignment given in a course about graphs on Coursera. enter code here. It passes on the test cases given in the question description but fails on one when submitted I'm new to graphs. Can some one help me find the error in this code?
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Iterator;
public class Reachability {
private static boolean[] visited;
private static int reach(ArrayList<Integer>[] adj, int x, int y) {
if(x == y){
return 1;
}
visited[x] = true;
Iterator itr = adj[x].iterator();
while(itr.hasNext()){
x = (int)itr.next();
if(!visited[x]){
return reach(adj,x,y);
}
}
return 0;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
ArrayList<Integer>[] adj = (ArrayList<Integer>[])new ArrayList[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<Integer>();
}
for (int i = 0; i < m; i++) {
int x, y;
x = scanner.nextInt();
y = scanner.nextInt();
adj[x - 1].add(y - 1);
adj[y - 1].add(x - 1);
}
int x = scanner.nextInt() - 1;
int y = scanner.nextInt() - 1;
visited = new boolean[n];
System.out.println(reach(adj, x, y));
}
}
Given below is the failed test case.
Failed case #6/16: (Wrong answer)
Input:
100 100
27 96
6 9
81 98
21 94
22 68
76 100
8 50
38 86
71 75
32 93
16 50
71 84
6 72
22 58
7 19
19 76
44 75
24 76
31 35
11 89
42 98
63 92
37 38
20 98
45 91
23 53
37 91
76 93
67 90
12 22
43 52
23 56
67 68
1 21
17 83
63 72
30 32
7 91
50 69
38 44
55 89
15 23
11 72
28 42
22 69
56 79
5 83
55 73
13 72
7 93
20 54
21 55
66 89
2 91
18 88
26 64
11 61
28 59
12 86
42 95
17 82
50 66
66 99
40 71
20 40
5 66
92 95
32 46
7 36
44 94
6 31
19 67
26 57
53 84
10 68
28 74
34 94
25 61
71 88
10 89
28 52
72 79
39 73
11 80
44 79
13 77
30 96
30 53
10 39
1 90
40 91
62 71
44 54
15 17
69 74
13 67
24 69
34 96
21 50
20 91
42 46
Your output:
0
Correct output:
1

The problem is that you do an early return from reach() if y cannot be reached via the first unvisited neighbor of x:
return reach(adj,x,y);
What you want to do instead is:
If y can be reached then return 1.
If y cannot be reached then continue with the next neighbor
That would read:
if (reach(adj, x, y) == 1)
return 1;
Unrelated comment: You may want to use Iterator<Integer> instead of the raw type Integer. This avoids the cast and compiler warnings.

Related

Using priority queue with File Reader

I want to order numbers between the range of 1-100 with File Reader. It's very weird otput. My code:
import java.io.*;
import java.util.*;
public class Main {
public static String strLine;
public static int order = 1;
public static void main(String[] args) {
Queue<String> queue = new PriorityQueue<String>();
try{
FileReader fr = new FileReader("File.txt");
BufferedReader br = new BufferedReader(fr);
while ((strLine = br.readLine()) != null) {
queue.offer(strLine);
}
br.close();
} catch (IOException e){
System.out.println("File not found");
}
while (!queue.isEmpty()){
System.out.println(order + ".Number: " + queue.poll());
order++;
}
} }
File.txt:
81
56
42
3
49
100
61
76
74
79
6
90
34
93
22
71
72
63
67
18
14
5
91
39
53
86
40
45
60
55
48
62
8
4
80
11
87
47
15
46
97
66
43
58
41
54
33
96
82
44
98
27
85
10
32
95
35
24
7
30
83
9
31
99
25
29
57
36
69
50
28
59
12
37
78
73
70
89
65
51
77
88
20
17
21
94
64
19
26
23
92
52
2
16
68
75
38
1
84
13
output:
1.Number: 1
2.Number: 10
3.Number: 100
4.Number: 11
5.Number: 12
6.Number: 13
7.Number: 14
8.Number: 15
9.Number: 16
10.Number: 17
11.Number: 18
12.Number: 19
13.Number: 2
14.Number: 20
15.Number: 21
16.Number: 22
17.Number: 23
18.Number: 24
19.Number: 25
20.Number: 26
21.Number: 27
22.Number: 28
23.Number: 29
24.Number: 3
25.Number: 30
26.Number: 31
27.Number: 32
28.Number: 33
29.Number: 34
30.Number: 35
31.Number: 36
32.Number: 37
33.Number: 38
34.Number: 39
35.Number: 4
36.Number: 40
37.Number: 41
38.Number: 42
39.Number: 43
40.Number: 44
41.Number: 45
42.Number: 46
43.Number: 47
44.Number: 48
45.Number: 49
46.Number: 5
47.Number: 50
48.Number: 51
49.Number: 52
50.Number: 53
51.Number: 54
52.Number: 55
53.Number: 56
54.Number: 57
55.Number: 58
56.Number: 59
57.Number: 6
58.Number: 60
59.Number: 61
60.Number: 62
61.Number: 63
62.Number: 64
63.Number: 65
64.Number: 66
65.Number: 67
66.Number: 68
67.Number: 69
68.Number: 7
69.Number: 70
70.Number: 71
71.Number: 72
72.Number: 73
73.Number: 74
74.Number: 75
75.Number: 76
76.Number: 77
77.Number: 78
78.Number: 79
79.Number: 8
80.Number: 80
81.Number: 81
82.Number: 82
83.Number: 83
84.Number: 84
85.Number: 85
86.Number: 86
87.Number: 87
88.Number: 88
89.Number: 89
90.Number: 9
91.Number: 90
92.Number: 91
93.Number: 92
94.Number: 93
95.Number: 94
96.Number: 95
97.Number: 96
98.Number: 97
99.Number: 98
100.Number: 99
Process finished with exit code 0
Since it is a queue of Strings it is sorted alphabetically. If you want to keep it as Strings, you can use a custom comparator that converts the Strings to ints and compares those:
// This constructor requires an initial capacity before the comparator
Queue<String> queue = new PriorityQueue<String>(100, new Comparator<String>() {
public int compare(String s1, String s2) {
return Integer.parseInt(s1) - Integer.parseInt(s2);
}
});
Another option is to change the type of the queue to Integer and convert the input:
Queue<Integer> queue = new PriorityQueue<Integer>();
...
while ((strLine = br.readLine()) != null) {
queue.offer(Integer.parseInt(strLine));
}

Custom iterator to print every second member of the ArrayList

so basically I am trying to write my own custom iterator to print every second number of an ArrayList. I populated the arraylist with ints from 0 up to 100.
But somehow it still not using my custom Iterator, oddly print out every single int in my arrayList.
Am I approaching this the wrong way?
Code:
public class IteratorTesting implements Iterable{
ArrayList<Integer> rand;
public IteratorTesting(){
rand = new ArrayList<>();
}
public ArrayList<Integer> getRand(){
return rand;
}
public int getSize(){
return rand.size();
}
#Override
public Iterator iterator() {
return new MyIterator();
}
public class MyIterator implements Iterator<Object>{
int currentIndex=0;
#Override
public boolean hasNext() {
if(currentIndex<rand.size()){
return true;
}else{
return false;
}
}
#Override
public Object next() {
return rand.get(currentIndex+=2);
}
}
public static void main(String[] args) {
IteratorTesting name = new IteratorTesting();
for (int i = 0; i < 100; i++) {
name.getRand().add(i);
}
Iterator it = name.getRand().iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}
Output:
run:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
On this line
Iterator it = name.getRand().iterator();
you've fetched the Iterator for the ArrayList, instead of your custom iterator. So you're just iterating the list in the regular way.

Unable to read Inputs from Console in java

I need to read input test cases from console into a 2D array and a 1D array but unable to find the error in my code
import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static final int SIZE = 5;
public static int card[][];
public static int ball[];
public static void read()throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count=0;
for(int row = 0; row < SIZE && count < 25; row++){
String line= br.readLine();
System.out.println("line"+line);
//if((line= br.readLine()) != null) {
int column = 0;
StringTokenizer st = new StringTokenizer(line);
while(st.hasMoreTokens()){
card[row][column] = Integer.parseInt(st.nextToken());
column++;
count++;
}
column = 0;
// }
}
int i = 0, b=0;
String line ;
while((line =br.readLine() )!= null && i < 75) {
StringTokenizer st1 = new StringTokenizer(line);
while(st1.hasMoreTokens()){
ball[i++]= Integer.parseInt(st1.nextToken());
}
}
}
public static void printing() {
System.out.println("Card values\n");
for(int i=0;i<SIZE;i++) {
for(int j=0;j<SIZE;j++){
System.out.print(card[i][j]+"\t");
}
System.out.println();
}
System.out.println("Ball values\n");
for(int j=0;j<75;j++) {
System.out.print(ball[j]+"\t");
//System.out.println();
}
}
public static void main (String[] args) throws java.lang.Exception
{
card = new int[SIZE][SIZE];
ball = new int[75];
for(int t=0 ; t<2 ; t++) {
read();
printing();
}
}
}
Also the test case input is as follows
10 17 44 59 62
2 28 31 58 68
5 16 37 53 71
6 26 35 51 66
9 21 34 60 70
45 37 19 47 16 39 66 14 28 15
72 17 62 12 55 11 73 75 9 18
56 4 29 32 61 63 51 38 33 2
8 36 6 24 23 22 21 5 60 35
41 74 34 7 67 25 50 10 43 53
3 46 68 40 48 69 54 30 20 70
31 59 57 49 1 42 58 27 52 13
64 44 71 26 65
1 7 4 9 2
2 28 31 58 68
5 16 37 53 71
6 26 35 51 66
9 21 34 60 70
45 37 19 47 16 39 66 14 28 15
72 17 62 12 55 11 73 75 9 18
56 4 29 32 61 63 51 38 33 2
8 36 6 24 23 22 21 5 60 35
41 74 34 7 67 25 50 10 43 53
3 46 68 40 48 69 54 30 20 70
31 59 57 49 1 42 58 27 52 13
64 44 71 26 65
I am able to read the first test case but while reading the 2nd test case i am getting null error.
On your second pass:
for(int t=0 ; t<2 ; t++) {
read();
You create a second BufferedReader from System.in.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
It looks like you're expecting this to re-read the input. But all the input has already been read.
You might be able to reset this small input stream with a combination of InputStream.readLimit(), InputStream.mark() and InputStream.reset(). Or you could put the data in a file, and create a new FileInputStream for each read.

Sorting a matrix by rows in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have to get a matrix with rows sorted (descending) like sortrows function in matlab.
A =
95 45 92 41 13 1 84
95 7 73 89 20 74 52
95 7 73 5 19 44 20
95 7 40 35 60 93 67
76 61 93 81 27 46 83
76 79 91 0 19 41 1
to
B =
76 61 93 81 27 46 83
76 79 91 0 19 41 1
95 7 40 35 60 93 67
95 7 73 5 19 44 20
95 7 73 89 20 74 52
95 45 92 41 13 1 84
I am using JAMA which not contains a method like sortrows in matlab.
Which is the fastest way of doing this?
One way to do this is to sort rows from last column to first:
int n = A.length;
for (int c = n - 1; c >= 0; c--) {
// bubble sort
for (int i = 0; i < n; i++) {
for (int j = i; j < n ;j++) {
if (A[i][c] < A[j][c]) {
SWAP(A[i], A[j]); // swap the whole row
}
}
}
}
Time Complexity = O(n * O(sorting algo))
Well if you have a row such as
A = 95 45 92 41 13 1 84 95 7 73 89 20 74 52 95 7 73 5 19 44 20 95 7 40 35 60 93 67 76 61 93 81 27 46 83 76 79 91 0 19 41 1
and you need to sort it to
B = 76 61 93 81 27 46 83 76 79 91 0 19 41 1 95 7 40 35 60 93 67 95 7 73 5 19 44 20 95 7 73 89 20 74 52 95 45 92 41 13 1 84
You could probably write
A = B;
Would this be an acceptable solution? Could you still get an 'A' by doing that?
#codetrolling
I don't know what Jama is, but if your data is stored in a 2 Dimension array then you can use the Column Comparator to sort the data on any column.

Project Euler #11 java. Not getting the right answer

I have some problems with Problem 11 on Project Euler.
I manage to get an answer, but it's not the right one. I'm getting 51267216.
This is found by the greatestVert method. I think the problem is located in the greatestDiaglonal method, but I'm not quite sure. Can somebody check if my algorithm is correct?
The task is to find out what the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) is in the 20x20 grid.
package euler;
public class Problem11 {
int num1, num2, num3, num4;
int highestNum1, highestNum2, highestNum3, highestNum4;
private int sum = 0;
String[] l = {
"08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08",
"49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00",
"81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65",
"52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91",
"22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80",
"24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50",
"32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70",
"67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21",
"24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72",
"21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95",
"78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92",
"16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57",
"86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58",
"19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40",
"04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66",
"88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69",
"04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36",
"20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16",
"20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54",
"01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"
};
int greatestSide() {
for(int i = 0;i<=19;i++) {
for(int n = 0;n<=l[i].length()-12;n+=3) {
num1 = Integer.parseInt(l[i].substring(0+n,3+n).trim());
num2 = Integer.parseInt(l[i].substring(3+n,6+n).trim());
num3 = Integer.parseInt(l[i].substring(6+n,9+n).trim());
num4 = Integer.parseInt(l[i].substring(9+n,12+n).trim());
if(num1*num2*num3*num4 > sum) {
sum = num1*num2*num3*num4;
highestNum1 = num1;
highestNum2 = num2;
highestNum3 = num3;
highestNum4 = num4;
}
}
}
return sum;
}
int greatestVert() {
for(int i = 0; i<=16; i++) {
for(int n = 0; n<=l[i].length()-3; n+=3) {
num1 = Integer.parseInt(l[i].substring(0+n,3+n).trim());
num2 = Integer.parseInt(l[i+1].substring(0+n,3+n).trim());
num3 = Integer.parseInt(l[i+2].substring(0+n,3+n).trim());
num4 = Integer.parseInt(l[i+3].substring(0+n,3+n).trim());
if(num1*num2*num3*num4 > sum) {
sum = num1*num2*num3*num4;
highestNum1 = num1;
highestNum2 = num2;
highestNum3 = num3;
highestNum4 = num4;
}
}
}
return sum;
}
int greatestDiagonal() {
for(int i = 19; i>=3; i--) {
for(int n = 0; n<=l[i].length()-12; n+=3) {
num4 = Integer.parseInt(l[i].substring(9+n,12+n).trim());
num3 = Integer.parseInt(l[i-1].substring(6+n,9+n).trim());
num2 = Integer.parseInt(l[i-2].substring(3+n,6+n).trim());
num1 = Integer.parseInt(l[i-3].substring(0+n,3+n).trim());
if(num1*num2*num3*num4 > sum) {
sum = num1*num2*num3*num4;
highestNum1 = num1;
highestNum2 = num2;
highestNum3 = num3;
highestNum4 = num4;
}
}
}
for(int i = 0; i>=16; i++) {
for(int n = 0; n<=l[i].length()-12; n+=3) {
num4 = Integer.parseInt(l[i].substring(9+n,12+n).trim());
num3 = Integer.parseInt(l[i+1].substring(6+n,9+n).trim());
num2 = Integer.parseInt(l[i+2].substring(3+n,6+n).trim());
num1 = Integer.parseInt(l[i+3].substring(0+n,3+n).trim());
if(num1*num2*num3*num4 > sum) {
sum = num1*num2*num3*num4;
highestNum1 = num1;
highestNum2 = num2;
highestNum3 = num3;
highestNum4 = num4;
}
}
}
return sum;
}
public static void main(String[] args) {
Problem11 prog = new Problem11();
prog.greatestSide();
prog.greatestVert();
prog.greatestDiagonal();
System.out.println(prog.sum);
System.out.println(prog.highestNum1);
System.out.println(prog.highestNum2);
System.out.println(prog.highestNum3);
System.out.println(prog.highestNum4);
}
}
You are omitting to scan one direction of diagonals:
for(int i = 0; i>=16; i++) {
doesn't run at all. You meant i <= 16 there.
Here is another solution that firstly converts the String to a 2D array:
public class P11 {
public static void main(String[] args) throws Exception {
String input = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48";
int n = 20;
int a[][] = getMatrixOfIntegers(input, n);
System.out.println(maxProduct(a));
}
private static long maxProduct(int a[][]) {
int size = a.length - 4;
int l = a.length - 1;
long pMax = 1;
for ( int i = 0 ; i < size ; i++ ) {
for ( int j = 0 ; j < size ; j++ ) {
int prodDiag1 = a[i][j]*a[i+1][j+1]*a[i+2][j+2]*a[i+3][j+3];
if ( pMax < prodDiag1 ) {
pMax = prodDiag1;
}
int prodCol = a[i][j]*a[i+1][j]*a[i+2][j]*a[i+3][j];
if ( pMax < prodCol ) {
pMax = prodCol;
}
int prodRow = a[i][j]*a[i][j+1]*a[i][j+2]*a[i][j+3];
if ( pMax < prodRow ) {
pMax = prodRow;
}
int prodDiag2 = a[i][l-j]*a[i+1][l-j-1]*a[i+2][l-j-2]*a[i+3][l-j-3];
if ( pMax < prodDiag2 ) {
pMax = prodDiag2;
}
}
}
return pMax;
}
private static int[][] getMatrixOfIntegers(String input, int n) {
String m[] = input.split(" ");
int a[][] = new int[n][n];
for ( int i = 0 ; i < n*n ; i++ ) {
a[i/n][i%n] = Integer.parseInt(m[i]);
}
return a;
}
}
Time for maxProduct: 0.2 ms

Categories