There is no Spoon - Episode 1 puzzle discussion

nope, the next cell on the right can be empty.

For some reason I pass all of the tests but the Vertical and Horizontal, which give me an out of bounds error at line 11. Any ideas why?

1. static void Main(string[] args)
2.     {
3.         int width = int.Parse(Console.ReadLine()); // the number of cells on the X axis
4.         int height = int.Parse(Console.ReadLine()); // the number of cells on the Y axis
5.         char [,] a = new char [width, height];
6.         for (int i = 0; i < height; ++i)
7.         {
8.             string line = Console.ReadLine(); // width characters, each either 0 or .
9.             for(int j = 0; j < width; ++j)
10.             {
11.                 a[i,j] = line[j];
12.             }
13.         }
14.         
15.         for (int i = 0; i < height; i++)
16.         {
17.             for(int j = 0; j < width; j++)
18.             {
19.                 
20.                 if(a[i,j] == '0')
21.                 {
22.                    string endLine = findNodes(a, i, j, height, width);
23.                    Console.WriteLine(j + " " + i + " " + endLine);
24.                 }
25.                 
26.             }
27.         }

28.         // Write an action using Console.WriteLine()
29.         // To debug: Console.Error.WriteLine("Debug messages...");


30.         // Three coordinates: a node, its right neighbor, its bottom neighbor
31.         
32.     }`Preformatted text`

what if height > width and i = height -1 ?

I don’t think I follow. Say height = 4, width = 1. at i =height -1 we have i =3, so a[3,0] = line[0] that should be in bounds.

a[3,0] is out of bounds of your array of char [1, 4]

Oh I see, my declaration is backwards.

Hello all,
I had the same! I have found my mistake!
My code had took the gril for a square but it is n’t.
I put “width” to “height” in a loop.

I can’t figure out what’s the bug is in my code when it runs test case ‘horizontal’ or the test cases including nodes in horizontal direction. Though i’ve cleared the test cases ‘Example’ and ‘vertical’.


// our grid of size height
vector grid(height);

for ( int i = 0; i < height; ++i)
{
	getline( cin, grid[i] );	// store our grid
}

// Now here goes the main program
for ( int i = 0; i < height; ++i ) {
	for ( int j = 0; j < width; ++j ) {
		
					
			if ( grid[i][j] == '0' ) // found a node
				cout << j << ' ' << i << ' ';
			else if ( grid[i][j] == '.' ) // not a node
				cout << -1 << ' ' << -1 << ' ';
			
			// check node's allias
			// right node:
			if ( j != (width -1) ) {
				if ( grid[i][j+1] == '0' ) // found a node
					cout << j+1 << ' ' << i << ' ';
				else if ( grid[i][j+1] == '.' ) // not a node
					cout << -1 << ' ' << -1 << ' ';
			}else{
			    cout << -1 << ' ' << -1 << ' ' ;
			}
			// bottom node:
			if ( i != (height -1)) {
				if ( grid[i+1][j] == '0' ) // found a node
					cout << j << ' ' << i+1 << '\n';
				else if ( grid[i+1][j] == '.' ) // not a node
					cout << -1 << ' ' << -1 << '\n';
			}else{
			    cout << -1 << ' ' << -1 << '\n';
			}
	
	}
}

You need to check for any closest node to the right and to the bottom, not just the immediate right and bottom.

aren’t those just the same things?

No, e.g. the closest node to the right may not be the one immediately right, there may be empty cells in between.

oh! i get it. Thanks for your help.

I have a question about the challenge : There is no Spoon - Episode 1.
When a node is empty, represented by a dote (.). How do i write the required response. does it an empty string or something else.

If a cell is empty, it means there is no node on this cell.

If a neighbor does not exist, you must output the coordinates -1 -1 instead of (x2,y2) and/or (x3,y3).

So i return an emty string for an emty node or do i return the coordinates nevertheless ??
Thx

In There is no Spoon - Episode 1 coding with php.

The first test grid / Map
00
0.

I can generate those results using error_log().

Sortie d’erreur :

‘0 0 1 0 0 1’
‘1 0 -1 -1 -1 -1’
‘0 1 -1 -1 -1 -1’
‘1 1 -1 -1 -1 -1’

But echo() return an empty result

Sortie standard :

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

Thank for helping

Any help ??

How do you store the numbers? and how do you use echo()?

I don’t understand how the grid system works in this problem
basically, a 2D array would follow s
(0,0) (0,1)
(1,0) (1,1)

can you help me please, i really don’t see when i failed.

 int width; // le nombre de cellule sur l'axe des X
    scanf("%d", &width); // afficher le nbr de cellule sur l'axe des X
    int height; // le nombre de cellule sur l'axe des Y
    scanf("%d", &height); fgetc(stdin); // afficher le nbr de cellule sur l'axe des Y
    char tab[width][height];
    int x1=0, y1=0, x2=0,y2=0,x3=0,y3=0;
    for (int i = 0; i < height; i++) {
        char line[32]; // width characters, each either 0 or .
        fgets(line, 32, stdin); // width characters, each either 0 or .
        
        for(int j=0; j<width; j++)
        {
         tab[i][j] = line[j];
        }
    }
    
    for (int i = 0; i < height; i++) {
      for(int j=0; j<width; j++)
        {
             x1=i;
             y1=j;
             x2=-1;
             y2=-1;
             x3=-1;
             y3=-1;
        if(tab[i][j]="0")
        {
            x1=i;
            y1=j;
            i+=1;
            
            if(tab[i][j]="0")
            {
                x2=i;
                y2=j;
                 
                i-=1;
                j+=1;
            if(tab[i][j]="0")
            {
             x3=i;
             y3=j;
             i+=1;
            } else
           {
             x3=-1;
             y3=-1;
             i+=1;
            }
            
            }    else 
            {
                 
                x2=-1;
                y2=-1;
                i-=1;
                j+=1;
        
             }
            
        
        }else
        {
            x1=-1;
            y1=-1;
            i+=1;
        }
    
        
        }
    }   
     
    // Three coordinates: a node, its right neighbor, its bottom neighbor
      printf("%d %d %d %d %d %d\n",x1,y1,x2,y2,x3,y3);