【问题标题】:find an object which has at least 1 parameter(out of three)that matches with corresponding parameter of each object from array找到一个对象,该对象至少有 1 个参数(三个参数中)与数组中每个对象的相应参数匹配
【发布时间】:2014-11-12 19:43:02
【问题描述】:

我正在创建一个匹配的益智游戏,但我一直在为这个函数创建逻辑。

Node 是一个有 3 个参数的类:

{
 int a;
 int b;
 int c;
} 

如果我有 2 个节点对象,比如 n1n2

(n1 == n2) if (n1.a == n2.a || n1.b == n2.b || n1.c == n2.c)    

如果:

n1.a=6, n1.b=4, n1.c=3

和:

n2.a=4, n2.b=4, n2.c=5.

这里( n1 == n2 )n1n2 连接,因为( n1.b == n2.b )

问题:我需要为接受node 对象数组的函数编写逻辑,它应该返回一个node 对象,该对象可以与数组中的所有nodes 连接。如果连接节点是不可能的,它应该返回一个空值。所以返回的节点应该与数组的每个对象至少有一个共同的参数。

我使用的是 ActionScript 3,但只需要 AS3 或伪代码中的逻辑部分。

【问题讨论】:

  • 什么编程语言?请适当标记。
  • 我正在使用 as3,但我只需要伪代码或 as3 中的逻辑。
  • OK - 为您修复标签。
  • 很酷,你知道我可以使用的逻辑吗?
  • 不是随手 - 不过这看起来是个很有趣的问题。

标签: algorithm actionscript-3


【解决方案1】:

您需要有一组满足此条件的可能点,并在添加到列表中的每个新点处将其过滤掉。首先,您必须生成许多“任意任意”点,这些点必须与正常点区分开来。您从一个“任意,任意,任意”点开始算法,然后每当添加一个点(a,b,c)时,您检查现有点的列表,并删除任何不符合“a,b, c",并且带有 "any" 的点现在轴锁定到任一轴,这意味着第一步将设置 "a,any,any","any,b,any","any,any,c"列表中的点数。这种情况一直持续到处理完所有列表,或者列表中没有剩余点为止。

function allconnected(nodes:Vector.<Node>):Node {
    var list:Array=[];
    list.push({a:null,b:null,c:null}); // "any,any,any" initial object
    for (var i:int=nodes.length-1;i>=0;i--) {
        var node:Node=nodes[i]; // current node
        if (list.length==0) return null; // no nodes match
        for (var j:int=list.length-1;j>=0;j--) {
            var o:Object=list.splice(j,1)[0]; // get the element out of the array
            var pushed:Boolean=false;
            if (o.a!==null) if (!pushed && (o.a==node.a)) {
                list.push(o);
                pushed=true;
            }
            if (o.b!==null) if (!pushed && (o.b==node.b)) {
                list.push(o);
                pushed=true;
            }
            if (o.c!==null) if (!pushed && (o.c==node.c)) {
                list.push(o);
                pushed=true;
            }
            // if this point connects by either side, and the side is defined, push it back
            if (o.a===null) list.push({a:node.a,b:o.b,c:o.c});
            if (o.b===null) list.push({a:o.a,b:node.b,c:o.c});
            if (o.c===null) list.push({a:o.a,b:o.b,c:node.c});
            // and if any side is "any", push an axis-aligned object in the list
        } // this way new object that are aligned with current node 
        // are put into the array in an already processed segment, so we won't hit
        // an infinite loop
    }
    // okay if we are here, then there is something left in the array
    var result:Node=new Node();
    result.a=list[0].a; // 0 if null, this is pretty much OK
    result.b=list[0].b;
    result.c=list[0].c;
    return result;
}

这段代码是动态写的,里面可能有错误,请不要盲目复制粘贴。

【讨论】:

  • 我不认为这很简单!我测试了你的代码,举这个例子: n0 : 3,2,8; n1 : 2,7,8; n2 : 1,2,9; n3 : 0,6,0; n4 : 2,2,9; n5 : 8,7,9,这里你的代码给出了 3,7,9 但我们可以看到 n3 (0,6,0) 没有连接到 3,7,9,第二个例子:n0 : 8,9,4 ; n1 : 9,4,4; n2 : 0,3,2; n3 : 1,8,6; n4 : 5,0,9; n5 : 0,3,0, 代码给出了 8, 4, 6 与 n2, n4 和 n5 无关,这些示例中使用的所有对象都是随机生成的。我尝试写一些东西,因为我喜欢这类问题,但现在我没有得到适用于所有情况的代码。
  • 知道了,我告诉过你,可能有错误。如果o.a==0if (!o.a) 返回真。将 null 比较更改为严格形式 ===!==,请再次检查。
猜你喜欢
  • 2018-06-21
  • 2011-06-05
  • 1970-01-01
  • 2017-10-12
  • 2013-04-08
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
  • 2013-07-08
相关资源
最近更新 更多