📌  相关文章
📜  找到具有给定 3 个顶点的矩形的第四个顶点的坐标

📅  最后修改于: 2021-10-23 08:35:35             🧑  作者: Mango

给定一个N * M网格。它正好包含三个“*” ,其他每个单元格都是一个“.”。其中“*”表示矩形的顶点。任务是找到第四个(缺失的)顶点的坐标(基于 1 的索引)。
例子:

方法:通过迭代给定的网格来找到 3 个顶点的坐标。由于矩形由 2 个 X 坐标和 2 个 Y 坐标以及 4 个顶点组成,因此每个 X 坐标和 Y 坐标都应该恰好出现两次。我们可以计算每个 X 和 Y 坐标在 3 个给定顶点中出现的次数,而第 4 个将具有仅出现一次的坐标。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that return the coordinates
// of the fourth vertex of the rectangle
pair findFourthVertex(int n, int m, string s[])
{
    map row, col;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
 
            // Save the coordinates of the given
            // vertices of the rectangle
            if (s[i][j] == '*') {
                row[i]++;
                col[j]++;
            }
    int x, y;
    for (auto tm : row)
        if (tm.second == 1)
            x = tm.first;
    for (auto tm : col)
        if (tm.second == 1)
            y = tm.first;
 
    // 1-based indexing
    return make_pair(x + 1, y + 1);
}
 
// Driver code
int main()
{
    string s[] = { "*.*", "*..", "..." };
    int n = sizeof(s) / sizeof(s[0]);
    int m = s[0].length();
 
    auto rs = findFourthVertex(n, m, s);
    cout << rs.first << " " << rs.second;
}


Java
// Java implementation of the approach
import java.util.HashMap;
import java.util.Map;
 
class GfG
{
 
    // Function that return the coordinates
    // of the fourth vertex of the rectangle
    static Pair findFourthVertex(int n,
                                        int m, String s[])
    {
        HashMap row = new HashMap<>();
        HashMap col = new HashMap<>();
         
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
     
                // Save the coordinates of the given
                // vertices of the rectangle
                if (s[i].charAt(j) == '*')
                {
                     
                    if (row.containsKey(i))
                    {
                        row.put(i, row.get(i) + 1);
                    }
                    else
                    {
                        row.put(i, 1);
                    }
                     
                    if (col.containsKey(j))
                    {
                        col.put(j, col.get(j) + 1);
                    }
                    else
                    {
                        col.put(j, 1);
                    }
                }
            }
        }
                 
        int x = 0, y = 0;
        for (Map.Entry entry : row.entrySet())
        {
            if (entry.getValue() == 1)
                x = entry.getKey();
        }
                 
        for (Map.Entry entry : col.entrySet())
        {
            if (entry.getValue() == 1)
                y = entry.getKey();
        }
     
        // 1-based indexing
        Pair ans = new Pair<>(x + 1, y + 1);
        return ans;
    }
 
    // Driver Code
    public static void main(String []args)
    {
         
        String s[] = { "*.*", "*..", "..." };
        int n = s.length;
        int m = s[0].length();
     
        Pair rs = findFourthVertex(n, m, s);
        System.out.println(rs.first + " " + rs.second);
    }
}
 
class Pair
{
    A first;
    B second;
 
    public Pair(A first, B second)
    {
        this.first = first;
        this.second = second;
    }
}
     
// This code is contributed by Rituraj Jain


Python3
# Python3 implementation of the approach
 
# Function that return the coordinates
# of the fourth vertex of the rectangle
def findFourthVertex(n, m, s) :
 
    row = dict.fromkeys(range(n), 0)
    col = dict.fromkeys(range(m), 0)
     
    for i in range(n) :
        for j in range(m) :
 
            # Save the coordinates of the given
            # vertices of the rectangle
            if (s[i][j] == '*') :
                row[i] += 1;
                col[j] += 1;
 
    for keys,values in row.items() :
        if (values == 1) :
            x = keys;
             
    for keys,values in col.items() :
        if (values == 1) :
            y = keys;
 
    # 1-based indexing
    return (x + 1, y + 1) ;
 
# Driver code
if __name__ == "__main__" :
     
    s = [ "*.*", "*..", "..." ]
    n = len(s);
    m = len(s[0]);
     
    rs = findFourthVertex(n, m, s);
    print(rs[0], rs[1])
 
# This code is contributed by Ryuga


C#
// C# implementation of the approach
using System;
using System.Collections.Generic;
 
class GfG
{
 
    // Function that return the coordinates
    // of the fourth vertex of the rectangle
    static Pair findFourthVertex(int n,
                                        int m, String []s)
    {
        Dictionary row = new Dictionary();
        Dictionary col = new Dictionary();
         
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
     
                // Save the coordinates of the given
                // vertices of the rectangle
                if (s[i][j] == '*')
                {
                     
                    if (row.ContainsKey(i))
                    {
                        row[i] = row[i] + 1;
                    }
                    else
                    {
                        row.Add(i, 1);
                    }
                     
                    if (col.ContainsKey(j))
                    {
                        col[j] = col[j] + 1;
                    }
                    else
                    {
                        col.Add(j, 1);
                    }
                }
            }
        }
                 
        int x = 0, y = 0;
        foreach(KeyValuePair entry in row)
        {
            if (entry.Value == 1)
                x = entry.Key;
        }
                 
        foreach(KeyValuePair entry in col)
        {
            if (entry.Value == 1)
                y = entry.Key;
        }
     
        // 1-based indexing
        Pair ans = new Pair(x + 1, y + 1);
        return ans;
    }
 
    // Driver Code
    public static void Main(String []args)
    {
         
        String []s = { "*.*", "*..", "..." };
        int n = s.Length;
        int m = s[0].Length;
     
        Pair rs = findFourthVertex(n, m, s);
        Console.WriteLine(rs.first + " " + rs.second);
    }
}
 
class Pair
{
    public A first;
    public B second;
 
    public Pair(A first, B second)
    {
        this.first = first;
        this.second = second;
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:

2 3

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程