gpt4 book ai didi

java - 在最后两行不相同的每一行上写0's and 1'

转载 作者:搜寻专家 更新时间:2023-10-31 19:33:19 25 4
gpt4 key购买 nike

我目前构建的逻辑存在错误。
应该发生的是,我的代码应显示0和1的网格。
像这样:

001001
101101
010110
110010
001101

因此,这里必须发生的是:
  • 对于每一行,连续不超过2个相同类型的数字
  • 随机抽取数字
  • 每一列的
  • 连续不能超过2个相同类型的数字
  • 每种类型的数字中最多可以有3个按列或行显示

  • 编辑:进一步澄清
    好的,所以我有这样的一行:
    0 1 0 1 1 0
    -如您所见,总会有3 x 1和3 x 0
    -数字的顺序是随机选择的(因此它可能会从0 1或1 1或0 0开始,依此类推)
    -连续不能有超过2个相同类型的数字,例如,如果是001100,您会看到有2个0,那么它必须显示1,但随后有2个1,因此必须显示0。因此无法发生011100(连续3个1)或000101(连续3 0个)
  • 基于此,但现在不是必须的,必须在列中连续应用相同的no 2数字(因此,在我成功的示例中,它跨过001001,最多连续存在20。但是向下看,您会得到010101(即,再说一次,连续不超过2个)

  • 所以我的代码如下:
        import java.util.Random;

    public class Main {

    public static void main(String[] args) {
    int l = 6;
    int w = 6;
    Random rd = new Random();

    // Create a grid that is 6 x 6
    int[][] grid = new int[l][w];
    // for each row
    for (int i = 0; i < l; i++) {
    int zCount = 0;
    int oCount = 0;
    int current;
    int lastA = 2;
    int lastB = 2;
    // for each item in the row
    for (int j = 0; j < w; j++) {
    // set the current item to either 0 or 1
    current = rd.nextInt(2);
    // make sure there aren't already (e.g. 3 items out of 6)
    // items in the row
    if (j % 2 == 1) {
    // hold every second element
    lastA = current;
    } else {
    // hold every first element
    lastB = current;
    }
    if (current == 1) {
    if (oCount != 3) {
    if (lastA != lastB) {
    // if the two previous items aren't the same
    grid[i][j] = current;
    // add to the counter
    oCount++;
    }
    }
    }
    if (current == 0) {
    if (zCount != 3) {
    if (lastA != lastB) {
    // if the two previous items aren't the same
    grid[i][j] = current;
    // add to the counter
    zCount++;
    }
    }
    }
    System.out.print(grid[i][j]);
    }
    System.out.println(" ");
    }
    }
    }

    问题在于它生成如下:
    010010 
    100001
    100010
    000010
    100001
    001000

    因此很明显,它不符合第一,第三或第四点。
    我完全不知道为什么!除了我尚未初始化的列(第三点)。

    谁能弄清楚我的代码中的逻辑故障是什么?

    谢谢你的帮助!

    最佳答案

    给出的许多解决方案都非常长且复杂。这是一个使用极少代码(Ideone Example here)的解决方案:

    int row, col, n = 8;
    int[][] grid = new int[n][n], cCount = new int[n][2], rCount = new int[n][2];
    Deque<Entry<Integer,Integer>> freeInd = new ArrayDeque<Entry<Integer,Integer>>();
    Random rand=new Random();
    for(int i = 0; i < grid.length; i++){
    for(int j = 0; j < grid[0].length; j++){
    // Calcualte constraints: row, col = {-1, 0, 1}, -1 => no constraint.
    row = j > 1 && grid[i][j-2] == grid[i][j-1] ? (grid[i][j-1] == 0 ? 1:0):
    (rCount[i][0] >= n/2 ? 1: // too many 0's
    (rCount[i][1] >= n/2 ? 0:-1)); // too many 1's
    col = i > 1 && grid[i-2][j] == grid[i-1][j] ? (grid[i-1][j] == 0 ? 1:0):
    (cCount[j][0] >= n/2 ? 1: // too many 0's
    (cCount[j][1] >= n/2 ? 0:-1)); // too many 1's
    grid[i][j] = row == -1 && col == -1 ? rand.nextInt(2):(row > -1 ? row:col);

    // Handle Constraints
    if( row == -1 && col == -1){ // no constraint
    freeInd.push(new SimpleEntry<Integer,Integer>(i, j)); // add to free indices
    } else if( (row > -1 && col > -1 && row != col) // constraint conflict
    || (row > -1 && rCount[i][row] >= n/2) // count conflict
    || (col > -1 && cCount[j][col] >= n/2)){ // count conflict
    Entry<Integer, Integer> last = freeInd.pop(); // grab last free index
    while(i > last.getKey() || j > last.getValue()){
    j = (j-1+ n)%n; // step indices back
    i = (j == n-1) ? i-1:i;
    rCount[i][grid[i][j]]--; // reduce counters
    cCount[j][grid[i][j]]--;
    }
    grid[i][j] = grid[i][j] == 0 ? 1:0; // flip value
    }
    rCount[i][grid[i][j]]++; // increment counters
    cCount[j][grid[i][j]]++;
    }
    }

    这里的想法是,您遵循以下规则沿着矩阵的每一行相加0和1:
  • 如果当前索引不受限制(即可以是0或1),我们将随机选择一个值。
  • 如果当前索引受约束,则我们强制它具有约束值。
  • 如果存在多个不同的约束条件,我们将首先沿矩阵的行逐步向后退一步,以减少给定值(0或1)的计数,从而返回到最后一个不受约束的索引(freeInd)。例如。这是针对带有rCount[i][grid[i][j]]--的行完成的。最终达到无约束顶点时,翻转其值。
  • 最后,为当前行和列增加值的计数(0或1)。例如。这是针对带有rCount[i][grid[i][j]]++
  • 的行完成的

    关于java - 在最后两行不相同的每一行上写0's and 1',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23329611/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com