Day 6: The Connected Cell in a Grid

Sort 50 Discussions, By:

Sorry, you do not have a permission to answer to this question.

  • Pwter 9 years ago + 3 comments

    The learning curve and pace of this contest is terrible. Day 5: const PI=Math.PI; Day 6: 2 dimensional matrix search algorithm with input handling with different whitespaces.

    Add Reply Preview cancel

    Sorry, you do not have a permission to answer to this question.

    • dotkim 9 years ago + 0 comments

      No kidding. They don't even explain what DP is.

      Add Reply Preview cancel

      Sorry, you do not have a permission to answer to this question.

      • Ayelis 9 years ago + 0 comments

        That's pretty much how I've found the learning curve on the entire HackerRank site overall. Warmup lessons? A breeze. Everything else listed as Easy? Calculate the average airspeed velocity of an unladen swallow, with nothing but these geotagged coordinates of its migration pattern and the Google Maps API to determine if it is an African or European swallow.

        Add Reply Preview cancel

        Sorry, you do not have a permission to answer to this question.

        • sasankadx 9 years ago + 0 comments

          Seriously they are not taking it seriously. No one can get some important subject like js,jq in 7 days only.

          Add Reply Preview cancel

          Sorry, you do not have a permission to answer to this question.

        • XiongX 9 years ago + 2 comments

          The input is not clean. Some lines ends with "\n", some lines end with "\r\n".

          So use input.split(/\r?\n/);

          Add Reply Preview cancel

          Sorry, you do not have a permission to answer to this question.

          • alvinlin123 9 years ago + 0 comments

            yea I got bit by that to. I use trim() to trim each row instead

            Add Reply Preview cancel

            Sorry, you do not have a permission to answer to this question.

            • iweizmeisk 9 years ago + 0 comments

              Use this : input.replace(/\n/g," ").split(" ");

              Add Reply Preview cancel

              Sorry, you do not have a permission to answer to this question.

            • Misfit 9 years ago + 3 comments

              My inelegant but rather easy way of solving this problem for those stuck.

              function fill(arr, i, j, m, n) {
                  // return if any of the indices is out of matrix bounds
                  if (i < 0 || i >= m || j < 0 || j >= n) 
                      return 0;
                  else if (arr[i][j] == 1) {
                  	// zero the element for a "visited"-like indicator,
                      // so every connected component is recursed through
                      // only once 
                      arr[i][j] = 0;
                      // return 1 as the component size, adding the recursive
                      // returns for all its possible neighbours
                      return 1+fill(arr, i-1, j-1, m, n)
                              +fill(arr, i-1, j, m, n)
                              +fill(arr, i-1, j+1, m, n)
                              +fill(arr, i, j-1, m, n)
                              +fill(arr, i, j+1, m, n)
                              +fill(arr, i+1, j-1, m, n)
                              +fill(arr, i+1, j, m, n)
                              +fill(arr, i+1, j+1, m, n);
                  }
                  return 0;
              }
              function processData(input) {
                  // read and parse the input
                  var arr = input.split(/\r?\n/);
                  var m = arr[0], n = arr[1], chck = 0, rslt = 0;
                  arr = arr.slice(2).map(e => e.split(' ').map(Number));
                  // cycle through every element of the matrix
                  for (var i = 0; i < m; i++) {
                      for (var j = 0; j < n; j++) {
                          // check arr[i][j] value and count its component
                          chck = fill(arr, i, j, m, n);
                          // compare this component size with max size yet
                          rslt = (chck > rslt) ? chck : rslt;
                      }
                  }
                  console.log(rslt);
              } 
              

              Add Reply Preview cancel

              Sorry, you do not have a permission to answer to this question.

              • leonardofa 9 years ago + 0 comments

                Pretty nice, tks

                Add Reply Preview cancel

                Sorry, you do not have a permission to answer to this question.

                • wackzingo 9 years ago + 1 comment

                  Would you consider adding soem good comments explaining what you did here? I get most of it but still unsure about a few things.

                  Add Reply Preview cancel

                  Sorry, you do not have a permission to answer to this question.

                  • Misfit 9 years ago + 0 comments

                    I've added a few comments, if any part remains unclear, feel free to ask.

                    Add Reply Preview cancel

                    Sorry, you do not have a permission to answer to this question.

                  • PRASHANTB1984 Challenge Author 9 years ago + 1 comment

                    It is pretty nice! In the return from the fill function you could perhaps generate the 8 terms via some kind of iterator, not that it implies something significantly different.

                    Add Reply Preview cancel

                    Sorry, you do not have a permission to answer to this question.

                    • emcas88 9 years ago + 0 comments

                      var dx = [-1, 0, 1,-1, 1, -1, 0, 1]; var dy = [-1,-1,-1, 0, 0, +1,+1,+1];

                      function countCells(matrix, x, y) { var c = 0;

                      if (matrix[y][x] === "1") {
                          c = 1;
                          matrix[y][x] = "x";
                      
                          for (var i = 0; i < dx.length; i++) {
                              var nx = dx[i] + x;
                              var ny = dy[i] + y;
                      
                              if (ny >= 0 && ny < matrix.length && nx >= 0 && nx < matrix[ny].length) {
                                  c += countCells(matrix, nx, ny);
                              }
                          }
                      }
                      
                      return c;
                      

                      }

                      Add Reply Preview cancel

                      Sorry, you do not have a permission to answer to this question.

                  • kenrick95 9 years ago + 2 comments

                    Take note that a cell is also connected if they are adjacent to each other diagonally.

                    Add Reply Preview cancel

                    Sorry, you do not have a permission to answer to this question.

                    • ykurmangaliyev 9 years ago + 1 comment

                      Yeah, that costed one attempt to me :(

                      Add Reply Preview cancel

                      Sorry, you do not have a permission to answer to this question.

                      • vyletel 9 years ago + 0 comments

                        I forgot about triangle pattern like this:

                        x x x 1 x 1

                        x x x x 1 x

                        It's cost me an attempt as well...

                        Add Reply Preview cancel

                        Sorry, you do not have a permission to answer to this question.

                      • PRASHANTB1984 Challenge Author 9 years ago + 0 comments

                        Yes, its the 8-connected pixel. Thanks for sharing it with others as well.

                        Add Reply Preview cancel

                        Sorry, you do not have a permission to answer to this question.

                      • sasankadx 9 years ago + 1 comment

                        Anyone solved it passing all test cases? Coz I'm failing in the last two.

                        Add Reply Preview cancel

                        Sorry, you do not have a permission to answer to this question.

                        • ivmos 9 years ago + 0 comments

                          I'm failing #4 and #5...

                          Add Reply Preview cancel

                          Sorry, you do not have a permission to answer to this question.

                        1. Challenge Walkthrough
                          Let's walk through this sample challenge and explore the features of the code editor.1 of 6
                        2. Review the problem statement
                          Each challenge has a problem statement that includes sample inputs and outputs. Some challenges include additional information to help you out.2 of 6
                        3. Choose a language
                          Select the language you wish to use to solve this challenge.3 of 6
                        4. Enter your code
                          Code your solution in our custom editor or code in your own environment and upload your solution as a file.4 of 6
                        5. Test your code
                          You can compile your code and test it for errors and accuracy before submitting.5 of 6
                        6. Submit to see results
                          When you're ready, submit your solution! Remember, you can go back and refine your code anytime.6 of 6
                        1. Check your score