给定一个区间寻找三角形_计算给定图片中的三角形数量–程序化解决方案

给定一个区间寻找三角形

One of my friend sent me below image as puzzle to count the triangles. I started counting them and first got to 15, then again recounted and reached to 19.

我的一位朋友给我发了下图作为拼图来计算三角形。 我开始计算它们,首先达到15,然后再次计数,达到19。

But I was not sure whether I was right or not. So I thought of writing a simple program to find out the total number of triangles. I used below algorithm to find out the number of triangles.

但是我不确定我是否正确。 因此,我想到编写一个简单的程序来找出三角形的总数。 我使用下面的算法找出三角形的数量。

  • Any triangle has three distinct points.

    任何三角形都有三个不同的点。
  • Any triangle consists of three different lines.

    任何三角形均由三条不同的线组成。

Now counting lines with points is easy, from below image we can easily see that it contains 7 lines.

现在用点数线很容易,从下面的图像中我们可以很容易地看到它包含7条线。

Below is the program to find out the total number of triangles in these scenarios.

下面是查找这些情况下三角形总数的程序。

package com.journaldev.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

public class FindTriangles {

	public static void main(String[] args) {

		// Scanner scanner = new Scanner(System.in);
		// System.out.println("Please enter number of lines");
		// int count = Integer.parseInt(scanner.nextLine());
		//
		// List<String> lines = new ArrayList<String>();
		// for(int i=0; i<count; i++){
		// System.out.println("Please enter line "+(i+1));
		// lines.add(scanner.nextLine());
		// }
		// scanner.close();
		// System.out.println("Lines are::"+lines.toString());

		findTriangles(Arrays.asList(new String[] { "ABC", "CDEF", "AIF",
				"BGHI", "CKJI", "AGKD", "AHJE" }));
		findTriangles(Arrays.asList(new String[] { "AB","BC","CA"}));
	}

	// Logic to find triangles
	public static void findTriangles(List<String> lines) {
		int count=0;
		// Find total distinct points using Set
		Set<String> points = new HashSet<String>();
		for (String line : lines) {
			char[] pointArray = line.toCharArray();
			for (char c : pointArray)
				points.add(c + "");
		}
		System.out.println("Points are::" + points.toString());
		
		//Create Array from Set
		String[] pointsArray = new String[points.size()];
		pointsArray = points.toArray(pointsArray);
		System.out.println(Arrays.toString(pointsArray));
		
		//Create distinct points set by looping
		for(int i=0; i< pointsArray.length-2; i++){
			for(int j=i+1; j< pointsArray.length-1; j++){
				for(int k=j+1; k< pointsArray.length; k++){
					// Triangle has 3 distinct points and 3 different lines
					String triangleOption = pointsArray[i]+pointsArray[j]+pointsArray[k];
					//if all 3 points are in single line, its not a triangle
					// also 2 points should be part of a line
					boolean b1 = false;
					boolean b2 = false;
					boolean b3 = false;
					
					for( String line : lines){
						if(line.contains(pointsArray[i]) 
								&& line.contains(pointsArray[j]) 
								&& !line.contains(pointsArray[k])){
							b1=true;
						}
						if(line.contains(pointsArray[i]) 
								&& line.contains(pointsArray[k]) 
								&& !line.contains(pointsArray[j])){
							b2=true;
						}
						if(line.contains(pointsArray[j]) 
								&& line.contains(pointsArray[k]) 
								&& !line.contains(pointsArray[i])){
							b3=true;
						}
					}
					if(b1 && b2 && b3){
						count++;
						System.out.println(triangleOption);
					}
				}
			}
		}
		System.out.println("Total Triangles="+count);
	}

}

Below is the output of the above program.

下面是上述程序的输出。

Points are::[D, E, F, G, A, B, C, H, I, J, K]
[D, E, F, G, A, B, C, H, I, J, K]
DEA
DFA
DAC
DCK
EFA
EAC
ECJ
FAC
FCI
GAB
GAH
GAI
GIK
ABH
ABI
ACI
ACJ
ACK
AHI
AIJ
AIK
AJK
BCI
HIJ
Total Triangles=24
Points are::[A, B, C]
[A, B, C]
ABC
Total Triangles=1

So the total number of triangles is 24, well I missed a lot in counting them. But now I have an easy way to do it, I hope it will help someone too for these kind of problems.

所以三角形的总数是24,所以我在计算它们时非常想念。 但是,现在我有一个简单的方法来实现它,我希望它也可以对此类问题有所帮助。

翻译自: https://www.journaldev.com/7064/count-the-number-of-triangles-in-given-picture-programmatic-solution

给定一个区间寻找三角形

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值