细胞竞争问题 [英] Cell Compete Problems

查看:73
本文介绍了细胞竞争问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的任务:


有8个细胞的菌落以直线排列,每个细胞每天都与其竞争相邻单元格(邻居)。每天,对于每个单元格,如果其邻居都处于活动状态或都处于非活动状态,则该单元格在第二天变为非活动状态。

There is a colony of 8 cells arranged in a straight line where each day every cell competes with its adjacent cells(neighbour). Each day, for each cell, if its neighbours are both active or both inactive, the cell becomes inactive the next day,. otherwise itbecomes active the next day.

假设:两端的两个单元具有单个相邻单元,因此可以将另一个相邻单元中的
假定为始终不活跃。更新单元格状态后甚至
。考虑
的先前状态,以更新其他单元格的状态。同时更新
个所有单元格的单元格信息。

Assumptions: The two cells on the ends have single adjacent cell, so the other adjacent cell can be assumsed to be always inactive. Even after updating the cell state. consider its pervious state for updating the state of other cells. Update the cell informationof allcells simultaneously.

编写一个函数cellCompete,它需要一个8个元素的
整数单元格数组,这些数组代表当前状态8单元格和一个
整数天,代表要模拟的天数。整数
的值为1表示活动单元格,值为0表示
的非活动单元格。

Write a fuction cellCompete which takes takes one 8 element array of integers cells representing the current state of 8 cells and one integer days representing te number of days to simulate. An integer value of 1 represents an active cell and value of 0 represents an inactive cell.

程序:

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]


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

This is the problem statement given above for the problem. The code which I have written for this problem is given below. But the output is coming same as the input.

#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];
}

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

I am not able to get the error and I think my logic is wrong. Can we apply dynamic programming here If we can then how?

推荐答案

这是可能答案的C#版本。我真的出于某种原因为此苦了一段时间!
我还在上面结合了Janardan的一些东西,因为它有助于激励我朝着正确的方向前进。 (欢呼!)

This is a C# version of a possible answer. I really struggled with this for a while for some reason! I also incorporated some of Janardan's stuff above as it helped spur me in the right direction. (cheers!)

问题的棘手部分是要处理这样一个事实,即您必须保持单元的状态才能确定下一次单元竞赛最初尝试使用第二个混乱的数组。

The tricky part of the question was dealing with the fact that you had to persist the state of the cell to figure out the next cell competition which I had originally tried with a second array which was messy.

注意:我选择使用Array.Copy方法,因为我相信它比复制数组的效率更高,可读性更高。阅读时为for循环。

Note: I chose to use the Array.Copy method as I believe it is slightly more efficient and a lot more readable than copying arrays with a for loop when reading through.

希望这对以后的工作有所帮助!

Hopefully this helps someone out in the future!

   public int[] cellCompete(int[] cell, int day)
    {
        //First create an array with an extra 2 cells (these represent the empty cells on either end)
        int[] inputArray = new int[cell.Length + 2];

        //Copy the cell array into the new input array leaving the value of the first and last indexes as zero (empty cells)
        Array.Copy(cell, 0, inputArray, 1, cell.Length);    

        //This is cool I stole this from the guy above! (cheers mate), this decrements the day count while checking that we are still above zero.
        while (day-- > 0) 
        {
            int oldCellValue = 0;

            //In this section we loop through the array starting at the first real cell and going to the last real cell
            //(we are not including the empty cells at the ends which are always inactive/0)

            for (int i = 1; i < inputArray.Length - 1; i++)
            {
                //if the cells below and above our current index are the same == then the target cell will be inactive/0
                //otherwise if they are different then the target cell will be set to active/1
                //NOTE: before we change the index value to active/inactive state we are saving the cells oldvalue to a variable so that
                //we can use that to do the next "cell competition" comparison (this fulfills the requirement to update the values at the same time)

                if (oldCellValue == inputArray[i + 1])
                {
                    oldCellValue = inputArray[i];
                    inputArray[i] = 0;
                }
                else
                {
                    oldCellValue = inputArray[i];
                    inputArray[i] = 1;
                }
            }
        }

        //Finally we create a new output array that doesn't include the empty cells on each end
        //copy the input array to the output array and Bob's yer uncle ;)...(comments are lies)

        int[] outputArray = new int[cell.Length];
        Array.Copy(inputArray, 1, outputArray, 0, outputArray.Length);
        return outputArray;
    }

这篇关于细胞竞争问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆