📜  k天后的活动和非活动细胞

📅  最后修改于: 2022-05-13 01:57:50.016000             🧑  作者: Mango

k天后的活动和非活动细胞

给定一个大小为 n 的二进制数组,其中n > 3 。数组中的真(或 1)值表示活动,假(或 0)表示不活动。给定一个数字 k,任务是找出 k 天后活动和非活动单元的数量。每天之后,如果左右单元格不相同,则第 i 个单元格的状态变为活动状态;如果左右单元格相同(均为 0 或均为 1),则第 i 个单元格的状态变为不活动状态。
由于在最左侧单元格之前和最右侧单元格之后没有单元格,因此最左侧单元格之前和最右侧单元格之后的值单元格始终被视为 0(或非活动)。

例子:

Input  : cells[] = {1, 0, 1, 1}, k = 2
Output : Active cells = 3, Inactive cells = 1
After 1 day,  cells[] = {0, 0, 1, 1}
After 2 days, cells[] = {0, 1, 1, 1}

Input : cells[] = {0, 1, 0, 1, 0, 1, 0, 1},  k = 3
Output: Active Cells = 2 , Inactive Cells = 6
Explanation : 
After 1 day, cells[] = {1, 0, 0, 0, 0, 0, 0, 0}
After 2 days, cells[] = {0, 1, 0, 0, 0, 0, 0, 0}
After 3 days, cells[] =  {1, 0, 1, 0, 0, 0, 0, 0}

Input : cells[] = {0, 1, 1, 1, 0, 1, 1, 0},  k = 4
Output: Active Cells = 3 , Inactive Cells = 5

唯一重要的是确保我们维护给定数组的副本,因为我们需要在第二天更新以前的值。下面是详细步骤。

  1. 首先,我们将 cells[] 数组复制到 temp[] 数组中,并根据给定条件对 temp[] 数组进行更改。
  2. 在该条件下,如果第 i 个单元格的直接左右单元格不活跃或第二天活跃,则 i 变为不活跃,即; (cells[i-1] == 0 and cells[i+1] == 0) 或者 (cells[i-1] == 1 and cells[i+1] == 1) then cells[i] = 0 ,这些条件可以使用单元格[i-1] 和单元格[i+1] 的异或来应用。
  3. 对于第 0 个索引单元 temp[0] = 0^cells[1] 和第 (n-1)'th 个索引单元 temp[n-1] = 0^cells[n-2]。
  4. 现在对于索引 1 到 n-2,执行以下操作 temp[i] = cells[i-1] ^ cells[i+1]
  5. 重复这个过程,直到 k 天结束。

以下是上述步骤的实现。

C++
// C++ program to count active and inactive cells after k
// days
#include
using namespace std;
 
// cells[] - store current status of cells
// n - Number of cells
// temp[] - to perform intermediate operations
// k - number of days
// active - count of active cells after k days
// inactive - count of active cells after k days
void activeAndInactive(bool cells[], int n, int k)
{
    // copy cells[] array into temp [] array
    bool temp[n];
    for (int i=0; i


Java
// Java program to count active and
// inactive cells after k days
 
class GFG {
     
// cells[] - store current status
// of cells n - Number of cells
// temp[] - to perform intermediate operations
// k - number of days
// active - count of active cells after k days
// inactive - count of active cells after k days
static void activeAndInactive(boolean cells[],
                                 int n, int k)
{
    // copy cells[] array into temp [] array
    boolean temp[] = new boolean[n];
    for (int i = 0; i < n; i++)
    temp[i] = cells[i];
 
    // Iterate for k days
    while (k-- > 0) {
         
    // Finding next values for corner cells
    temp[0] = false ^ cells[1];
    temp[n - 1] = false ^ cells[n - 2];
 
    // Compute values of intermediate cells
    // If both cells active or inactive, then
    // temp[i]=0 else temp[i] = 1.
    for (int i = 1; i <= n - 2; i++)
        temp[i] = cells[i - 1] ^ cells[i + 1];
 
    // Copy temp[] to cells[] for next iteration
    for (int i = 0; i < n; i++)
        cells[i] = temp[i];
    }
 
    // count active and inactive cells
    int active = 0, inactive = 0;
    for (int i = 0; i < n; i++)
    if (cells[i] == true)
        active++;
    else
        inactive++;
 
    System.out.print("Active Cells = " + active + ", " +
                     "Inactive Cells = " + inactive);
}
 
// Driver code
public static void main(String[] args)
{
    boolean cells[] = {false, true, false, true,
                       false, true, false, true};
    int k = 3;
    int n = cells.length;
    activeAndInactive(cells, n, k);
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to count
# active and inactive cells after k
# days
 
# cells[] - store current
# status of cells
# n - Number of cells
# temp[] - to perform
# intermediate operations
# k - number of days
# active - count of active
# cells after k days
# inactive - count of active
# cells after k days
def activeAndInactive(cells,n,k):
     
    # copy cells[] array into temp [] array
    temp=[]
    for i in range(n+1):
        temp.append(False)
    for i in range(n):
        temp[i] = cells[i]
  
    # Iterate for k days
    while (k >0):
     
        # Finding next values for corner cells
        temp[0]   = False^cells[1]
        temp[n-1] = False^cells[n-2]
  
        # Compute values of intermediate cells
        # If both cells active or
        # inactive, then temp[i]=0
        # else temp[i] = 1.
        for i in range(1,n-2+1):
            temp[i] = cells[i-1] ^ cells[i+1]
  
        # Copy temp[] to cells[]
        # for next iteration
        for i in range(n):
            cells[i] = temp[i]
        k-=1
     
    # count active and inactive cells
    active = 0
    inactive = 0;
    for i in range(n):
        if(cells[i] == True):
            active+=1
        else:
            inactive+=1
  
    print("Active Cells =",active," , "
          , "Inactive Cells =",
           inactive)
 
# Driver code
 
cells = [False, True, False, True,
         False, True, False, True]
k = 3
n =len(cells)
 
activeAndInactive(cells, n, k)
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to count active and
// inactive cells after k days
using System;
 
class GFG {
     
// cells[] - store current status
// of cells n - Number of cells
// temp[] - to perform intermediate
// operations k - number of days
// active - count of active cells
// after k days inactive - count
// of active cells after k days
static void activeAndInactive(bool []cells,
                              int n, int k)
{
     
    // copy cells[] array into
    // temp [] array
    bool []temp = new bool[n];
    for (int i = 0; i < n; i++)
    temp[i] = cells[i];
 
    // Iterate for k days
    while (k-- > 0) {
         
    // Finding next values
    // for corner cells
    temp[0] = false ^ cells[1];
    temp[n - 1] = false ^ cells[n - 2];
 
    // Compute values of intermediate cells
    // If both cells active or inactive, then
    // temp[i]=0 else temp[i] = 1.
    for (int i = 1; i <= n - 2; i++)
        temp[i] = cells[i - 1] ^ cells[i + 1];
 
    // Copy temp[] to cells[]
    // for next iteration
    for (int i = 0; i < n; i++)
        cells[i] = temp[i];
    }
 
    // count active and inactive cells
    int active = 0, inactive = 0;
    for (int i = 0; i < n; i++)
    if (cells[i] == true)
        active++;
    else
        inactive++;
 
    Console.Write("Active Cells = " + active + ", " +
                  "Inactive Cells = " + inactive);
}
 
// Driver code
public static void Main()
{
    bool []cells = {false, true, false, true,
                    false, true, false, true};
    int k = 3;
    int n = cells.Length;
    activeAndInactive(cells, n, k);
}
}
 
// This code is contributed by Nitin Mittal.


PHP


Javascript


输出:

Active Cells = 2 , Inactive Cells = 6