I'm trying to read a file and generate a 2D Array. So I believe my constructor will create the correct dimensions, I just don't know how to input the actual values into the arrays.
File format:
6
1 4 2 2 2 3
4 2 2 4 1 2
2 1 3 4 3 2
1 3 3 2 6 2
0 0 0 2 0 0
3 4 0 0 0 0
0 0 0 1 0 0
0 1 0 0 0 0
0 0 0 0 0 6
5 0 1 0 0 4
The file input is on the left, and the board result should look like the right :
6 | 1 4 2 2 2 3
1 4 2 2 2 3 | -----------
4 2 2 4 1 2 | 1|. . . 2 . .|4
2 1 3 4 3 2 | 3|3 4 . . . .|2
1 3 3 2 6 2 | 3|. . . 1 . .|2
0 0 0 2 0 0 | 2|. 1 . . . .|4
3 4 0 0 0 0 | 6|. . . . . 6|1
0 0 0 1 0 0 | 2|5 . 1 . . 4|2
0 1 0 0 0 0 | -----------
0 0 0 0 0 6 | 2 1 3 4 3 2
5 0 1 0 0 4 |
this first line of the file is the size of the board (6x6).
The second line is the "North to South(NS)" facing values
The third line is the "East to West(EW)" facing values
The fourth line is the "South to North(SN)" facing values
The fifth line is the "West to East(WE)" facing values.
And the rest of the lines will populate the board. A 0 will put nothing in.
public static final int EMPTY = 0;
int[][] board;
int dim;
int[] NS, SN, EW, WE; //the outter arrays
public SkyscraperConfig(Scanner f){
while(f.hasNextLine()){
if(f.nextLine().length() == 1){
dim = f.nextInt();
}
else{
outterArrays = f.nextLine().length();
}
}
this.board = new int[dimension+1][dimension+1];//I made the dimension +1 to hold the outter arrays that hold the NS, SN, EW, and WE values
this.NS = new int[outterArrays+1];
this.SN = new int[outterArrays+1];
this.EW = new int[outterArrays+1];
this.WE = new int[outterArrays+1];
}
My Idea was to create a 2D Array that is the size of the first line in the file. Then for the outer values, create four arrays which will represent the outside. I don't know how to put those outer arrays into my 2D array though.
As with all file reading, try to separate each task distinct task. Ask yourself "What do I need to know before I do , and what do I have to do in order to complete ?" Hopefully the tasks are listed in order (each task only requires information above it in the file), which is the case for your problem.
Your task seems to involve three sub tasks:
Figure out how large the arrays and matrix need to be (1 line)
Read in the side arrays (4 lines)
Read in the matrix (N lines)
So let's work with that:
int[][] board;
int dim;
int[] NS, SN, EW, WE; //the outter arrays
public SkyscraperConfig(Scanner f){
//First line should be dimension line
int dim = Integer.parseInt(f.nextLine());
//Initalize data structures based off of this dimension
this.NS = new int[dim];
this.SN = new int[dim];
this.EW = new int[dim];
this.WE = new int[dim];
this.board = new int[dim][dim];
//Read in side arrays
//...
//Read in the board
//...
}
Here we can guess that we're going to have a lot of duplicated code in reading the lines - probably time to start designing helper methods. One thing we seem to be doing a lot of is reading in a line and parsing all of the ints in it. So let's write a method for that
private static int[] parseIntLine(String line){
String[] arr = line.trim().split(' '); //Split on the space character
int[] intArr = new int[arr.length];
for(int i = 0; i < arr.length; i++){
intArr[i] = Integer.parseInt(arr[i]);
}
return intArr;
}
Now we can use this method to finish up our implementation, letting the reading take care of the array length:
public SkyscraperConfig(Scanner f){
//First line should be dimension line
int dim = Integer.parseInt(f.nextLine());
//Only actual initialization we have to do is for the matrix's outer array
board = new int[dim][];
//Read in side arrays
NS = parseIntLine(f.nextLine());
EW = parseIntLine(f.nextLine());
SN = parseIntLine(f.nextLine());
WE = parseIntLine(f.nextLine());
//Read in the board - dim rows to read
for(int i = 0; i < dim; i++){
board[i] = parseIntLine(f.nextLine());
}
f.close();
}
Now there's a lot of things that could go wrong that you should probably account for. What if the first line contains more than just one number? What if the first line contains a non-integer value? What if one of the side arrays or one of the board rows is of the wrong length? What if there aren't enough rows to fill the board? What if there are too many rows? For each of these questions you should either handle the case within the method (via try/catch or if-else logic) or if it's an unfixable problem throw some kind of exception.
Related
My scanner is not accessing last line of my text file correctly, thus is storing the last line of the text file as all 0's into my 2D array instead of what is actually there. I believe I have provided everything that would give context as to what is going wrong, but if more info is needed I can update this question, thanks in advance.
//Creates 2-d array object, stores values from file.
public DominoSort (String fileName) throws java.io.FileNotFoundException{
this.grid = new int [7][8]; //2-d array to hold values from text file
Scanner br = new Scanner(new File(fileName));
String line = br.nextLine();
int r = 0;
int c = 0;
while (br.hasNextLine) {
String[] row = line.split("\\s+");
for (String s : row) {
this.grid[r][c] = Integer.parseInt(s);
c++;
}
line = br.nextLine();
r++;
c = 0;
}
//this.setS = new ArrayList<>();
//this.pairMappings = new ArrayList<ArrayList<dominoLocation>>();
br.close();
}
//Print grid function, prints out the grid
public void printGrid() {
for(int r = 0; r < this.grid.length; r++) {
System.out.println("");
for(int c = 0; c < this.grid[r].length; c++) {
System.out.print(this.grid[r][c] + " ");
}
}
System.out.println("");
}
//Driver for checking
public static void main(String[] args) throws IOException {
// String line;
//System.out.println(new File(".").getAbsolutePath());
Scanner input = new Scanner(System.in); //get textfile name from user input
System.out.print("Enter the file name: ");
String fileName = input.next();
DominoSort dom = new DominoSort(fileName); //this call populates the 2-d array object
//dom.solvePuzzle(6);
dom.printGrid(); //prints 2d array for output
//dom.solvePuzzle(6);
}
text file used for testing / expected output:
3 3 4 2 2 0 0 0
4 6 3 6 3 1 4 1
5 5 4 1 2 1 6 5
5 6 0 2 1 1 5 3
5 4 4 2 6 0 2 6
3 0 4 6 6 1 3 1
2 0 3 2 5 0 5 4 {Notice this line}
actual output:
3 3 4 2 2 0 0 0
4 6 3 6 3 1 4 1
5 5 4 1 2 1 6 5
5 6 0 2 1 1 5 3
5 4 4 2 6 0 2 6
3 0 4 6 6 1 3 1
0 0 0 0 0 0 0 0 {this line is not right}
Your problem lies within the nested while/for loop. It reaches the end condition before all the lines are read. (The nextLine() method doesn't have any more lines before you read the last line). You can see this by putting an extra 1 or 2 lines in your file at the very end, making it show the last lines.
There are a few ways to fix it, one of them is by just adding on an extra for loop after the while loop to compute the last line individually:
while (br.hasNextLine()) {
String[] row = line.split("\\s+");
for (String s : row) {
this.grid[r][c] = Integer.parseInt(s);
c++;
}
line = br.nextLine();
r++;
c = 0;
}
String[] row = line.split("\\s+");
for (String s : row) {
this.grid[r][c] = Integer.parseInt(s);
c++;
}
or alternatively, don't increment the line on the first run:
while (br.hasNextLine()) {
String[] row = line.split("\\s+");
for (String s : row) {
this.grid[r][c] = Integer.parseInt(s);
c++;
}
if (r != 0)
line = br.nextLine();
r++;
c = 0;
}
I have a sudoku puzzle 9x9 in a text file and I wondering how can we create a Graph from sudoku puzzle.Sudoku puzzle is a int[][] Example puzzle
0 0 0 0 9 8 0 4 5
0 0 4 3 2 0 7 0 0
7 9 0 5 0 0 0 3 0
0 0 0 9 0 0 4 0 0
0 4 5 0 0 2 8 0 0
8 7 9 6 0 4 0 1 0
0 3 0 0 7 9 0 6 4
4 5 0 2 1 3 9 0 8
0 8 7 4 6 5 0 0 0
and class Graph
class Graph
{
private int V;
private LinkedList<Integer> adj[];
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
void addEdge(int v,int w)
{
adj[v].add(w);
adj[w].add(v);
}
public int getV()
{
return V;
}
public LinkedList<Integer> getListAdj(int u)
{
return adj[u];
}
I write a function to read puzzle from a text file and implement it to graph
public boolean readGraph(String input_name, int result[])
{
return true;
}
But I stuck in this step.
Here goes:
First, I already know that the puzzle is 9x9, so the newlines in the text file are meaningless to me. I can ignore them. Also, I know that each element of the puzzle is only ever going to be a single character long so (after getting the text file into memory like so: Reading a plain text file in Java):
I get my String puzzle which is that text file into memory. Now I want to iterate over that String like so:
Graph graph = new Graph(81); //9x9 graph with 81 verticies
int whichVertextAreWeOn = 0;
for (int i = 0; i < puzzle.length(); i++){
char c = puzzle.charAt(i);
if(c>'0' && c > '9'){
int integer = Integer.parseInt(c);
//now I need to add this to my Graph... Saving my work now, comments are appreciated
//Turns out you simply add edges to each vertex in a graph, the vertex itself has no value...
//so I'm going to keep track of which vertex I'm on, this is starting to seem like a bad data structure for this purpose, but I shall carry on. -Adding an iterator to keep track of which vertex I'm on.
for(int w = 0; w < graph.V(); w++;){
if(vertextIsLinked(whichVertextAreWeOn, w))
graph.addEdge(whichVertextAreWeOn, w);
}
//hum... from here I need to add an edge to this vertex for each connected vertex...
}
}
private boolean vertextIsLinked(int vertexWeAreOn, int possibleVertext){
//use rules of Sukdoku to figure out if these two verticies should be linked. This would return true for any vertex in horizontal or vertical alignment to the vertexWeAreOn.
}
I was solving the Connected Sets problem on Amazon's Interview Street site https://amazon.interviewstreet.com/challenges and my code worked perfectly for the public sample test cases provided by the site, but I'm getting a NumberFormatException for the hidden test cases on line 25. Here is the part of my code that parses the input:
public class Solution
{
static int [][] arr;
static int num = 2;
static int N;
static String output = "";
public static void main(String[] args) throws IOException
{
int T;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
T = Integer.parseInt(reader.readLine());
int i, j, k;
for(i=0;i<T;i++) //the loop for each of the 'T' test cases
{
N = Integer.parseInt(reader.readLine()); //line 25
arr = new int[N][N];
for(j=0;j<N;j++) //the loops for storing the input 2D array
{
for(k=0;k<N;k++)
{
arr[j][k] = Character.getNumericValue(reader.read());
reader.read();
}
}
I spent a lot of time trying to find what the problem is, but I've been unsuccessful at it. Thanks for your help in advance.
EDIT: The problem statement is given as follows on the site:
Given a 2–d matrix, which has only 1’s and 0’s in it. Find the total number of connected sets in that matrix.
Explanation:
Connected set can be defined as group of cell(s) which has 1 mentioned on it and have at least one other cell in that set with which they share the neighbor relationship. A cell with 1 in it and no surrounding neighbor having 1 in it can be considered as a set with one cell in it. Neighbors can be defined as all the cells adjacent to the given cell in 8 possible directions ( i.e N , W , E , S , NE , NW , SE , SW direction ). A cell is not a neighbor of itself.
Input format:
First line of the input contains T, number of test-cases.
Then follow T testcases. Each testcase has given format.
N [ representing the dimension of the matrix N X N ].
Followed by N lines , with N numbers on each line.
Output format:
For each test case print one line, number of connected component it has.
Sample Input:
4
4
0 0 1 0
1 0 1 0
0 1 0 0
1 1 1 1
4
1 0 0 1
0 0 0 0
0 1 1 0
1 0 0 1
5
1 0 0 1 1
0 0 1 0 0
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
8
0 0 1 0 0 1 0 0
1 0 0 0 0 0 0 1
0 0 1 0 0 1 0 1
0 1 0 0 0 1 0 0
1 0 0 0 0 0 0 0
0 0 1 1 0 1 1 0
1 0 1 1 0 1 1 0
0 0 0 0 0 0 0 0
Sample output:
1
3
3
9
Constraint:
0 < T < 6
0 < N < 1009
Note that the above sample test cases worked on my code. The hidden test cases gave the exception.
Okay, so I modified my program to incorporate LeosLiterak's suggestion to use trim() and the new code is as follows:
public class Solution
{
static int [][] arr;
static int num = 2;
static int N;
static String output = "";
public static void main(String[] args) throws IOException
{
int T;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
T = Integer.parseInt(reader.readLine().trim());
int i, j, k;
for(i=0;i<T;i++)
{
N = Integer.parseInt(reader.readLine().trim());
arr = new int[N][N];
for(j=0;j<N;j++)
{
String [] temp = reader.readLine().trim().split(" ");
for(k=0;k<N;k++)
{
arr[j][k] = Integer.parseInt(temp[k]);
}
}
So instead of reading each character in the input matrix and converting it to an integer and storing, I now read the entire line and trim and split the string and convert each substring into an integer and store in my array.
Copied from comments: try to remove all white characters like spaces, tabulators etc. These characters are not prohibited by goal definition but they cannot be parsed to number. You must trim them first.
Check if its number before parse to int.
Character.isNumber()
T = Integer.parseInt(reader.readLine()); This where the problem. when user try to give string
and it convert into integer and run through loop? For eg : if user gives 'R' as char than how the integer conversion can happen and will the loop run further?? No right.
For example, say we have a Sudoku board like this:
0 0 6 5 8 9 7 4 3
0 5 0 0 0 0 0 6 0
7 0 9 0 6 0 1 0 0
0 3 0 0 0 2 0 8 7
0 0 1 0 0 0 4 0 0
8 9 0 6 0 0 0 5 0
0 0 2 0 5 0 3 0 6
0 7 0 0 0 0 0 9 0
3 1 8 4 9 6 5 0 0
I want to store it into one array such that the first 9 elements of the array are the first sub block, i.e. the values {0 0 6 0 5 0 7 0 9} and followed by {5 8 9 0 0 0 0 6 0}.
I've tried finding a solution but I always get an array index out of bounds error and it is too brute force. Something similar to this:
while(st.hasMoreTokens()) {
if(ctr == 27) {
c.addSubBlock(sb1);
c.addSubBlock(sb2);
c.addSubBlock(sb3);
sb1 = new SubBlock();
sb2 = new SubBlock();
sb3 = new SubBlock();
ctr = 0;
}
sb1.addElement(Integer.parseInt(st.nextToken()));
sb1.addElement(Integer.parseInt(st.nextToken()));
sb1.addElement(Integer.parseInt(st.nextToken()));
sb2.addElement(Integer.parseInt(st.nextToken()));
sb2.addElement(Integer.parseInt(st.nextToken()));
sb2.addElement(Integer.parseInt(st.nextToken()));
sb3.addElement(Integer.parseInt(st.nextToken()));
sb3.addElement(Integer.parseInt(st.nextToken()));
sb3.addElement(Integer.parseInt(st.nextToken()));
ctr+=9;
}
Please give me some tips. Code snippets would also be a great help.
EDIT: This thread somehow helped me figured it out. And yes, this is part of the Sudoku where I'm trying to encode the board into an array.
What I did was to transform first the input String into a 2d array (9x9) and use int block = (row/3)*3 + (col/3); to compute exactly which sub block each element belongs.
Create a 3x3 array of sub blocks
Use 2 counters (x & y) for tracking the position in the full board of each element read
Add the values at (x,y) into sub block (x/3,y/3)
Something like this:
SubBlock board[][] = new SubBlock[3][3];
int x, y;
for ( y=0; y<9; y++ )
for ( x=0; x<9; x++ )
board[y/3][x/3].addElement(Integer.parseInt(st.nextToken()));
board[0][0] will be the top-left SubBlock, board[2][2] the bottom-right one.
Store everything in a two dimension array. E.g.
int[] board = {
{1,2,3,4,5,6,7,8,9}
{1,2,3,4,5,6,7,8,9}
{1,2,3,4,5,6,7,8,9}
{1,2,3,4,5,6,7,8,9}
{1,2,3,4,5,6,7,8,9}
{1,2,3,4,5,6,7,8,9}
{1,2,3,4,5,6,7,8,9}
{1,2,3,4,5,6,7,8,9}
{1,2,3,4,5,6,7,8,9}};
//looping
public static void Main(string[] args){
for(int i = 0; i < 9; i++)
{
System.out.println("SubBlock number"+i);
for(int j = 0; j < 9; j++){
System.out.println(board[i][j]);
}
}
}
Assuming that you are reading input left to right, top to bottom, try a set of 4 nested loops like this:
int board[] = new int[81];
for (int outY = 0; outY < 3; outY++)
{
for (int outX = 0; outX < 3; outX++)
{
for (int inY = 0; inY < 3; inY++)
{
for (int inX = 0; inX < 3; inX++)
{
board[27*outY + 9*outX + 3 * inY + inX] = //parse an int from your input here
}
}
}
}
It would be great if we knew why you are trying to loop through the board.
If you want to check if you can enter a number, I recommend you use maps for each of the 3x3 squares.
Then check if the item is in the map already or not. For the rows and columns, you can either loop over the 2D array and check each element, or -again- use a map for each column and a map for each row.
I'm not entirely sure if you want an answer for a single-dimension array or if you're willing to make it a two-dimensional array (as you mention each nine element set with curly braces), but if two-dimensional is OK...
The OP in this Code Review posting used a 'fancy' way of sifting through the subgrids by using the math (i % 3) + rowStart inside one of the square brackets and (i / 3) + colStart inside the other. One commenter noted this modulo method to be a bit obscure, and I'm prone to agree, but for how clean it is and the fact that it works, I think it's a solid solution. So, paired with the iteration of the for loop, we can sift through each 'subgrid' cell, as well as each element of row + col.
for(i=0; i<9; ++i)
{
if (puzzle[row][i] == num) return 0;
if (puzzle[i][col] == num) return 0;
if (puzzle[rowStart + (i%3)][colStart + (i/3)] == num) return 0;
}
If we find a number in one of the cells that matches, it's a duplicate, and we exit the function as 'false', or, 0.
EDIT:
Now that I think of it, you could use this same trick for a single-dimension array by using i % 9 instead of 3. You could then determine which 'row' we're on by doing i / 9 and trusting that, since we're dealing with type ints, we'll truncate the unnecessary decimals.
This does verify that this trick is a bit prone towards N-1 indexed data, as someone would assume 'go to the 81st element' would mean go to the 9th column of the 9th row, but using 81 % 9 would yield 0, and 81 / 9 would yield 9, so we'd go to the 0th place at row 9.
Before the Floyd–Warshall/Dijkstra replies flood comes in please let me explain the situation as i'm sure either algorithm can be tuned for this case, and it has to be as this is not a toy example program (mind you, in java so have to keep it manageable memory-wise)
What i have is a web graph generated from node 0 to node n, node 3 cannot link to node 5, because node 5 didnt exist when node 3 was choosing it's out links. Every "node" is represented as in_neighbours[nodeID] and out_neighbours[nodeID] say nodeId=3, so we're talking about node 3. Note also that in_/out_ are both sorted, (in_ is naturally sorted as 5 will have chosen its out links all at once, only then 6 will choose out_links so 3's in_'s can never contain {6, 5, 7}) and ofc both can contain duplicates. (in/out are ArrayList arrays of size n, where out_ is always of size d or m, which along with n is specified at startup by the user)
No weights. What i must do is find the averageDistance()
public double getAvgDistance() {
int sum = 0;
for (int i=1; i<n; i++) {
for (int j=0; j < i; j++) {
sum += dist(i, j); // there are duplicates, make sure i skip
}
}
return (double)sum / (double)( ((n*(n-1)) / 2) );
}
What I have so far is the best case. Note i want only to find the distance between j & i, not all distances at the same time (not enough memory, it will be tested at m=20 d=1 000 000)
private int dist(int i, int j) {
int dist = 0;
for (int link : in_neighbours[j]) {
System.out.print("\nIs "+j+" linked to by "+i);
if (out_neighbours[i].contains(link)) {
System.out.print(" - yes!");
dist = 1;
}
}
return dist;
}
So im asking if the "fresher" (ofc at this point the graph is completed) node i is linking to any of its older buddies directly if so, distance is 1 hop.
Is it just me or the 'shortest' path will always be the first found path if nodes are traversed backwards?
How do i check if its not 1, the "else" after the base case? My math is fairly weak please be gentle :)
Any hints how to make use of the fact that the links are sorted?
It's not homework or something that im trying to cheat around from, it's not about the code itself, this has to be a useful tool, the "learning" comes by itself along the way.
here's how a graph looks nodeID, out links, in links for m=7 n=13, (note the 0 cycles is just how the graph is initialized):
0 | 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 1 1 1 1 1 1 1 2 2 2 2 2 3 4 5 6 6 7 8 9
1 | 0 0 0 0 0 0 0 | 2 2 3 4 5 5 8 12
2 | 0 0 0 0 0 1 1 | 3 3 3 3 3 4 4 4 6 7 8 10
3 | 0 1 2 2 2 2 2 | 4 4 5 5 6 6 7 11
4 | 0 1 2 2 2 3 3 | 5 5 6 8 9 10
5 | 0 1 1 3 3 4 4 | 6 7 8 9 9 11 12
6 | 0 0 2 3 3 4 5 | 7 7 7 8 9 9 12
7 | 0 2 3 5 6 6 6 | 8 9 10 11 11 12
8 | 0 1 2 4 5 6 7 | 10 10 10 11 12
9 | 0 4 5 5 6 6 7 | 10 11 11
10 | 2 4 7 8 8 8 9 | 12 12
11 | 3 5 7 7 8 9 9 |
12 | 1 5 6 7 8 10 10 |
Sorry for the agonising long read.
EDIT: Wrong code in the methods, this is what i think is correct now.
Revision of dist nr2, just try and find if theres a path at all:
private int dist(int i, int j) {
int dist = 0, c = 0, count = 0;
boolean linkExists = false;
for (int link : in_neighbours[j]) {
//System.out.print("\nIs "+j+" linked to by "+i);
if (out_neighbours[i].contains(link)) {
//System.out.print(" - yes!");
dist = 1; // there is a direct link
} else {
while ( c < j ) {
// if there's a path from 0 up to j, check if 'i' links to a node which eventually links to 'j'
if (out_neighbours[i].contains(c) &&
(out_neighbours[c].contains(link) || in_neighbours[c].contains(link) )) {
count++; // yes. and this is one node we had to step through to get closer
linkExists = true;
} else {
linkExists = false; // unreachable, the path was interrupted somewhere on the way
break;
}
c++;
}
if (linkExists) {
dist = count-1; // as 2 nodes are linked with 1 edge
} else {
dist = 0; // no path was found
}
}
}
return dist;
}
Since all edges have the same weight in your model, you can use a BFS search to find the shortest path from S to T.
This is an iterative process, starting with set #0, containing only the source node ({S}).
At each step i, you create set #i by finding all nodes achievable from set (i-1) in one step.
The iteration terminates in two cases:
1) When you detect that set #k contains T. In this case you return k-1.
2) When the set is empty, meaning that the two nodes are unreachable.
The memory consumption is about twice the number of nodes, since at each step i you are working with two sets (i-1 and i), bounded by the total number of nodes.
--EDIT--
Here is a possible implementation (I made some tests on it):
private Integer getDist(int i, int j) {
Set<Integer> currentSet = new HashSet<Integer>();
currentSet.add(i);
int dist = 0;
while (true) {
Set<Integer> nextSet = new HashSet<Integer>();
for (Integer currNode : currentSet)
nextSet.addAll(out[currNode]);
if (nextSet.isEmpty())
return null; //i.e. infinite
if (nextSet.contains(j))
return dist;
dist++;
currentSet = nextSet;
}
}
The implementation assumes that in and out are defined as List<Integer>[], and nodes are identified by numbers starting from 0. The minimal distance is counted as the number of intermediate nodes in the path, and not as the number of edges.
The duplicates you have in the lists do not break anything here, but they are irrelevant for the algorithm.