Question

In: Computer Science

Find all rectangles filled with 0 We have one 2D array, filled with zeros and ones....

Find all rectangles filled with 0

We have one 2D array, filled with zeros and ones. We have to find the starting point and ending point of all rectangles filled with 0. It is given that rectangles are separated and do not touch each other however they can touch the boundary of the array.A rectangle might contain only one element. In Javascript preferred.

Examples:

input = [
            [1, 1, 1, 1, 1, 1, 1],
            [1, 1, 1, 1, 1, 1, 1],
            [1, 1, 1, 0, 0, 0, 1],
            [1, 0, 1, 0, 0, 0, 1],
            [1, 0, 1, 1, 1, 1, 1],
            [1, 0, 1, 0, 0, 0, 0],
            [1, 1, 1, 0, 0, 0, 1],
            [1, 1, 1, 1, 1, 1, 1]
        ]


Output:
[
  [2, 3, 3, 5], [3, 1, 5, 1], [5, 3, 6, 5]
]

Explanation:
We have three rectangles here, starting from 
(2, 3), (3, 1), (5, 3)

Input = [
            [1, 0, 1, 1, 1, 1, 1],
            [1, 1, 0, 1, 1, 1, 1],
            [1, 1, 1, 0, 0, 0, 1],
            [1, 0, 1, 0, 0, 0, 1],
            [1, 0, 1, 1, 1, 1, 1],
            [1, 1, 1, 0, 0, 0, 0],
            [1, 1, 1, 1, 1, 1, 1],
            [1, 1, 0, 1, 1, 1, 0]
        ]


Output:
[
  [0, 1, 0, 1], [1, 2, 1, 2], [2, 3, 3, 5], 
  [3, 1, 4, 1], [5, 3, 5, 6], [7, 2, 7, 2], 
  [7, 6, 7, 6]
]

Solutions

Expert Solution

<script language="JavaScript">

var nl = getNewLine()

function getNewLine() {
   var agent = navigator.userAgent

   if (agent.indexOf("Win") >= 0)
       return "\r\n"
   else
       if (agent.indexOf("Mac") >= 0)
           return "\r"

    return "\r"

}

pagecode = '<scr'+'ipt language="JavaScript">

var nl = getNewLine()

function getNewLine() {
   var agent = navigator.userAgent

   if (agent.indexOf("Win") >= 0)
       return "\\r\\n"
   else
       if (agent.indexOf("Mac") >= 0)
           return "\\r"

    return "\\r"

}

pagecode = \'# Python program to find all
# rectangles filled with 0

function findend(i,j,a,output,index):
   x = len(a)
   y = len(a[0])

   # flag to check column edge case,
   # initializing with 0
   flagc = 0

   # flag to check row edge case,
   # initializing with 0
   flagr = 0

   for m in range(i,x):

       # loop breaks where first 1 encounters
       if a[m][j] == 1:
           flagr = 1 # set the flag
           break

       # pass because already processed
       if a[m][j] == 5:
           pass

       for n in range(j, y):

           # loop breaks where first 1 encounters
           if a[m][n] == 1:
               flagc = 1 # set the flag
               break

           # fill rectangle elements with any
           # number so that we can exclude
           # next time
           a[m][n] = 5

   if flagr == 1:
       output[index].append( m-1)
   else:
       # when end point touch the boundary
       output[index].append(m)

   if flagc == 1:
       output[index].append(n-1)
   else:
       # when end point touch the boundary
       output[index].append(n)


def get_rectangle_coordinates(a):

   # retrieving the column size of array
   size_of_array = len(a)

   # output array where we are going
   # to store our output
   output = []

   # It will be used for storing start
   # and end location in the same index
   index = -1

   for i in range(0,size_of_array):
       for j in range(0, len(a[0])):
           if a[i][j] == 0:

               # storing initial position
               # of rectangle
               output.append([i, j])

               # will be used for the
               # last position
               index = index + 1      
               findend(i, j, a, output, index)


   print (output)

# driver code
tests = [

           [1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 1, 1, 1, 1],
           [1, 1, 1, 0, 0, 0, 1],
           [1, 0, 1, 0, 0, 0, 1],
           [1, 0, 1, 1, 1, 1, 1],
           [1, 0, 1, 0, 0, 0, 0],
           [1, 1, 1, 0, 0, 0, 1],
           [1, 1, 1, 1, 1, 1, 1]

       ]


get_rectangle_coordinates(tests)
\'

document.write(pagecode);

</scr'+'ipt>'

document.write(pagecode);

</script>


Related Solutions

1.Find all rational zeros of the polynomial, and then find the irrational zeros, if any. Whenever...
1.Find all rational zeros of the polynomial, and then find the irrational zeros, if any. Whenever appropriate, use the Rational Zeros Theorem, the Upper and Lower Bounds Theorem, Descartes' Rule of Signs, the Quadratic Formula, or other factoring techniques. (Enter your answers as comma-separated lists. Enter all answers including repetitions. If an answer does not exist, enter DNE.) P(x) = 12x4 − 11x3 − 18x2 + 5x rational zeros     x = 2. Find all rational zeros of the polynomial. (Enter...
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[...
Given a 2D array a, sum up ALL the edges of the array. Ex. int a[ ][ ] = { {1, 2, 3, 4},                        {5, 6, 7, 8},                        {9, 10, 11, 12} }; OUTPUT: Sum of the edges = 65
8. All the real zeros of the given polynomial are integers. Find the zeros. (Enter your...
8. All the real zeros of the given polynomial are integers. Find the zeros. (Enter your answers as a comma-separated list. Enter all answers including repetitions.) P(x) = x3 + 6x2 − 32 x = Write the polynomial in factored form. P(x) = 9. Find all rational zeros of the polynomial. (Enter your answers as a comma-separated list. Enter all answers including repetitions.) P(x) = 4x4 − 45x2 + 81 x = Write the polynomial in factored form. P(x) =...
We have an array of numbers, and we start at index 0. At every point, we're...
We have an array of numbers, and we start at index 0. At every point, we're allowed to jump from index i to i+3, i+4, or stop where we are. We'd like to find the maximum possible sum of the numbers we visit. For example, for the array [14, 28, 79, -87, 29, 34, -7, 65, -11, 91, 32, 27, -5], the answer is 140. (starting at the 14, we jump 4 spots to 29, then 3 spots to 65,...
use the Rational Zeros Theorem to find all the real zeros of each polynomial function given...
use the Rational Zeros Theorem to find all the real zeros of each polynomial function given below: f(X)=3X^3 + 6X^2 -15X - 30 f(X) = 2X^4 - X^3 - 5X^2 + 2X + 2
You are given an array of length n that is filled with two symbols (say 0...
You are given an array of length n that is filled with two symbols (say 0 and 1); all m copies of one symbol appear first, at the beginning of the array, followed by all n − m copies of the other symbol. You are to find the index of the first copy of the second symbol in time O(logm). include: code,  proof of correctness, and runtime analysis  
Find the median (middle value) of an array of numbers, which we will assume are all...
Find the median (middle value) of an array of numbers, which we will assume are all distinct. For example, if there are 27 elements, then you must return the 14th smallest (which is also the 14th largest). The strategy used to solve this problem is somewhat like the quicksort algorithm, but has some important differences. Consider choosing a pivot element and partitioning the input so that the elements less than the pivot are to its left, and those larger than...
In all cases we have an array of int or char of some fixed size. The...
In all cases we have an array of int or char of some fixed size. The program will prompt the user to enter some values, such as: Enter 7 integers to be stored in the array: 5 13 8 5 1 2    The questions that could then be asked of this data might be: Count how many numbers are < the last element Count how many numbers are odd Similarly we might prompt for character input: Enter 7 characters...
USE C PROGRAMMING, call all functions in main, and use a 2 by 3 2d array...
USE C PROGRAMMING, call all functions in main, and use a 2 by 3 2d array to test, thanks. get_location_of_min This function takes a matrix as a 2-D array of integers with NUM_COLS width, the number of rows in the matrix and two integer pointers. The function finds the location of the minimum value in the matrix and stores the row and column of that value to the memory location pointed to by the pointer arguments. If the minimum value...
For the given polynomial, find all zeros of the polynomial algebraically. Factor the polynomial completely. ?(?)...
For the given polynomial, find all zeros of the polynomial algebraically. Factor the polynomial completely. ?(?) = ?^4 − 2?^3 − 2?^2 − 2? − 3 For the given polynomial, find all zeros of the polynomial algebraically. Factor the polynomial completely. ?(?) = 6?^4 − 7?^3 − 12?^2 + 3? + 2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT