📜  打印可以从网格中转义的网格每一行的索引

📅  最后修改于: 2021-09-04 09:41:26             🧑  作者: Mango

给定一个维度为M * N的二进制 2D 数组arr[]表示网格,其中“ 0 ”表示单元格的主对角线上有墙,“ 1 ”表示单元格的交叉对角线上有墙。任务是对于每i行打印可以从网格中逃脱的行的索引,因为人们无法从网格的顶部或底部逃脱。

例子:

方法:根据以下观察可以解决给定的问题:

  • 从上图可以看出,对于给定的墙壁方向,取决于人进入细胞的方向,只有一种选择可以离开该细胞。
  • 因此,对于每一行,想法是在给定的方向上迭代,跟踪一个人进入单元格的方向。

请按照以下步骤解决问题:

  • 使用变量迭代范围[0, M – 1]并执行以下操作:
    • 初始化两个变量ij来存储人当前所在单元格的行索引和列索引。
    • row分配给i ,将0分配给j
    • 初始化一个变量,比如dir,以存储一个人进入单元格的方向。
    • 迭代直到j不至少为N并执行以下操作:
      • 如果arr[i][j]1,则检查以下条件:
        • 如果dir等于“ L ”,则将i1并将“ D ”分配给dir
        • 否则,如果dir等于 ‘ U ‘,则将j1并将 ‘ R ‘ 分配给dir
        • 否则,如果dir等于 ‘ R ‘ 则将i增加1并将 ‘ U ‘ 分配给dir
        • 否则,将j增加1并将“ L ”分配给dir
      • 否则,如果arr[i][j]0,则检查以下条件:
        • 如果dir等于“ L ”,则将i增加1并将“ U ”分配给dir
        • 否则,如果dir等于 ‘ U ‘,则将j增加1并将 ‘ L ‘ 分配给dir
        • 否则,如果dir等于 ‘ R ‘ 则将i1并将 ‘ D ‘ 分配给dir
        • 否则,将j1并将“ R ”分配给dir
      • 否则,如果ij0i等于Mj等于N,则中断。
    • 如果j等于N则打印值i
    • 否则,打印-1

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the row index for
// every row of a matrix from which one
// can exit the grid after entering from left
void findPath(vector >& arr,
              int M, int N)
{
    // Iterate over the range [0, M-1]
    for (int row = 0; row < M; row++) {
 
        // Stores the direction from
        // which a person enters a cell
        char dir = 'L';
 
        // Row index from which
        // one enters the grid
        int i = row;
 
        // Column index from which
        // one enters the grid
        int j = 0;
 
        // Iterate until j is atleast N-1
        while (j < N) {
 
            // If Mat[i][j] is equal to 1
            if (arr[i][j] == 1) {
 
                // If entry is from left cell
                if (dir == 'L') {
 
                    // Decrement i by 1
                    i--;
 
                    // Assign 'D' to dir
                    dir = 'D';
                }
 
                // If entry is from upper cell
                else if (dir == 'U') {
                    // Decrement j by 1
                    j--;
                    // Assign 'R' to dir
                    dir = 'R';
                }
                // If entry is from right cell
                else if (dir == 'R') {
 
                    // Increment i by 1
                    i++;
 
                    // Assign 'U' to dir
                    dir = 'U';
                }
 
                // If entry is from bottom cell
                else if (dir == 'D') {
 
                    // Increment j by 1
                    j++;
 
                    // Assign 'L' to dir
                    dir = 'L';
                }
            }
 
            // Otherwise,
            else {
                // If entry is from left cell
                if (dir == 'L') {
 
                    // Increment i by 1
                    i++;
 
                    // Assign 'U' to dir
                    dir = 'U';
                }
 
                // If entry is from upper cell
                else if (dir == 'U') {
 
                    // Increment j by 1
                    j++;
 
                    // Assign 'L' to dir
                    dir = 'L';
                }
 
                // If entry is from right cell
                else if (dir == 'R') {
 
                    // Decrement i by 1
                    i--;
 
                    // Assign 'D' to dir
                    dir = 'D';
                }
                // If entry is from lower cell
                else if (dir == 'D') {
 
                    // Decrement j by 1
                    j--;
 
                    // Assign 'R' to dir
                    dir = 'R';
                }
            }
 
            // If i or j is less than 0 or i is
            // equal to M or j is equal to N
            if (i < 0 || i == M || j < 0 || j == N)
                break;
        }
 
        // If j is equal to N
        if (j == N)
            cout << i << " ";
 
        // Otherwise
        else
            cout << -1 << " ";
    }
}
// Driver Code
int main()
{
    // Input
    vector > arr = { { 1, 1, 0, 1 },
                                 { 1, 1, 0, 0 },
                                 { 1, 0, 0, 0 },
                                 { 1, 1, 0, 1 },
                                 { 0, 1, 0, 1 } };
    int M = arr.size();
    int N = arr[0].size();
 
    // Function call
    findPath(arr, M, N);
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the row index for
// every row of a matrix from which one
// can exit the grid after entering from left
static void findPath(int [][]arr,
                     int M, int N)
{
     
    // Iterate over the range [0, M-1]
    for(int row = 0; row < M; row++)
    {
         
        // Stores the direction from
        // which a person enters a cell
        char dir = 'L';
 
        // Row index from which
        // one enters the grid
        int i = row;
 
        // Column index from which
        // one enters the grid
        int j = 0;
 
        // Iterate until j is atleast N-1
        while (j < N)
        {
             
            // If Mat[i][j] is equal to 1
            if (arr[i][j] == 1)
            {
 
                // If entry is from left cell
                if (dir == 'L')
                {
                     
                    // Decrement i by 1
                    i--;
 
                    // Assign 'D' to dir
                    dir = 'D';
                }
 
                // If entry is from upper cell
                else if (dir == 'U')
                {
                     
                    // Decrement j by 1
                    j--;
                     
                    // Assign 'R' to dir
                    dir = 'R';
                }
                 
                // If entry is from right cell
                else if (dir == 'R')
                {
                     
                    // Increment i by 1
                    i++;
 
                    // Assign 'U' to dir
                    dir = 'U';
                }
 
                // If entry is from bottom cell
                else if (dir == 'D')
                {
                     
                    // Increment j by 1
                    j++;
 
                    // Assign 'L' to dir
                    dir = 'L';
                }
            }
 
            // Otherwise,
            else
            {
                 
                // If entry is from left cell
                if (dir == 'L')
                {
                     
                    // Increment i by 1
                    i++;
 
                    // Assign 'U' to dir
                    dir = 'U';
                }
 
                // If entry is from upper cell
                else if (dir == 'U')
                {
                     
                    // Increment j by 1
                    j++;
 
                    // Assign 'L' to dir
                    dir = 'L';
                }
 
                // If entry is from right cell
                else if (dir == 'R')
                {
                     
                    // Decrement i by 1
                    i--;
 
                    // Assign 'D' to dir
                    dir = 'D';
                }
                 
                // If entry is from lower cell
                else if (dir == 'D')
                {
                     
                    // Decrement j by 1
                    j--;
 
                    // Assign 'R' to dir
                    dir = 'R';
                }
            }
 
            // If i or j is less than 0 or i is
            // equal to M or j is equal to N
            if (i < 0 || i == M || j < 0 || j == N)
                break;
        }
 
        // If j is equal to N
        if (j == N)
            System.out.print(i + " ");
 
        // Otherwise
        else
            System.out.print(-1 + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Input
    int [][]arr = { { 1, 1, 0, 1 },
                    { 1, 1, 0, 0 },
                    { 1, 0, 0, 0 },
                    { 1, 1, 0, 1 },
                    { 0, 1, 0, 1 } };
    int M = arr.length;
    int N = arr[0].length;
 
    // Function call
    findPath(arr, M, N);
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program
# for the above approach
 
# Function to find the row index for
# every row of a matrix from which one
# can exit the grid after entering from left
def findPath(arr, M,  N) :
     
    # Iterate over the range [0, M-1]
    for row in range(M):
  
        # Stores the direction from
        # which a person enters a cell
        dir = 'L'
  
        # Row index from which
        # one enters the grid
        i = row
  
        # Column index from which
        # one enters the grid
        j = 0
  
        # Iterate until j is atleast N-1
        while (j < N) :
  
            # If Mat[i][j] is equal to 1
            if (arr[i][j] == 1) :
  
                # If entry is from left cell
                if (dir == 'L') :
  
                    # Decrement i by 1
                    i -= 1
  
                    # Assign 'D' to dir
                    dir = 'D'
                 
  
                # If entry is from upper cell
                elif (dir == 'U') :
                    # Decrement j by 1
                    j -= 1
                    # Assign 'R' to dir
                    dir = 'R'
                 
                # If entry is from right cell
                elif (dir == 'R') :
  
                    # Increment i by 1
                    i += 1
  
                    # Assign 'U' to dir
                    dir = 'U'
                 
  
                # If entry is from bottom cell
                elif (dir == 'D') :
  
                    # Increment j by 1
                    j += 1
  
                    # Assign 'L' to dir
                    dir = 'L'
                 
             
  
            # Otherwise,
            else :
                # If entry is from left cell
                if (dir == 'L') :
  
                    # Increment i by 1
                    i += 1
  
                    # Assign 'U' to dir
                    dir = 'U'
                 
  
                # If entry is from upper cell
                elif (dir == 'U') :
  
                    # Increment j by 1
                    j += 1
  
                    # Assign 'L' to dir
                    dir = 'L'
                 
  
                # If entry is from right cell
                elif (dir == 'R') :
  
                    # Decrement i by 1
                    i -= 1
  
                    # Assign 'D' to dir
                    dir = 'D'
                 
                # If entry is from lower cell
                elif (dir == 'D') :
  
                    # Decrement j by 1
                    j -= 1
  
                    # Assign 'R' to dir
                    dir = 'R'
                 
             
  
            # If i or j is less than 0 or i is
            # equal to M or j is equal to N
            if (i < 0 or i == M or j < 0 or j == N):
                break
         
  
        # If j is equal to N
        if (j == N) :
            print(i, end = " ")
  
        # Otherwise
        else :
            print(-1, end = " ")
     
 
# Driver Code
 
arr = [[ 1, 1, 0, 1 ],
    [ 1, 1, 0, 0 ],
    [ 1, 0, 0, 0 ],
    [ 1, 1, 0, 1 ],
    [ 0, 1, 0, 1 ]]
 
M = len(arr)
N = len(arr[0])
  
# Function call
findPath(arr, M, N)
 
# This code is contributed by susmitakundugoaldanga.


C#
// C# program for the above approach
 
using System;
 
class GFG {
    // Function to find the row index for
    // every row of a matrix from which one
    // can exit the grid after entering from left
    static void findPath(int[, ] arr, int M, int N)
    {
        // Iterate over the range [0, M-1]
        for (int row = 0; row < M; row++) {
 
            // Stores the direction from
            // which a person enters a cell
            char dir = 'L';
 
            // Row index from which
            // one enters the grid
            int i = row;
 
            // Column index from which
            // one enters the grid
            int j = 0;
 
            // Iterate until j is atleast N-1
            while (j < N) {
 
                // If Mat[i][j] is equal to 1
                if (arr[i, j] == 1) {
 
                    // If entry is from left cell
                    if (dir == 'L') {
 
                        // Decrement i by 1
                        i--;
 
                        // Assign 'D' to dir
                        dir = 'D';
                    }
 
                    // If entry is from upper cell
                    else if (dir == 'U') {
                        // Decrement j by 1
                        j--;
                        // Assign 'R' to dir
                        dir = 'R';
                    }
                    // If entry is from right cell
                    else if (dir == 'R') {
 
                        // Increment i by 1
                        i++;
 
                        // Assign 'U' to dir
                        dir = 'U';
                    }
 
                    // If entry is from bottom cell
                    else if (dir == 'D') {
 
                        // Increment j by 1
                        j++;
 
                        // Assign 'L' to dir
                        dir = 'L';
                    }
                }
 
                // Otherwise,
                else {
                    // If entry is from left cell
                    if (dir == 'L') {
 
                        // Increment i by 1
                        i++;
 
                        // Assign 'U' to dir
                        dir = 'U';
                    }
 
                    // If entry is from upper cell
                    else if (dir == 'U') {
 
                        // Increment j by 1
                        j++;
 
                        // Assign 'L' to dir
                        dir = 'L';
                    }
 
                    // If entry is from right cell
                    else if (dir == 'R') {
 
                        // Decrement i by 1
                        i--;
 
                        // Assign 'D' to dir
                        dir = 'D';
                    }
                    // If entry is from lower cell
                    else if (dir == 'D') {
 
                        // Decrement j by 1
                        j--;
 
                        // Assign 'R' to dir
                        dir = 'R';
                    }
                }
 
                // If i or j is less than 0 or i is
                // equal to M or j is equal to N
                if (i < 0 || i == M || j < 0 || j == N)
                    break;
            }
 
            // If j is equal to N
            if (j == N)
                Console.Write(i + " ");
 
            // Otherwise
            else
                Console.Write(-1 + " ");
        }
    }
   
    // Driver Code
    public static void Main()
    {
       
        // Input
        int[, ] arr = { { 1, 1, 0, 1 },
                        { 1, 1, 0, 0 },
                        { 1, 0, 0, 0 },
                        { 1, 1, 0, 1 },
                        { 0, 1, 0, 1 } };
        int M = arr.GetLength(0);
        int N = arr.GetLength(1);
 
        // Function call
        findPath(arr, M, N);
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
-1 -1 2 3 -1

时间复杂度: O(M * N)。
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live