📌  相关文章
📜  找到给定两个坐标的 Rectangle 的任何可能的两个坐标

📅  最后修改于: 2022-05-13 01:56:09.753000             🧑  作者: Mango

找到给定两个坐标的 Rectangle 的任何可能的两个坐标

给定一个大小为N×N的矩阵mat[][] ,其中矩阵的两个元素“1”表示矩形的坐标, “0”表示空白空间,任务是找到矩形的其他两个坐标.
注意:这个问题可能有多个答案,打印其中任何一个。

例子:

方法:可以使用这些给定的坐标找到剩余的坐标,因为一些点可能有一个共同的行,而一些点可能有一个共同的列。请按照以下步骤解决问题:

  • 初始化两对,比如p1p2以存储1在初始矩阵mat[] 中的位置。
  • 初始化两对,例如p3p4以存储要插入新1以使其成为矩形的位置。
  • 使用两个嵌套循环遍历矩阵并找到对p1p2
  • 现在有三种可能的情况:
    • 如果在这种情况下p1.firstp2.first相同,则将p1.first和 p2.first 加1得到p3.firstp4.first ,而p3.secondp4.secondp1.secondp2 保持相同。分别为第二
    • 如果p1.secondp2.second 相同 在这种情况下,将 1 添加到p1.secondp2.second给我们p3.secondp4.secondp3.firstp4.first保持与p1.firstp2.first相同
    • 如果没有相同的坐标,则p3.first = p2.firstp3.second = p1.secondp4.first = p1.firstp4.second = p2.second
  • p3p4的坐标替换为1并打印矩阵。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the remaining
// two rectangle coordinates
void Create_Rectangle(vector arr, int n)
{
 
    // Pairs to store the position of given
    // two coordinates of the rectangle.
    pair p1 = { -1, -1 };
    pair p2 = { -1, -1 };
 
    // Pairs to store the remaining two
    // coordinates of the rectangle.
    pair p3;
    pair p4;
 
    // Traverse through matrix and
    // find pairs p1 and p2
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (arr[i][j] == '1')
                if (p1.first == -1)
                    p1 = { i, j };
                else
                    p2 = { i, j };
        }
    }
 
    p3 = p1;
    p4 = p2;
 
    // First Case
    if (p1.first == p2.first) {
        p3.first = (p1.first + 1) % n;
        p4.first = (p2.first + 1) % n;
    }
    // Second Case
    else if (p1.second == p2.second) {
        p3.second = (p1.second + 1) % n;
        p4.second = (p2.second + 1) % n;
    }
    // Third Case
    else {
        swap(p3.first, p4.first);
    }
 
    arr[p3.first][p3.second] = '1';
    arr[p4.first][p4.second] = '1';
 
    // Print the matrix
    for (int i = 0; i < n; i++) {
        cout << arr[i] << endl;
    }
}
 
// Driver code
int main()
{
    // Given Input
    int n = 4;
    vector arr{ "0010", "0000", "1000", "0000" };
 
    // Function Call
    Create_Rectangle(arr, n);
    return 0;
}


Java
// Java program for above approach
import java.awt.*;
import java.util.*;
class GFG{
    static class pair{
        T first;
        V second;
    }
   
    // Function to find the remaining
    // two rectangle coordinates
    static void Create_Rectangle(ArrayList arr, int n)
    {
 
        // Pairs to store the position of given
        // two coordinates of the rectangle.
        pair p1 = new pair<>();
        p1.first = -1;
        p1.second= -1;
        pair p2 = new pair<>();
        p2.first = -1;
        p2.second = -1;
 
        // Pairs to store the remaining two
        // coordinates of the rectangle.
        pair p3 = new pair<>();
        pair p4 = new pair<>();
 
        // Traverse through matrix and
        // find pairs p1 and p2
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (arr.get(i).charAt(j) == '1')
                    if (p1.first == -1) {
                        p1.first =i;
                        p1.second = j;
                    }
                    else {
                        p2.first = i;
                        p2.second = j;
                    }
            }
        }
 
        p3 = p1;
        p4 = p2;
 
        // First Case
        if (p1.first.intValue() == (p2.first).intValue()) {
            p3.first = (p1.first + 1) % n;
            p4.first = (p2.first + 1) % n;
        }
        // Second Case
        else if (p1.second.intValue()==(p2.second).intValue()) {
            p3.second = (p1.second + 1) % n;
            p4.second = (p2.second + 1) % n;
        }
        // Third Case
        else {
            int temp = p3.first;
            p3.first = p4.first;
            p4.first = temp;
        }
 
        // Print the matrix
        for (int i = 0; i < n; i++) {
            if(i==p3.first){
                for (int j = 0;j arr = new ArrayList<>();
        arr.add("0010");
        arr.add("0000");
        arr.add("1000");
        arr.add("0000");
        //{ , "0000", "1000", "0000" };
 
        // Function Call
        Create_Rectangle(arr, n);
    }
}
 
// This code is contributed by hritikrommie.


Python3
# Python program for the above approach
 
# Function to find the remaining
# two rectangle coordinates
def Create_Rectangle(arr, n):
 
    for i in range(n):
        arr[i] = [i for i in arr[i]]
 
    # Pairs to store the position of given
    # two coordinates of the rectangle.
    p1 = [-1, -1]
    p2 = [-1, -1]
 
    # Pairs to store the remaining two
    # coordinates of the rectangle.
    p3 = []
    p4 = []
 
    # Traverse through matrix and
    # find pairs p1 and p2
    for i in range(n):
        for j in range(n):
            if (arr[i][j] == '1'):
                if (p1[0] == -1):
                    p1 = [i, j]
                else:
                    p2 = [i, j]
 
    p3 = p1
    p4 = p2
 
    # First Case
    if (p1[0] == p2[0]):
        p3[0] = (p1[0] + 1) % n
        p4[0] = (p2[0] + 1) % n
    # Second Case
    elif (p1[1] == p2[1]):
        p3[1] = (p1[1] + 1) % n
        p4[1] = (p2[1] + 1) % n
    # Third Case
    else:
        p3[0], p4[0] = p4[0],p3[0]
 
    arr[p3[0]][p3[1]] = '1'
    arr[p4[0]][p4[1]] = '1'
 
    # Print the matrix
    for i in range(n):
        print("".join(arr[i]))
 
 
# Driver code
if __name__ == '__main__':
    # Given Input
    n = 4
    arr = ["0010", "0000", "1000", "0000"]
 
    # Function Call
    Create_Rectangle(arr, n)
 
# This code is contributed by mohit kumar 29.


Javascript



输出
1010
0000
1010
0000

时间复杂度: O(N 2 )
辅助空间: O(1)