Hello CodinGame community!
I am struggling a little bit with this puzzle but feel like I am getting closer to a solution. If anyone could look at this and provide a nudge forward, it would be greatly appreciated. I am unable to print results and keep getting a “timeout” error message.
import java.util.*;
import java.io.*;
import java.math.*;
/**
* Don't let the machines win. You are humanity's last hope...
**/
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int width = in.nextInt(); // the number of cells on the X axis
int height = in.nextInt(); // the number of cells on the Y axis
if (in.hasNextLine()) {
in.nextLine();
}
String result = "";
String result1= "";
String result2 = "";
char[][] grid = new char [width][height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++){
String line = in.nextLine(); // width characters, each either 0 or .
char c = line.charAt(grid [i][j]);
if(c == '0'){
result = String.valueOf(c);
if(c == '.'){
result = "-1, -1";
continue;
}
}
for(int k = i + 1; k < height; k++){
line = in.nextLine();
char b1 = line.charAt(grid[k][j]);
if(b1 == '0'){
result1 = String.valueOf(b1);
if(b1 == '.'){
result = "-1, -1";
continue;
}
}
}
for(int m = j + 1; m < height; j++){
line = in.nextLine();
char r1 = line.charAt(grid[i][m]);
if(r1 == '0'){
result2 = String.valueOf(r1);
if(r1 == '.'){
result = "-1, -1";
continue;
}
}
}
}
System.out.println(result + "" + result1 + "" + result2);
}
}
}
Hello and thank you
I guess when you say runtime, are you talking about conversion of data types at runtime? Sorry I am still really new to a lot of this. I am going to keep tyring. If you feel like you would still like to help, I would be most grateful. This is what I changed it to and now I just get the output of “010000”.
import java.util.*;
import java.io.*;
import java.math.*;
/**
* Don't let the machines win. You are humanity's last hope...
**/
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int width = in.nextInt(); // the number of cells on the X axis
int height = in.nextInt(); // the number of cells on the Y axis
if (in.hasNextLine()) {
in.nextLine();
}
int x1, x2, x3, y1, y2, y3;
x1 = 0;
x2 = 0;
x3 = 0;
y1 = 0;
y2 = 0;
y3 = 0;
char[][] grid = new char [width][height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++){
String line = in.nextLine(); // width characters, each either 0 or .
char c = line.charAt(grid [i][j]);
if(c == '0'){
x1 = j;
y1 = i;
break;
}
if(c == '.'){
x1 = -1;
y1 = -1;
continue;
}
for(int k = 1; k < height; k++){
line = in.nextLine();
char b1 = line.charAt(grid[k][j]);
if(b1 == '0'){
x2 = k;
y2 = j;
}
if(b1 == '.'){
x2 = -1;
y2 = -1;
continue;
}
}
for(int m = j + 1; m < height; m++){
line = in.nextLine();
char r1 = line.charAt(grid[i][m]);
if(r1 == '0'){
x3 = i;
y3 = m;
}
if(r1 == '.'){
x3 = -1;
y3 = -1;
continue;
}
}
}
}
System.out.println(x1 + "" + y1 + "" + x2 + "" + y2 + "" + x3 + "" + y3);
}
}
Just add spaces between the coordinates like above, and you’re good.
Two: the input is read as in.nextLine(); not in.nextChar();, yet you have that enclosed in double loops… furthermore: Two point five: you are checking for neigbouring nodes within the input loop
meaning: you are checking whether a node has a neighbour, while you are still not finished reading the whole grid!
I suggest you build a grid first, fully read without any checking, then, and only then start solving the problem with the neighbours on a complete and unchanging grid…
Three: you have hardcoded the amount of outputs for just the first testcase. look at the following test cases, the grid gets way more complicated later on, and your code needs to be able to output a lot of coordinates!
*headscratch* there’s no easy way to “fix your code”, as there are some conceptual problems with it.
Thank you for your feedback. I will learn a little more on how 2d arrays work. I feel determined enough to figure this out. Again thanks for your time.
[SOLVED]
How should look string for an empty line?
According the second rule - “You give the neighbors for an empty cell.” I should show neighbors, but validator insist on x1 y1 x2 y2 x3 y3 format.
So I figured out what I needed to do. I needed a string [] to get the new line. After that I needed a nested loop to check initial node and two other loops to check the neighbors. It took a minute, but I got it!
I think there is a problem in horizontal test.
Here is the input from the code :
3; 1; 000; and my code provide the following results : 0 0 1 0 -1 -1
1 0 2 0 -1 -1
2 0 -1 -1 -1 -1 I don’t understand why it’s wrong…
(-1, -1) is not to the right of (0, 0). Expected (2, 0).
and i don’t understand the information in output standard because when i run my code in my IDE i don’t have this output … Moreover (2,0) is not on the first right of (0,0)
I know this is why i don’t understand the answer of the console, and I’m sure the code are the same, i tried again to paste my code and it doesn’t changed anything.
This is strange i’m gone try to debug step by step my code.
i am new to the python , when i am trying for the vertical test and it is successful.
But when i am trying to check the horizontal it is throwing error as below:
both for horizontal test / square test
invalid input. Expected ‘x1 y1 x2 y2 x3 y3’ but found ’ ’
i am trying to take the input of the lines as :
for i in range(int (height)):
Line.append(str(input(" ")))
Hi, could someone explain why is this code not indexing properly the values? Thanks in advance
import sys
import math
import array
# Don't let the machines win. You are humanity's last hope...
width = int(input()) # the number of cells on the X axis
height = int(input()) # the number of cells on the Y axis
array = [[0]*width]*height
for i in range(height):
line = input()
for j in range (len(line)):
node = line[j]
if node == "0":
array[i][j] = 1
if node == ".":
array[i][j] = 2
#if line[j] = 0;
print(array, file=sys.stderr)
Agree with @qwerty2leretour, I got a problem with the tunnel and I found my mistake height and width in initialization of the grid table. The only difference between the tunnel and the other test case is the size of the table (width != height)