Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >基于启发式的8字谜状态线性冲突的计数

基于启发式的8字谜状态线性冲突的计数
EN

Code Review用户
提问于 2014-03-15 01:49:59
回答 1查看 5.6K关注 0票数 7

需要找出8种状态的线性冲突,状态用int[9]表示,目标状态为{1,2,3,4,5,6,7,8,0}。线性冲突是,如果在一条线中,应该在这条线上的两个瓷砖被反转。

例如,在目标状态中,如果第一行为2, 1 ,3,则第一行为1,2,3,即由tiles 2和1造成的线性冲突。

我的代码很好用,但是太长了,太尴尬了。下面是:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public static int linearConflicts(int[] state) {
    ArrayList<Integer> row1 = new ArrayList<Integer>();
    ArrayList<Integer> row2 = new ArrayList<Integer>();
    ArrayList<Integer> row3 = new ArrayList<Integer>();
    ArrayList<Integer> column1 = new ArrayList<Integer>();
    ArrayList<Integer> column2 = new ArrayList<Integer>();
    ArrayList<Integer> column3 = new ArrayList<Integer>();
    int[] columnMarkers = new int[] { 0, 3, 6, 1, 4, 7, 2, 5, 8 };
    for (int i = 0; i < 9; i++) {
        if (i < 3) {
            row1.add(state[i]);
            column1.add(state[columnMarkers[i]]);
        } else if (i < 6) {
            row2.add(state[i]);
            column2.add(state[columnMarkers[i]]);
        } else {
            row3.add(state[i]);
            column3.add(state[columnMarkers[i]]);
        }
    }
    return row1Conflicts(row1) + row2Conflicts(row2) + row3Conflicts(row3)
            + column1Conflicts(column1) + column2Conflicts(column2)
            + column3Conflicts(column3);

}

public static int row1Conflicts(ArrayList<Integer> rowState) {
    int conflicts = 0;
    if (rowState.contains(1)) {
        if ((rowState.contains(2))
                && rowState.indexOf(1) > rowState.indexOf(2)) {
            conflicts++;
        }
        if ((rowState.contains(3))
                && rowState.indexOf(1) > rowState.indexOf(3)) {
            conflicts++;
        }

    }
    if (rowState.contains(2) && rowState.contains(3)
            && rowState.indexOf(2) > rowState.indexOf(3))
        conflicts++;
    return conflicts;
}

public static int row2Conflicts(ArrayList<Integer> rowState) {
    int conflicts = 0;
    if (rowState.contains(4)) {
        if ((rowState.contains(5))
                && rowState.indexOf(4) > rowState.indexOf(5)) {
            conflicts++;
        }
        if ((rowState.contains(6))
                && rowState.indexOf(4) > rowState.indexOf(6)) {
            conflicts++;
        }

    }
    if (rowState.contains(5) && rowState.contains(6)
            && rowState.indexOf(5) > rowState.indexOf(6))
        conflicts++;
    return conflicts;
}

public static int row3Conflicts(ArrayList<Integer> rowState) {
    int conflicts = 0;
    if (rowState.contains(7) && rowState.contains(8)
            && rowState.indexOf(7) > rowState.indexOf(8))
        conflicts++;
    return conflicts;
}

public static int column1Conflicts(ArrayList<Integer> columnState) {
    int conflicts = 0;
    if (columnState.contains(1)) {
        if ((columnState.contains(4))
                && columnState.indexOf(1) > columnState.indexOf(4)) {
            conflicts++;
        }
        if ((columnState.contains(7))
                && columnState.indexOf(1) > columnState.indexOf(7)) {
            conflicts++;
        }

    }
    if (columnState.contains(4) && columnState.contains(7)
            && columnState.indexOf(4) > columnState.indexOf(7))
        conflicts++;
    return conflicts;
}

public static int column2Conflicts(ArrayList<Integer> columnState) {
    int conflicts = 0;
    if (columnState.contains(2)) {
        if ((columnState.contains(5))
                && columnState.indexOf(2) > columnState.indexOf(5)) {
            conflicts++;
        }
        if ((columnState.contains(8))
                && columnState.indexOf(2) > columnState.indexOf(8)) {
            conflicts++;
        }

    }
    if (columnState.contains(5) && columnState.contains(8)
            && columnState.indexOf(5) > columnState.indexOf(8))
        conflicts++;
    return conflicts;
}

public static int column3Conflicts(ArrayList<Integer> columnState) {
    int conflicts = 0;
    if (columnState.contains(3) && columnState.contains(6)
            && columnState.indexOf(3) > columnState.indexOf(6))
        conflicts++;
    return conflicts;
}

有谁知道怎么做更短,更少笨拙?如果我继续这样做,我的代码将很难阅读。

EN

回答 1

Code Review用户

回答已采纳

发布于 2014-03-15 03:33:20

关键是要像疯了一样概括!

  • 强迫您的代码处理任何方块谜题。
  • 努力为行和列重用相同的代码。

附加提示:

  • 使其面向对象,以减少参数传递杂波。
  • 对行号和列号使用基于0的索引。
  • 避免ArrayList -你的列表不会增加。数组具有更整洁的语法和更好的性能。
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// Use Arrays.binarySearch() like ArrayList.indexOf()
import static java.util.Arrays.binarySearch;

public class Puzzle {
    public static enum Axis { ROW, COL };

    private int[] state;
    private int side;

    public Puzzle(int[] state) {
        this.state = state;
        this.side = (int)Math.sqrt(state.length);
        if (side * side != state.length) {
            throw new IllegalArgumentException("Puzzle must be square");
        }
    }

    /**
     * Returns the squares of the puzzle for a specified row or column.
     *
     * @param rc row or col number (0-based)
     */
    private int[] tuple(Axis dir, int rc) {
        int[] result = new int[this.side];
        switch (dir) {
          case ROW:
            System.arraycopy(this.state, rc * this.side, result, 0, this.side);
            break;
          case COL:
            for (int i = 0, j = rc; i < this.side; i++, j += this.side) {
                result[i] = this.state[j];
            }
            break;
        }
        return result;
    }

    /**
     * Returns the squares of the puzzle of this size as if it were in
     * its solved state for a specified row or column.
     *
     * @param rc row or col number (0-based)
     */
    private int[] idealTuple(Axis dir, int rc) {
        int[] result = new int[this.side];
        switch (dir) {
          case ROW:
            for (int i = 0, j = rc * this.side + 1; i < this.side; i++, j++) {
                result[i] = (j < this.state.length) ? j : 0;
            }
            break;
          case COL:
            for (int i = 0, j = this.side + rc + 1; i < this.side; i++, j += this.side) {
                result[i] = (j < this.state.length) ? j : 0;
            }
            break;
        }
        return result;
    }

    /**
     * Count inversions (linear conflicts) for a row or column.
     */
    public int inversions(Axis dir, int rc) {
        int[] have = this.tuple(dir, rc);
        int[] want = this.idealTuple(dir, rc);
        int inversions = 0;

        // For each pair of squares, if both numbers are supposed to be in this
        // tuple, and neither is 0 (blank)...
        for (int i = 1, iPos; i < this.side; i++) {
            if (have[i] != 0 && 0 <= (iPos = binarySearch(want, have[i]))) {
                for (int j = 0, jPos; j < i; j++) {
                    if (have[j] != 0 && 0 <= (jPos = binarySearch(want, have[j]))) {
                        // ... and are inverted, count it as a conflict.
                        if ((have[i] < have[j]) != (i < j)) {
                            inversions++;
                        }
                    }
                }
            }
        }
        return inversions;
    }

    public static void main(String[] args) {
        Puzzle p = new Puzzle(new int[] {
            3, 2, 1,
            4, 7, 5,
            8, 6, 0
        });
        System.out.printf("Row %d inversions = %d\n", 0, p.inversions(Axis.ROW, 0));
        System.out.printf("Row %d inversions = %d\n", 1, p.inversions(Axis.ROW, 1));
        System.out.printf("Row %d inversions = %d\n", 2, p.inversions(Axis.ROW, 2));
        System.out.printf("Col %d inversions = %d\n", 0, p.inversions(Axis.COL, 0));
        System.out.printf("Col %d inversions = %d\n", 1, p.inversions(Axis.COL, 1));
        System.out.printf("Col %d inversions = %d\n", 2, p.inversions(Axis.COL, 2));
    }
}
票数 5
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/44427

复制
相关文章
Solidity合约的状态槽冲突问题
这是"可升级智能合约:存储要点与挑战"系列的第二篇文章。这一次我们将仔细研究Solidity合约的状态变量的存储步距以及使用delegatecall时可能发生的地址/槽位冲突问题,并分析一个存在地址冲突问题的合约的示例,最终给出相应的解决方案。
用户5687508
2021/07/04
1.2K0
纵横字谜的答案(Crossword Answers)
输入一个r行c列(1<=r, c<=10)的网格,黑格用 * 表示,每个白格都填有一个字母。
Vincent-yuan
2020/05/26
9020
基于Redis的窗口计数场景
每一个月用户只能申请三次出校,这个需要该咋做呢?这个需求等价于每一个小时只允许发三次短信验证码,真的等价吗???
CBeann
2023/12/25
2790
基于Redis的窗口计数场景
【图论搜索专题】结合状态压缩的 BFS(含启发式搜索)
这是 LeetCode 上的「847. 访问所有节点的最短路径」,难度为「困难」。
宫水三叶的刷题日记
2022/02/09
3500
【图论搜索专题】结合状态压缩的 BFS(含启发式搜索)
基于python的图像分割并计数
注:合并时一般先考虑同一父节点下的四个区域,之后再扩展到其他父节点下同层次的区域。
跋扈洋
2021/02/02
1.6K0
基于python的图像分割并计数
字谜游戏
编写一个Java应用程序,实现下列功能: (1) 程序随机分配给客户一个1-100之间的整数。 (2) 用户输入自己的猜测。 (3) 程序返回提示信息,提示信息分别是:“猜大了”、“猜小了”和“猜对了”。 (4) 用户可根据提示信息再次输入猜测,直到提示信息是“猜对了”。
算法与编程之美
2023/01/03
3470
排序8: 计数排序
这种数字对应下标的叫做绝对映射。那么如果是100 ~ 110范围的数字我们总不可能开110个空间吧,所以我们下面介绍一种相对映射的办法。
青衫哥
2023/03/31
2160
排序8: 计数排序
基于OpenCV的手掌检测和手指计数
OpenCV(开源计算机视觉库)是一个开源计算机视觉和机器学习软件库。OpenCV的构建旨在为计算机视觉应用程序提供通用的基础结构,并加速在商业产品中使用机器感知。
小白学视觉
2020/09/22
1.9K0
基于OpenCV的手掌检测和手指计数
基于SDN的网络状态测量
为了更好地管理和运行网络,非常有必要收集网络资源及其状态信息。在很多网络场景中,SDN控制器的决策都取决时延,带宽和拓扑等网络状态。在开发SDN应用的过程中,笔者总结了一些有用的网络状态测量的解决方案
SDNLAB
2018/04/02
1.9K0
基于SDN的网络状态测量
设计模式(8)-状态模式(关注状态之间的变化)
状态模式(State Pattern)是设计模式的一种,属于行为模式。 定义(源于Design Pattern):当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。   状态模式主要解决的是当控制一个对象状态的条件表达式过于复杂时的情况。把状态的判断逻辑转移到表示不同状态的一系列类中,可以把复杂的判断逻辑简化。 意图:允许一个对象在其内部状态改变时改变它的行为 适用场景:   1.一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为。   2.一个操作中
cloudskyme
2018/03/20
9810
设计模式(8)-状态模式(关注状态之间的变化)
基于梯度下降算法的线性回归
算法:基于梯度下降算法的线性回归是使用梯度下降算法进行收敛得到的最佳拟合参数,画出线性拟合的直线,数据集的点零散分布在平面内,使这些点尽可能分布在这条线上或周围。
裴来凡
2022/05/29
4010
基于梯度下降算法的线性回归
TRIZ培训:基于冲突矩阵的专利挖掘流程
TRIZ培训 中讲到:冲突矩阵给我们提供一个强有力的参考工具,通过查找矩阵中解决技术问题的发明原理,可以对潜在的技术解决方案进行预测,一旦评估这些空白区的技术方案具有可行性,就可以进行跟进研发和创新立项,用新的专利申请占领空白区域,从而高效地、系统性地实现专利挖掘与布局。
用户9972271
2023/02/23
2920
基于Numpy的线性代数运算
numpy.matrix方法的参数可以为ndarray对象 numpy.matrix方法的参数也可以为字符串str,示例如下:
潇洒坤
2018/09/10
1.1K0
基于Numpy的线性代数运算
启发式的搜索策略
搜索是人工智能中解决问题采用的主要策略,在看《人工智能,一种现代的方法》的时候,发现了这个搜索算法。但书上讲的主要是理论,以下是该算法的总结和与ACM的结合训练。
狼啸风云
2019/11/03
1.2K0
NLP学习3-基于计数方法的改进
使用点互信息Pointwise Mutual Information,PMI;PMI值越高表示相关性越强
皮大大
2023/08/25
2600
NLP学习3-基于计数方法的改进
基于业务设计数据表的总结
抛去测试、架构来说,数据表设计是指定功能开发的一个起点,如果出现失误将会对未来开发以及运行都会有很大的影响。接下来我们聊聊应该如何根据需求去设计数据表。
CrazyCodes
2019/11/07
6560
基于业务设计数据表的总结
基于CRDT的一种协作冲突算法
当多个人同时编辑一个在线文档时,如何处理多人操作的冲突,一直是大家讨论的热点话题。解决协作冲突业界使用最多的两种思路是基于OT(Operation Transformation)的文档合并算法和基于CRDT的文档合并算法。其中OT算法我们之前已经详细介绍过(OT算法)就不再讨论了。本文我们主要介绍基于CRDT的一种文档合并算法-YATA。它有自己的开源实现Yjs(https://github.com/yjs/yjs)
一行舟
2022/08/25
2.6K0
基于CRDT的一种协作冲突算法
基于OpenCV与Dlib的行人计数开源实现
PyImageSearch昨天发布的行人计数的Blog,详述了使用OpenCV和Dlib库中的检测和跟踪算法如何完成该功能。原网址开源代码需要F-Q才能下载,我已经下载并上传到百度云,在“我爱计算机视觉”公众号后台回复counter,即可收到百度云下载地址。
CV君
2019/12/27
1.1K0
k8s中pod的状态包括_k8s pod状态
可以在根容器上设置Ip地址,其它容器都共享此ip,以实现Pod内部的网路通信,同时外部服务要访问容器也可以通过此ip
全栈程序员站长
2022/09/22
2.3K0
k8s中pod的状态包括_k8s pod状态
LeetCode 1178. 猜字谜(状态压缩+枚举二进制子集+哈希)
字谜的迷面 puzzle 按字符串形式给出,如果一个单词 word 符合下面两个条件,那么它就可以算作谜底:
Michael阿明
2021/09/06
2640

相似问题

字谜计数器

10

用Python优化8字谜的代码

10

给定字符串中文本的字谜计数

10

基于频率启发式的朝鲜语分词

30

给定输入的字谜

40
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文