传染程序、矩阵和语法问题

Contagion program, Matrices and Syntax issue

我正在尝试执行以下程序: 写一个函数onstepcontagion : bool array array -> bool array array = 给定一个矩形bool矩阵,其中true表示感染方块,false表示未感染方块,计算下一步感染。受感染的方块会无限期地保持感染状态,如果未受感染的方块 vertically/horizontally 与至少两个其他受感染的方块

相邻,则它们会被感染

到目前为止我的代码:


    let printmat matrix =
      let n = Array.length matrix in
      let n1 = Array.length matrix.(0) in
      for i = 0 to n - 1 do 
        for j = 0 to n1 - 1 do
          if matrix.(i).(j) == true then print_string"1"
          else print_string"0";
        done; 
        print_string "\n";
      done;;
      
      let onstepcontagion matrix =
      let n = Array.length matrix in
      let n1 = Array.length matrix.(0) in
      for i = 0 to n - 1 do 
        for j = 0 to n1 - 1 do
            if (j < n1-1) then
                let right = if matrix.(i).(j+1) == true then 1 else 0 in
            if (j > 0) then
                let left = if matrix.(i).(j-1) == true then 1 else 0 in
            if (i < n-1) then
                let up = if matrix.(i-1).(j) == true then 1 else 0 in
            if (i > 0) then
                let down = if matrix.(i+1).(j) == true then 1 else 0 in
            let sum = right + left + up + down in
            if sum > 1 then matrix.(i).(j) = true 
        done; 
        print_string "\n";
      done;;
      
      printmat matrix

**错误:错误:此表达式的类型为 bool 但表达式应为类型 单元 字符 1618-1639: 如果总和 > 1,则矩阵。(i).(j) = true **

我已经把我要实现的程序做了C版:


    #include <stdio.h>
    #include <stdlib.h>
    
    void print_mat (int n, int m, int arr[n][m])
    {
        int i,j;
        for (i = 0; i < n; i++){
            for (j = 0; j < m; j++)
                printf("%d ",arr[i][j]);
            printf("\n");
        }
        printf("\n\n");
    }
    
    
    int main()
    {
        int n, m, i, j, tmp, changes = 1;
        printf("input Mat len\n");
        scanf("%d%d",&n,&m);
        int arr[n][m];
        int cpy[n][m];
        for (i = 0; i < n; i++)
            for (j = 0; j < m; j++){
            scanf("%d",&tmp);
            if (tmp == 0)
                arr[i][j] = tmp;
            else
                arr[i][j] = 1;
            }
        while (changes){
            changes = 0;
        for (i = 0; i < n; i++){
            for (j = 0; j < m; j++)
            {
                tmp = 0;
                if (arr[i][j] != 1){
                    if (j < m-1)
                        if (arr[i][j+1] == 1)
                            tmp++;
                    if (j > 0)
                        if (arr[i][j-1] == 1)
                            tmp++;
                    if (i < n-1)
                        if (arr[i+1][j] == 1)
                            tmp++;
                    if (i > 0)
                        if (arr[i-1][j] == 1)
                            tmp++;
                    if (tmp > 1){
                        cpy[i][j] = 1;
                        changes = 1;
                    }
                }
            }
            }
            if (changes == 1){
             for (i = 0; i < n; i++)
                for (j = 0; j < m; j++)
                if (arr[i][j] == 1 || cpy[i][j] == 1)
                    arr[i][j] = 1;
               printf("\n");
               print_mat(n,m, arr);
            }
        }
        return 0;
    }

= 是结构相等运算符。要设置数组元素,您需要使用 <-.

此外,您几乎不应该使用 ==,因为它测试物理相等性,即引用指向内存中的相同地址。对于比较 bool 来说,这并不重要,因为它们不是指针,但您应该养成使用 = 的习惯,以避免将来出现意外。