Project: Codingame.com – There is no spoon

Tools and Technology: C++

There’s a great site called Codingame that gives practice code challenges, including weekly and sponsored challenges, as well as coder-vs-coder and 5-minute challenges. This site helps keep me coding every day. I’ve completed all of the Easy puzzles and am now pushing through the Medium ones. This is from the There Is No Spoon challenge.

Sample Code

#include 
#include 
using namespace std;

int main()
{
    int width; // the number of cells on the X axis
    cin >> width; cin.ignore();
    int height; // the number of cells on the Y axis
    cin >> height; cin.ignore();
    string *grid = new string[height]; // grid of characters, each either 0 or .
    for (int i = 0; i < height; i++) {
        getline(cin, grid[i]); // line of characters
    }
    

    for (int vert = 0; vert < height; vert++)
    {
     for (int horz = 0; horz < width; horz++)
     {
         if('0' == grid[vert][horz]) // If we found a 
         {
              // Print the base node coords
              cout << horz << " " << vert << " ";
              
              // Print possible right neighbor coords
              int i = 0;
              int tempx = -1;
              int tempy = -1;
              while(width != horz + ++i)
              {
                  if('0' == grid[vert][horz + i])
                  {
                      tempx = horz + i;
                      tempy = vert;
                      break;
                  }
              }
              cout << tempx << " " << tempy << " ";
              
              // Print possible bottom neighbor coords
              i = 0;
              tempx = tempy = -1;
              while(height != vert + ++i)
              {
                  if('0' == grid[vert + i][horz])
                  {
                      tempx = horz;
                      tempy = vert + i;
                      break;
                  }
              }
              cout << tempx << " " << tempy << " " << endl;
         }
     }  
    }
    
    delete[] grid;
}