There is no Spoon - Episode 1 puzzle discussion

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

Look at your loops

Specifically the

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

one :wink:

What is the expected runtime of it?

2 Likes

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

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

1 Like

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.

1 Like

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

1 Like

Every test case fails even though on my IDE everything works nicely. The console only tells me:

Standard Output Stream:

00

Game information:

invalid input. Expected ‘x1 y1 x2 y2 x3 y3’ but found ‘00’

A it is said, you need to :

Output for one game turn
One line per node. Six integers on each line: x1 y1 x2 y2 x3 y3
Where:

  • (x1,y1) the coordinates of a node
  • (x2,y2) the coordinates of the closest neighbor on the right of the node
  • (x3,y3) the coordinates of the closest bottom neighbor
    If there is no neighbor, the coordinates should be -1 -1.

The game is expeting 6 integer and you only provide 1 : ‘00’

What do you mean by “on my IDE everything works nicely” ?

Hi,

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…

What does the console say ? Maybe it’s a formatting problem ? An “\n” missing at the end ? A space that shouldn’t be output ?

the console say :
Sortie standard :

0 0 -1 -1 -1 -1

Informations :

(-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)

None of the result you provided mention that output

0 0 -1 -1 -1 -1

So there is a difference between what you think you output and what is really outputted…

Did you cheked that the code in CodinGame is the same as the one in your IDE ?

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.

here is my code if you also want to try:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	string x1, x2, x3, y1, y2, y3;
	int width;
	int height;
	string* tab;
	vector<pair<int, int>> coordonnees;
	vector<string> output;

	cin >> width; cin.ignore();
	cin >> height; cin.ignore();
	tab = new string[height];

	for (int i = 0; i < height; i++)
	{
		string line;
		getline(cin, line);
		tab[i] = line;
	}

	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
		{
			if (tab[i][j] == '0')
			{
				coordonnees.push_back({ j,i });
			}
		}
	}
	delete[] tab;

	for (int k = 0; k < coordonnees.size(); k++)
	{
		vector<pair<int, int>> test;
		test = coordonnees;
		string point;
		string right("-1 -1 ");
		string under("-1 -1 ");
		for (int n = 0; n < test.size(); n++)
		{
			if (test[n].first == coordonnees[k].first && test[n].second == coordonnees[k].second)
			{
				point = to_string(test[n].first) + " " + to_string(test[n].second) + " ";
			}
			else if (test[n].first == (coordonnees[k].first)+1 && test[n].second == coordonnees[k].second)
			{
				right = to_string(test[n].first) + " " + to_string(test[n].second) + " ";
			}
			else if (test[n].first == coordonnees[k].first && test[n].second == (coordonnees[k].second) + 1)
			{
				under = to_string(test[n].first) + " " + to_string(test[n].second) + " ";
			}
		}
		output.push_back(point + right + under);
	}

	for (int g = 0; g < output.size(); g++)
	{
		cout << output[g] << endl;
	}
	
	system("PAUSE");
	return 0;
	
}

Hi

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(" ")))

can anybody please tell me where i did wrong

I am also facing same problem, as u have faced, please tell me what’s wrong is going on.

I think we have to read node not ‘.’ , so if ‘.’ comes move forward in a row or column until u r not able to find any node…am i right?

I think I didn’t use a 2D loop for my answer, but it’s not exactly what I’d call “optimized”.

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) 

Resulting array = [[1, 2], [1, 2]]

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)