细胞完整问题

问题描述 投票:-5回答:7

这是我的任务:

有一个由8个细胞排列成直线的集落,每天每个细胞与其相邻的细胞(邻居)竞争。每天,对于每个小区,如果其邻居都是活动的或者都是非活动的,则该小区在第二天变为不活动。否则它会在第二天活跃起来。

假设:两端的两个小区具有单个相邻小区,因此可以假设另一个相邻小区始终处于非活动状态。即使在更新了单元状态之后。考虑其更新其他单元状态的过去状态。同时更新所有单元的单元信息。

写一个函数cellCompete,它接受一个8元素的整数单元格数组,代表8个单元格的当前状态,一个整数天代表模拟的天数。整数值1表示活动单元格,值0表示非活动单元格。

程序:

int* cellCompete(int* cells,int days)
{
//write your code here
} 
//function signature ends

Test Case 1:
INPUT:
[1,0,0,0,0,1,0,0],1
EXPECTED RETURN VALUE:
[0,1,0,0,1,0,1,0]

Test Case 2:
INPUT:
[1,1,1,0,1,1,1,1,],2
EXPECTED RETURN VALUE:
[0,0,0,0,0,1,1,0]

这是上面针对该问题给出的问题陈述。我为这个问题编写的代码如下。但输出与输入相同。

#include<iostream>
using namespace std;

// signature function to solve the problem
int *cells(int *cells,int days)
{   int previous=0;
    for(int i=0;i<days;i++)
    {

        if(i==0)
        {
            if(cells[i+1]==0)
            {

            previous=cells[i];
            cells[i]=0;
        }

        else
        {

            cells[i]=0;
        }       


        if(i==days-1)
        {
            if(cells[days-2]==0)
            {
                previous=cells[days-1];
                cells[days-1]=0;
            }
        else
        {
            cells[days-1]=1;
        }
        }



        if(previous==cells[i+1])
        {
            previous=cells[i];
            cells[i]=0;
        }

        else
        {
            previous=cells[i];
            cells[i]=1;
        }
    }

            }
return cells;
}




int main()
{
    int array[]={1,0,0,0,0,1,0,0};
    int *result=cells(array,8);
    for(int i=0;i<8;i++)
    cout<<result[i];
}

我无法得到错误,我认为我的逻辑错了。我们可以在这里应用动态编程吗?如果我们可以如何?

arrays data-structures
7个回答
0
投票

您的程序不区分模拟天数和单元格数。


0
投票
#include <stdio.h>
int main() {

    int days,ind,arr[8],outer;
    for(ind=0;ind<8;scanf("%d ",&arr[ind]),ind++);    //Reading the array
    scanf("%d",&days);
    int dupArr[8];
    for(outer=0;outer<days;outer++){ //Number of days to simulate
        for(ind=0;ind<8;ind++){    //Traverse the whole array
        //cells on the ends have single adjacent cell, so the other adjacent cell can be assumsed to be always inactive
            if(ind==0){
                if(arr[ind+1]==0)
                    dupArr[ind]=0;
                else
                    dupArr[ind]=1;
            }
            else if(ind==7){
                if(arr[ind-1]==0)
                    dupArr[ind]=0;
                else
                    dupArr[ind]=1;
            }
            else{
                if((arr[ind-1]==0&&arr[ind+1]==0) || (arr[ind-1]==1&&arr[ind+1]==1)){// if its neighbours are both active or both inactive, the cell becomes inactive the next day
                    dupArr[ind]=0;
                }
                else  //otherwise it becomes active the next day
                    dupArr[ind]=1;
            }
        }
        for(ind=0;ind<8;ind++){
            arr[ind]=dupArr[ind]; //Copying the altered array to original array, so that we can alter it n number of times.
        }
    }
    for(ind=0;ind<8;ind++)
        printf("%d ",arr[ind]);//Displaying output
    return 0;
}

这是我几个月前创建的代码,

  • 您想要创建两个不同的数组,因为更改相同的数组元素将为您提供不同的结果。

0
投票
func competeCell(cell []uint, days uint) []uint{
    n := len(cell)
    temp := make([]uint, n)
    for i :=0; i < n; i ++ {
        temp[i] = cell[i]
    }

    for days > 0 {
        temp[0] = 0 ^ cell[1]
        temp[n-1] = 0 ^ cell[n-2]

        for i := 1; i < n-2 +1; i++ {
            temp[i] = cell[i-1] ^ cell[i +1]
        }
        for i:=0; i < n; i++ {
            cell[i] = temp[i]
        }
        days -= 1
    }
    return cell
}

0
投票
#include <bits/stdc++.h>
using namespace std;
int* cellCompete(int* cells,int days)
{
    for(int j=0; j<days; j++)
    {
        int copy_cells[10];
        for(int i=1; i<9; i++)
            copy_cells[i]=cells[i-1];

        copy_cells[0]=0;copy_cells[9]=0;
        for(int i=0; i<8; i++)
            cells[i]=copy_cells[i]==copy_cells[i+2]?0:1;
    }
    return cells;
}

int main()
{
    int arr[8]={1,1,1,0,1,1,1,1};
    int arr2[8]={1,0,0,0,0,1,0,0};
    cellCompete(arr2,1);
    for(int i=0; i<8; i++)
    {
        cout<<arr2[i]<<" ";
    }
}

0
投票
private List<Integer> finalStates = new ArrayList<>();

public static void main(String[] args) {
    // int arr[] = { 1, 0, 0, 0, 0, 1, 0, 0 };
    // int days = 1;
    EightHousePuzzle eightHousePuzzle = new EightHousePuzzle();
    int arr[] = { 1, 1, 1, 0, 1, 1, 1, 1 };
    int days = 2;
    eightHousePuzzle.cellCompete(arr, days);
}

public List<Integer> cellCompete(int[] states, int days) {
    List<Integer> currentCellStates = Arrays.stream(states).boxed().collect(Collectors.toList());
    return getCellStateAfterNDays(currentCellStates, days);
}

private List<Integer> getCellStateAfterNDays(List<Integer> currentCellStates, int days) {
    List<Integer> changedCellStates = new ArrayList<>();
    int stateUnoccupied = 0;
    if (days != 0) {
        for (int i1 = 0; i1 < currentCellStates.size(); i1++) {
            if (i1 == 0) {
                changedCellStates.add(calculateCellState(stateUnoccupied, currentCellStates.get(i1 + 1)));

            } else if (i1 == 7) {
                changedCellStates.add(calculateCellState(currentCellStates.get(i1 - 1), stateUnoccupied));

            } else {
                changedCellStates
                        .add(calculateCellState(currentCellStates.get(i1 - 1), currentCellStates.get(i1 + 1)));
            }
        }
        if (days == 1) {
            System.out.println("days ==1 hit");
            finalStates = new ArrayList<>(changedCellStates);
            return finalStates;
        }
        days = days - 1;
        System.out.println("Starting recurssion");
        getCellStateAfterNDays(changedCellStates, days);
    }
    return finalStates;
}

private int calculateCellState(int previousLeft, int previousRight) {
    if ((previousLeft == 0 && previousRight == 0) || (previousLeft == 1 && previousRight == 1)) {
        // the state gets now changed to 0
        return 0;
    }
        // the state gets now changed to 0
    return 1;
}

0
投票

这是一些甜蜜的小python代码:

def cell(arr, days):
    new = arr[:] #get a copy of the array
    n = len(arr)

    if n == 1: print [0] #when only 1 node, return [0]

    for _ in range(days):
        new[0] = arr[1] #determine the edge nodes first
        new[n - 1] = arr[n - 2]

        for i in range(1, n-1):
            new[i] = 1 - (arr[i-1] == arr[i+1]) #logic for the rest nodes
        arr = new[:] #update the list for the next day

    return new

arr = [1, 1, 1, 0, 1, 1, 1, 1]
days = 2
print cell(arr, days)

-1
投票
public class Colony {
    public static int[] cellCompete(int[] cell, int day) {
        int[] ar = new int[10];
        for(int i=1; i<9; i++) {
            ar[i] = cell[i-1];
        }
        while(day-- >0) {
            int temp = 0;
            for(int i=1; i<9; i++) {
                if(Math.abs(temp-ar[i+1])==1) {
                    temp = ar[i];
                    ar[i] = 1;
                }
                else {
                    temp = ar[i];
                    ar[i] = 0;
                }
            }
        }
        return ar;
    }

    public static void main(String[] args) {

        int[] cell = {1,0,1,1,0,1,0,1};
        int day = 1;
        cell = cellCompete(cell, day);
        for(int i=1; i<9; i++) {
            System.out.print(cell[i]+" ");
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.