简体   繁体   English

边数

[英]Number of Edges

You are given a grid;给你一个网格; having n rows;n行; and m columns;m列; where two cells are called to be adjacent if : they have a common side.如果两个单元格被称为相邻,则它们有共同的一面。

let two adjacent cells be a and b .让两个相邻的单元格是ab Since they are adjacent;因为它们是相邻的; hence;因此; you may go both from a to b ;你可以 go 从ab ; and also from b to a .以及从ba

In terms of graph theory;在图论方面; we may say that if the grid is modelled as a directed graph;我们可以说,如果网格被建模为有向图; then, there exists a directed edge from a to b ;那么,存在从ab的有向边; and also a from b to;还有ab到; in case cells a and b are adjacent.如果单元格ab相邻。

You are asked to find the number of directed edges in the graph.你被要求找出图中有向边的数量。

Input Format输入格式

The first line of the input contains a single integer T ;输入的第一行包含单个 integer T denoting the number of test cases.表示测试用例的数量。

Then;然后; t lines follow;接下来是t行; where each line contains two space seperated integers n and m ;其中每行包含两个空格分隔的整数nm the dimensions of the grid respectively.网格的尺寸分别。

Sample Input 0样本输入 0

1
3 3

Sample Output 0样品 Output 0

24

Explanation 0解释 0

解释

Number of the directed edges is 24.有向边数为 24。

Is this approach correct?这种方法正确吗? My code did passes the sample test cases but fails for others我的代码确实通过了示例测试用例,但其他人却失败了

def compute(m,n):
 arr = [[0 for x in range(n)] for y in range(m)] 
 arr[0][0]=2
 arr[n-1][m-1]=2
 arr[0][m-1]=2
 arr[n-1][0]=2
 for i in range (1,m-1):
     arr[i][0]=3
     arr[i][n-1]=3
 for j in range (1,n-1):
     arr[0][j]=3
     arr[n-1][j]=3
 for i in range (1,n-2):
     for j in range (1,m-2):
         arr[i][j]=4
 return sum(sum(arr,[])) +4 

Please explain the correct approach for this problem.Thanks in advance请解释这个问题的正确方法。在此先感谢

I think you can solve this with dynamic programming.我认为你可以用动态编程来解决这个问题。

number of edge in m*n = number of edge in (m-1)*(n) + to_be_calculated

Then you can simply find the amount of to_be_calculated by 2*n + 2*(n-1)然后你可以简单地找到to_be_calculated by 2*n + 2*(n-1)

After you finished with columns and reached to m == 1 then you can reduce n to 1 .完成列并达到m == 1后,您可以将n减少到1

def compute(n,m,sum):
  if n == 1 and m == 1:
    return sum
  elif m == 1:
    return compute(1, 1, sum + 2*(n-1))
  else:
    return compute(n, m-1, sum + 2*n + 2*(n-1) )

compute(5,5,0) # For table 5x5

For a grid of n rows and m columns: the number of sides in any row is m-1, the number of sides in any column is n-1.对于 n 行 m 列的网格:任何一行的边数是 m-1,任何一列的边数是 n-1。 Every side has two edges in the graph of adjacent cells.在相邻单元格的图中,每一边都有两条边。

Therefore the number of edges for an n*m grid is:因此,n*m 网格的边数为:

def compute(n, m):
    return n * (m - 1) * 2 + m * (n - 1) * 2

Or, simplified even further:或者,进一步简化:

def compute(n, m):
    return 4 * n * m - 2 * n - 2 * m

Your algorithm goes and fills in the individual edges to sum them at the end, which is far more complicated than it needs to be for this problem without additional constraints.您的算法会填充各个边以在最后对它们求和,这比在没有额外约束的情况下解决此问题所需的复杂得多。

You can find a formula to compute the number of edges in the graph as follows: Suppose we have a grid with dimensions n and m .您可以找到一个公式来计算图中的边数,如下所示:假设我们有一个尺寸为 nm的网格。 For each cell we need to count the number of neighbor cells.对于每个单元,我们需要计算相邻单元的数量。 Then, the summation of such numbers is the number of edges.那么,这些数字的总和就是边的数量。

Case 1) such a grid has 4 corner cells each with 2 neighbors ;案例 1)这样的网格有4 个角单元,每个角单元有2 个邻居 total neighbors case 1: 4*2= 8总邻居案例 1:4*2= 8

Case 2: such a grid has 2(n+m-2)-4 cells in its sides exclude the corners each with 3 neighbors, total neighbors case 2: (2 (n+m-2)-4) 3情况 2:这样的网格在其边上有2(n+m-2)-4 个单元格,不包括每个有 3 个邻居的角,总共邻居情况 2: (2 (n+m-2)-4) 3

Case 3) such a grid has nm-(2(n+m-2)-4)-4 inner cells each with 4 neighbors , total neighbors case 3: * (nm-(2(n+m-2)-4)-4) 4案例 3)这样的网格有nm-(2(n+m-2)-4)-4 个内部单元格,每个单元格有4 个邻居,总共邻居案例 3:* (nm-(2(n+m-2)-4 )-4) 4

Total number of edges = Case 1 + Case 2 + Case 3 = 8 + (2(n+m-2)-4)3 + (nm-(2(n+m-2)-4)-4)4 = 4nm - 2(n+m)总边数 = 案例 1 + 案例 2 + 案例 3 = 8 + (2(n+m-2)-4)3 + (nm-(2(n+m-2)-4)-4)4 = 4nm - 2(n+m)

The figure below displays all the cases:下图展示了所有案例: 在此处输入图像描述

So you can use the code below to compute the number of edges:因此,您可以使用下面的代码来计算边数:

def compute_total_edges(m,n):
    return 4*n*m-2*(n+m)

print(compute_total_edges(3,3))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM