原文:

给定二维矩阵和整数“k”,任务是在“k”次迭代后预测矩阵,如下所示:

  • element 1 in the current matrix will remain 1 in the next iteration only if it is surrounded by multiple 1s, where 0 < = range 1a < = a < = range 1b.
  • the element 0 in the current matrix will only become 1 in the next iteration if it is surrounded by the number of b of 1, where 0<= range 0a<= b<= range 0b.

我们用一个例子来理解一下:

约束: 1<= k<= 100000 0<=范围 1a、范围 1b、范围 0a、范围 0b < = 8

  • 在上述单元格(0,0)的图像中,单元格在第一次迭代中为“0”,但由于它仅被一个包含“1”的相邻单元格包围,因此不在范围[range0a,range0b]内。因此它将继续保持“0”。
  • 对于第二次迭代,单元格(0,0)为 0,但这次它被两个包含“1”的单元格包围,两个单元格位于范围[range0a,range0b]内。因此,它在下一次(第二次)迭代中变为“1”。

示例:

输入:范围 1a = 2 范围 1b = 2 范围 0a = 2 范围 0b = 3 k = 1 输出: 0 1 1 0 0 1 1 1 0 0 1 0 0 1 0 输入:范围 1a = 2 范围 1b = 2 范围 0a = 2 范围 0b = 3

下面是上述方法的实现:

c

// c   implementation of the approach
#include 
using namespace std;
// dimension of array
#define n 4
void predictmatrix(int arr[n][n],
                   int range1a,
                   int range1b,
                   int range0a,
                   int range0b,
                   int k,
                   int b[n][n])
{
    // count of 1s
    int c = 0;
    while (k--) {
        for (int i = 0; i < n; i  ) {
            for (int j = 0; j < n; j  ) {
                c = 0;
                // counting all neighbouring 1s
                if (i > 0 && arr[i - 1][j] == 1)
                    c  ;
                if (j > 0 && arr[i][j - 1] == 1)
                    c  ;
                if (i > 0 && j > 0
                    && arr[i - 1][j - 1] == 1)
                    c  ;
                if (i < n - 1 && arr[i   1][j] == 1)
                    c  ;
                if (j < n - 1 && arr[i][j   1] == 1)
                    c  ;
                if (i < n - 1 && j < n - 1
                    && arr[i   1][j   1] == 1)
                    c  ;
                if (i < n - 1 && j > 0
                    && arr[i   1][j - 1] == 1)
                    c  ;
                if (i > 0 && j < n - 1
                    && arr[i - 1][j   1] == 1)
                    c  ;
                // comparing the number of
                // neighbouring 1s with
                // given ranges
                if (arr[i][j] == 1) {
                    if (c >= range1a && c <= range1b)
                        b[i][j] = 1;
                    else
                        b[i][j] = 0;
                }
                if (arr[i][j] == 0) {
                    if (c >= range0a && c <= range0b)
                        b[i][j] = 1;
                    else
                        b[i][j] = 0;
                }
            }
        }
        // copying changes to
        // the main matrix
        for (int k = 0; k < n; k  )
            for (int m = 0; m < n; m  )
                arr[k][m] = b[k][m];
    }
}
// driver code
int main()
{
    int arr[n][n] = { 0, 0, 0, 0,
                      0, 1, 1, 0,
                      0, 0, 1, 0,
                      0, 1, 0, 1 };
    int range1a = 2, range1b = 2;
    int range0a = 2, range0b = 3;
    int k = 3, b[n][n] = { 0 };
    // function call to calculate
    // the resultant matrix
    // after 'k' iterations.
    predictmatrix(arr, range1a, range1b,
                  range0a, range0b, k, b);
    // printing result
    for (int i = 0; i < n; i  ) {
        cout << endl;
        for (int j = 0; j < n; j  )
            cout << b[i][j] << " ";
    }
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of the approach
public class gfg{
// dimension of array
final static int n  = 4 ;
static void predictmatrix(int arr[][],
                   int range1a,
                   int range1b,
                   int range0a,
                   int range0b,
                   int k,
                   int b[][])
{
    // count of 1s
    int c = 0;
    while (k != 0) {
        k--;
        for (int i = 0; i < n; i  ) {
            for (int j = 0; j < n; j  ) {
                c = 0;
                // counting all neighbouring 1s
                if (i > 0 && arr[i - 1][j] == 1)
                    c  ;
                if (j > 0 && arr[i][j - 1] == 1)
                    c  ;
                if (i > 0 && j > 0
                    && arr[i - 1][j - 1] == 1)
                    c  ;
                if (i < n - 1 && arr[i   1][j] == 1)
                    c  ;
                if (j < n - 1 && arr[i][j   1] == 1)
                    c  ;
                if (i < n - 1 && j < n - 1
                    && arr[i   1][j   1] == 1)
                    c  ;
                if (i < n - 1 && j > 0
                    && arr[i   1][j - 1] == 1)
                    c  ;
                if (i > 0 && j < n - 1
                    && arr[i - 1][j   1] == 1)
                    c  ;
                // comparing the number of
                // neighbouring 1s with
                // given ranges
                if (arr[i][j] == 1) {
                    if (c >= range1a && c <= range1b)
                        b[i][j] = 1;
                    else
                        b[i][j] = 0;
                }
                if (arr[i][j] == 0) {
                    if (c >= range0a && c <= range0b)
                        b[i][j] = 1;
                    else
                        b[i][j] = 0;
                }
            }
        }
        // copying changes to
        // the main matrix
        for (int k = 0; k < n; k  )
            for (int m = 0; m < n; m  )
                arr[k][m] = b[k][m];
    }
}
// driver code
public static void main(string []args)
{
    int arr[][] = { {0, 0, 0, 0},
                      {0, 1, 1, 0},
                      {0, 0, 1, 0},
                      {0, 1, 0, 1 } };
    int range1a = 2, range1b = 2;
    int range0a = 2, range0b = 3;
    int k = 3;
    int b[][] = new int[n][n] ;
    // function call to calculate
    // the resultant matrix
    // after 'k' iterations.
    predictmatrix(arr, range1a, range1b,
                  range0a, range0b, k, b);
    // printing result
    for (int i = 0; i < n; i  ) {
        system.out.println();
        for (int j = 0; j < n; j  )
            system.out.print(b[i][j]  " ");
    }
}
// this code is contributed by ryuga
}

python 3

# python3 implementation of the approach
# dimension of array
n = 4
def predictmatrix(arr, range1a, range1b,
                  range0a, range0b, k, b):
    # count of 1s
    c = 0
    while (k):
        for i in range(n) :
            for j in range(n):
                c = 0
                # counting all neighbouring 1s
                if (i > 0 and arr[i - 1][j] == 1):
                    c  = 1
                if (j > 0 and arr[i][j - 1] == 1):
                    c  = 1
                if (i > 0 and j > 0 and
                    arr[i - 1][j - 1] == 1):
                    c  = 1
                if (i < n - 1 and arr[i   1][j] == 1):
                    c  = 1
                if (j < n - 1 and arr[i][j   1] == 1):
                    c  = 1
                if (i < n - 1 and j < n - 1
                    and arr[i   1][j   1] == 1):
                    c  = 1
                if (i < n - 1 and j > 0
                    and arr[i   1][j - 1] == 1):
                    c  = 1
                if (i > 0 and j < n - 1
                    and arr[i - 1][j   1] == 1):
                    c  = 1
                # comparing the number of neighbouring
                # 1s with given ranges
                if (arr[i][j] == 1) :
                    if (c >= range1a and c <= range1b):
                        b[i][j] = 1
                    else:
                        b[i][j] = 0
                if (arr[i][j] == 0):
                    if (c >= range0a and c <= range0b):
                        b[i][j] = 1
                    else:
                        b[i][j] = 0
        k -= 1
        # copying changes to the main matrix
        for k in range(n):
            for m in range( n):
                arr[k][m] = b[k][m]
# driver code
if __name__ == "__main__":
    arr = [[0, 0, 0, 0],
           [0, 1, 1, 0],
           [0, 0, 1, 0],
           [0, 1, 0, 1]]
    range1a = 2
    range1b = 2
    range0a = 2
    range0b = 3
    k = 3
    b = [[0 for x in range(n)]
            for y in range(n)]
    # function call to calculate
    # the resultant matrix
    # after 'k' iterations.
    predictmatrix(arr, range1a, range1b,
                  range0a, range0b, k, b)
    # printing result
    for i in range( n):
        print()
        for j in range(n):
            print(b[i][j], end = " ")
# this code is contributed
# by chitranayal

c

// c# implementation of the approach
using system;
class gfg
{
// dimension of array
readonly static int n = 4 ;
static void predictmatrix(int [,]arr, int range1a,
                          int range1b, int range0a,
                          int range0b, int k, int [,]b)
{
    // count of 1s
    int c = 0;
    while (k != 0)
    {
        k--;
        for (int i = 0; i < n; i  )
        {
            for (int j = 0; j < n; j  )
            {
                c = 0;
                // counting all neighbouring 1s
                if (i > 0 && arr[i - 1, j] == 1)
                    c  ;
                if (j > 0 && arr[i, j - 1] == 1)
                    c  ;
                if (i > 0 && j > 0
                    && arr[i - 1, j - 1] == 1)
                    c  ;
                if (i < n - 1 && arr[i   1, j] == 1)
                    c  ;
                if (j < n - 1 && arr[i, j   1] == 1)
                    c  ;
                if (i < n - 1 && j < n - 1 &&
                    arr[i   1, j   1] == 1)
                    c  ;
                if (i < n - 1 && j > 0 &&
                    arr[i   1, j - 1] == 1)
                    c  ;
                if (i > 0 && j < n - 1 &&
                    arr[i - 1, j   1] == 1)
                    c  ;
                // comparing the number of
                // neighbouring 1s with
                // given ranges
                if (arr[i,j] == 1)
                {
                    if (c >= range1a && c <= range1b)
                        b[i, j] = 1;
                    else
                        b[i, j] = 0;
                }
                if (arr[i,j] == 0)
                {
                    if (c >= range0a && c <= range0b)
                        b[i, j] = 1;
                    else
                        b[i, j] = 0;
                }
            }
        }
        // copying changes to the main matrix
        for (int k = 0; k < n; k  )
            for (int m = 0; m < n; m  )
                arr[k, m] = b[k, m];
    }
}
// driver code
public static void main()
{
    int [,]arr = { {0, 0, 0, 0},
                   {0, 1, 1, 0},
                   {0, 0, 1, 0},
                   {0, 1, 0, 1 } };
    int range1a = 2, range1b = 2;
    int range0a = 2, range0b = 3;
    int k = 3;
    int [,]b = new int[n, n];
    // function call to calculate
    // the resultant matrix
    // after 'k' iterations.
    predictmatrix(arr, range1a, range1b,
                range0a, range0b, k, b);
    // printing result
    for (int i = 0; i < n; i  )
    {
        console.writeline();
        for (int j = 0; j < n; j  )
            console.write(b[i, j]   " ");
    }
}
}
// this code is contributed by 29ajaykumar

服务器端编程语言(professional hypertext preprocessor 的缩写)

 0 && $arr[$i - 1][$j] == 1)
                    $c  ;
                if ($j > 0 && $arr[$i][$j - 1] == 1)
                    $c  ;
                if ($i > 0 && $j > 0 && $arr[$i - 1][$j - 1] == 1)
                    $c  ;
                if ($i < $n - 1 && $arr[$i   1][$j] == 1)
                    $c  ;
                if ($j < $n - 1 && $arr[$i][$j   1] == 1)
                    $c  ;
                if ($i < $n - 1 && $j < $n - 1 &&
                    $arr[$i   1][$j   1] == 1)
                    $c  ;
                if ($i < $n - 1 && $j > 0 && $arr[$i   1][$j - 1] == 1)
                    $c  ;
                if ($i > 0 && $j < $n - 1 && $arr[$i - 1][$j   1] == 1)
                    $c  ;
                // comparing the number of
                // neighbouring 1s with
                // given ranges
                if ($arr[$i][$j] == 1)
                {
                    if ($c >= $range1a && $c <= $range1b)
                        $b[$i][$j] = 1;
                    else
                        $b[$i][$j] = 0;
                }
                if ($arr[$i][$j] == 0) {
                    if ($c >= $range0a && $c <= $range0b)
                        $b[$i][$j] = 1;
                    else
                        $b[$i][$j] = 0;
                }
            }
        }
        // copying changes to
        // the main matrix
        for ($k = 0; $k < $n; $k  )
            for ($m = 0; $m < $n; $m  )
                $arr[$k][$m] = $b[$k][$m];
    }
    return $b;
}
// driver code
$n = 4;
$arr= array(array(0, 0, 0, 0),
        array(0, 1, 1, 0),
        array(0, 0, 1, 0),
        array(0, 1, 0, 1));
$range1a = 2; $range1b = 2;
$range0a = 2; $range0b = 3;
$k = 3; $b = array(array(0));
// function call to calculate
// the resultant matrix
// after 'k' iterations.
$b1 = predictmatrix($arr, $range1a, $range1b,
            $range0a, $range0b, $k, $b);
// printing result
for ($i = 0; $i < $n; $i  )
{
    echo "\n";
    for ($j = 0; $j < $n; $j  )
        echo $b1[$i][$j] . " ";
}
// this code is contributed by akanksha rai

java 描述语言


output: 

0 1 0 0 
0 1 0 0 
1 1 1 0 
0 0 1 0