There is no Spoon - Episode 1 puzzle discussion


#176

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);
        }
    }
}

#177

Look at your loops

Specifically the

for(int m = j + 1; m < height; j++)

one :wink:

What is the expected runtime of it?


#178

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);
    }
}

#179

Few issues with your code:
One: your output needs to be separated by spaces:

       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.

HTH


#180

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.


#181

[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.


#182

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!