需要一个Ruby的方法来确定一个矩阵QUOT的元素;触摸"另一个元素元素、矩阵、方法来、Ruby

2023-09-11 05:20:36 作者:如月

我想我需要一个所谓的感人的方法(如连续的,没有感情的。) 我需要确定一个矩阵的旁边,是一个单独的元素或设置元素的那些元素。至少这是我想到的解决手头的问题的方式。

在下面再presents程序中的矩阵状态,比方说,一些水下地形。当我放下水,最终的最高点会伸出,并成为一个孤岛。当水位是34,那么元素状态[2,3]是单点的岛屿。阵列亚特兰蒂斯认为单点的坐标。

由于我们降低水位进一步,附加分将是露出水面。附加连续点将成为岛的一部分,并且其坐标将被添加到阵列亚特兰蒂斯。 (例如,下一块土地是亚特兰蒂斯的部分是国发[3,4] 31日。)

我如何做到这一点的想法是,以确定所有触碰/是旁亚特兰蒂斯元素的矩阵元素,找到一个与海拔最高,然后将其添加到阵列中的亚特兰蒂斯。寻找的元素旁边的一个元素,本身就是一个挑战,但我们可以写一些code检查组[I,J-1],[I,J + 1],[I-1,J -1],[I-1,j]时,[I-1,J + 1],[I + 1,J-1],[I + 1,J],[I + 1,J + 1]。 (我想我说对了。)

但是,当我们添加其他点,确定该点周围的点在亚特兰蒂斯的任务变得越来越困难。所以这是我的问题:任何人都可以想到的任何机制,做到这一点?任何一种使用这些我不知道红宝石的功能简化的算法? (其中包括所有,但最基本的。)如果可以写这样的方法,那么我可以写atlantis.touching,并得到一个阵列,例如包含的所有点presently连续亚特兰蒂斯所有的坐标。

至少这是如何我想可以这样做。任何其他的想法将受到欢迎。如果有人知道任何形式的合作网站,我可以寻求其他人谁可能有兴趣与我在这工作,那将是巨大的。

 #创建使用矩阵状态数据库
需要'矩阵'

国家=矩阵[3,1,4,4,6,2,8,12,8,2]
              [6,2,4,13,25,21,11,22,9,3,],
              [6,20,27,34,22,14,12,11,2,5]
              [6,28,17,23,31,18,11,9,18,12]
              [9,18,11,13,8,9,10,14,24,11]
              [3,9,7,16,9,12,28,24,29,21]
              [5,8,4,7,17​​,14,19,30,33,4]
              [7,17,23,9,5,9,22,21,12,21,],
              [7,14,25,22,16,10,19,15,12,11]
              [5,16,7,3,6,3,9,8,1,5]

#find沙爹元素连续岛屿
亚特兰蒂斯= [[2,3]]

找到所有的状态[I,J]触摸亚特兰蒂斯
 

解决方案

只检查周围的当前曝光区域的点听起来并不像它可以涵盖所有的情况下 - 如果下一个点被曝光是一个开始新洲?

我会去了解它是这样的:有另一个阵列 - 姑且称之为排序包含排序身高您的观点。你抬高水位时,都会弹出所有的比新的水位较高元素关闭排序键,到亚特兰蒂斯

其实,没有必要单独排序亚特兰蒂斯阵列,如果你做这种方式。只是存储的最高点的指数的没有的上面的水,你已经基本上得到了两个阵列在一个 - 一切都在水面上的一个侧面,水上和水下的一切另一

希望帮助!

声而不凡 东微RUBY系列旗舰版数字音频处理器 闪耀发布

I think I need a method called "Touching" (as in contiguous, not emotional.) I need to identify those elements of a matrix that are next to an individual element or set of elements. At least that’s the way I’ve thought of to solve the problem at hand.

The matrix State in the program below represents, let’s say, some underwater topography. As I lower the water, eventually the highest point will stick out and become an "island". When the "water level" is at 34 then the element State[2,3] is the single point of the island. The array atlantis holds the coordinates of that single point .

As we lower the water level further, additional points will be "above water." Additional contiguous points will become part of the island and their coordinates would be added to the array atlantis. (For example, the next piece of land to be part of atlantis would be State[3,4] at 31.)

My thought about how to do this is to identify all the matrix elements that touch/are next to the element in the atlantis, find the one with the highest elevation and then add it to the array atlantis. Looking for the elements next to a single element is a challenge in itself, but we could write some code to examine the set [i,j-1], [i,j+1], [i-1,j-1], [i-1,j], [i-1,j+1], [i+1,j-1], [i+1,J], [i+1,j+1]. (I think I got that right.)

But as we add additional points, the task of determining which points surround the points in atlantis becomes increasingly difficult. So that’s my question: can anyone think of any mechanism by which to do this? Any kind of simplified algorithm using capabilities of ruby of which I am unaware? (which include all but the most basic.) If such a method could be written then I could write atlantis.touching and get an array, for example, containing all the coordinates of all the points presently contiguous to atlantis.

At least that’s how I’m thinking this could be done. Any other ideas would be welcome. And if anyone knows any kind of partnering site where I could seek others who might be interested in working with me on this, that would be great.

# create State database using matrix
require 'matrix'

State=Matrix[ [3,1,4,4,6,2,8,12,8,2],
              [6,2,4,13,25,21,11,22,9,3,],
              [6,20,27,34,22,14,12,11,2,5],
              [6,28,17,23,31,18,11,9,18,12],
              [9,18,11,13,8,9,10,14,24,11],
              [3,9,7,16,9,12,28,24,29,21],
              [5,8,4,7,17,14,19,30,33,4],
              [7,17,23,9,5,9,22,21,12,21,],
              [7,14,25,22,16,10,19,15,12,11],
              [5,16,7,3,6,3,9,8,1,5] ]

#find sate elements contiguous to island
atlantis=[[2,3]]

find all state[i,j] "touching" atlantis

解决方案

Only checking the points around the currently exposed area doesn't sound like it could cover every case - what if the next point to be exposed was the beginning of a new island?

I'd go about it like this: Have another array - let's call it sorted which contains your points sorted by height. Every time you raise the water level, pop all the elements higher than the new water level off sorted and onto atlantis.

In fact, there's no need for separate sorted and atlantis arrays if you do it this way. Just store the index of the highest point not above water, and you've essentially got two arrays in one - everything above water on one side, and everything below water on the other.

Hope that helps!