#Help with CS problem 6

5 messages · Page 1 of 1 (latest)

long moth
#

Nazi is back and will be eliminating some Jews. Both Nazi and the Jews lived in an n × n grid. Initially Nazi is located at the cell (r, c) where r is the row number and c is the column number both starting from 1, with r=1 is the bottom row. Each cell (i, j) denoted as grid[i][j] can be either 0 or 1, where 0 indicates the absence of Jews and 1 means presence. Nazi can move to any of the adjacent cells from it's current cell each time, as long as it does not goes out of the border. Find the number of ways Nazi can eliminate all the Jews.

Constraints:
1<=n<=10
1<=r,c<=n
grid[i][j]=0 or grid[i][j]=1, 1<=i,j<=n

Example:

Input:
n=2
r=1, c=1
grid={{0, 1},
{0, 0}}

Output:
3

Explanation:
Nazi can eliminate the only Jew located at (2, 2) in the following 3 ways:

  1. Move up 1 unit, then move right 1 unit
    ((1,1)->(2,1), (2,1)->(2,2))
  2. Move right 1 unit, then move up 1 unit
    ((1,1)->(1,2), (1,2)->(2,2))
  3. Move diagonally up-right 1 unit
    ((1,1)->(2,2))
median ironBOT
#
  1. Wait patiently for a helper to come along.
  2. Once someone helps you, say thank you and close the thread with:
+close
  1. Feel free to nominate the person for helper of the week in #helper-nominations
  2. Do not ping the mods, unless someone is breaking the rules.
  3. If you're happy with the help you got here, and the server overall, you can contribute financially as well:
summer quiver