二进制矩阵中的退出点
给定一个大小为N x M的二进制矩阵,您在单元格 (0, 0) 处从左到右输入矩阵。每当遇到相同方向的 0 时,如果遇到 1 改变方向到当前方向的右侧并将该 1 值更改为 0,则从 Matrix 中找出退出点。
例子:
Input: matrix = {{0, 1, 0},
{0, 1, 1},
{0, 0, 0}}
Output: 1 0
Explanation:
Enter the matrix at 0, 0 -> then move towards 0, 1 -> 1 is encountered -> turn right towards 1, 1 -> again 1 is encountered -> turn right again towards 1, 0 -> now, the boundary of matrix will be crossed ->hence, exit point reached at 1, 0.
Input: matrix = {{0, 0}}
Output: 0 1
方法:由于矩阵是在 0, 0 位置输入的,解决这个问题的方法是基于以下观察
- 最初,在 0、0 处输入一个矩阵并向右移动。
- 一旦遇到 1,方向就会顺时针旋转 90 度,即右 -> 下 -> 左 -> 上。
- 以上述方式继续遍历矩阵,直到到达边界。
- 一旦到达边界并且没有遇到转弯,就会越过边界,出口点将是最后经过的单元格。
插图:
Consider the matrix:
{{0, 1, 0},
{0, 1, 1},
{0, 0, 0}}
- Traverse the matrix using row as i and column as j.
- Initially the matrix is entered at 0, 0 and moved towards right ( (i, j) -> (i, j++) ) till 1 is encountered.
- 1 is encountered at 1, 0. So, direction will be changed 90 degrees clockwise towards down ( (i, j) -> (i++, j) ).
- Keep moving down till 1 is encountered at 1, 1
- Again direction will be changed 90 degrees towards left ( (i, j) -> (i, j–) ).
- Keep moving left.
- No 1 is encountered now but the boundary of the matrix is crossed at 1, 0 and hence, 1, 0 is the required exit point.
以下是上述方法的实现:
C++
// C++ program to find the exit point in a matrix
#include
using namespace std;
// Function to find the exit
// point in a given matrix
vector FindExitPoint(
vector >& matrix)
{
// initialization of row, column
int i = 0, j = 0;
int dir = 0;
while (true) {
dir = (dir + matrix[i][j]) % 4;
// If a cell is traversed
// then mark it has 0
if (matrix[i][j] == 1) {
matrix[i][j] = 0;
}
// Right direction
if (dir == 0) {
j++;
}
// Down direction
else if (dir == 1) {
i++;
}
// Left direction
else if (dir == 2) {
j--;
}
// Up direction
else if (dir == 3) {
i--;
}
// decrement either the row or col
// since it crossed the boundary
if (i < 0) {
i++;
break;
}
else if (i == matrix.size()) {
i--;
break;
}
else if (j < 0) {
j++;
break;
}
else if (j == matrix[0].size()) {
j--;
break;
}
}
// return row and column
vector v{ i, j };
return v;
}
// Driver Code
int main()
{
vector > matrix{ { 0, 1, 0 },
{ 0, 1, 1 },
{ 0, 0, 0 } };
vector exitPoints = FindExitPoint(matrix);
cout << exitPoints[0] << " " << exitPoints[1];
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG {
// Function to find the exit
// point in a given matrix
public static int[] FindExitPoint(int[][] matrix)
{
// initialization of row, column
int i = 0, j = 0;
int dir = 0;
while (true) {
dir = (dir + matrix[i][j]) % 4;
// If a cell is traversed
// then mark it has 0
if (matrix[i][j] == 1) {
matrix[i][j] = 0;
}
// Right direction
if (dir == 0) {
j++;
}
// Down direction
else if (dir == 1) {
i++;
}
// Left direction
else if (dir == 2) {
j--;
}
// Up direction
else if (dir == 3) {
i--;
}
// decrement either the row or col
// since it crossed the boundary
if (i < 0) {
i++;
break;
}
else if (i == matrix.length) {
i--;
break;
}
else if (j < 0) {
j++;
break;
}
else if (j == matrix[0].length) {
j--;
break;
}
}
// return row and column
int[] v = new int[] { i, j };
return v;
}
// Driver Code
public static void main(String[] args)
{
int[][] matrix = new int[][] { { 0, 1, 0 },
{ 0, 1, 1 },
{ 0, 0, 0 } };
int[] exitPoints = FindExitPoint(matrix);
System.out.println(exitPoints[0] + " "
+ exitPoints[1]);
}
}
// This code is contributed by rakeshsahni
Python3
# Python code for the above approach
# Function to find the exit
# point in a given matrix
def FindExitPoint(matrix) :
# initialization of row, column
i = 0
j = 0;
dir = 0;
while (True):
dir = (dir + matrix[i][j]) % 4;
# If a cell is traversed
# then mark it has 0
if (matrix[i][j] == 1):
matrix[i][j] = 0;
# Right direction
if (dir == 0):
j += 1
# Down direction
elif (dir == 1):
i += 1
# Left direction
elif (dir == 2):
j -= 1
# Up direction
elif (dir == 3):
i -= 1
# decrement either the row or col
# since it crossed the boundary
if (i < 0):
i += 1
break;
elif (i == len(matrix)):
i -= 1
break;
elif (j < 0):
j += 1
break;
elif (j == len(matrix[0])):
j -= 1
break
# return row and column
v = [i, j];
return v
# Driver Code
matrix = [[0, 1, 0], [0, 1, 1], [0, 0, 0]];
exitPoints = FindExitPoint(matrix);
print(f"{exitPoints[0]} {exitPoints[1]}");
# This code is contributed by Saurabh Jaiswal
C#
// C# program for the above approach
using System;
class GFG {
// Function to find the exit
// point in a given matrix
static int[] FindExitPoint(int[, ] matrix)
{
// initialization of row, column
int i = 0, j = 0;
int dir = 0;
while (true) {
dir = (dir + matrix[i, j]) % 4;
// If a cell is traversed
// then mark it has 0
if (matrix[i, j] == 1) {
matrix[i, j] = 0;
}
// Right direction
if (dir == 0) {
j++;
}
// Down direction
else if (dir == 1) {
i++;
}
// Left direction
else if (dir == 2) {
j--;
}
// Up direction
else if (dir == 3) {
i--;
}
// decrement either the row or col
// since it crossed the boundary
if (i < 0) {
i++;
break;
}
else if (i == matrix.GetLength(0)) {
i--;
break;
}
else if (j < 0) {
j++;
break;
}
else if (j == matrix.GetLength(1)) {
j--;
break;
}
}
// return row and column
int[] v = new int[] { i, j };
return v;
}
// Driver Code
public static void Main()
{
int[, ] matrix = new int[, ] { { 0, 1, 0 },
{ 0, 1, 1 },
{ 0, 0, 0 } };
int[] exitPoints = FindExitPoint(matrix);
Console.WriteLine(exitPoints[0] + " "
+ exitPoints[1]);
}
}
// This code is contributed by Samim Hossain Mondal.
Javascript
1 0
时间复杂度: O(NxM),其中N是行数, M是列数。
辅助空间: O(1)