## 457 - Linear Cellular Automata

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

amishera
New poster
Posts: 38
Joined: Sat Dec 27, 2008 10:42 pm

### 457 wa

This code:

1 #include <stdio.h>
2
3 int dna[10];
4
5 void print_dishes(int dishes[])
6 {
7 for (int i = 0;i < 40;i++)
8 {
9 switch (dishes)
10 {
11 case 1: printf(".");break;
12 case 2: printf("x");break;
13 case 3: printf("W");break;
14 case 0: printf("b");break;
15 // default: printf("%d",dishes);break;
16 }
17 }
18 printf("\n");
19 }
20
21 void calculate_and_print_dishes(int dishes[])
22 {
23 int prev = 0;
24
25 for (int i = 0;i < 40;i++)
26 {
27 int t = dishes;
28 dishes = dna[dishes+prev+dishes[i+1]];
29 prev = t;
30 }
31 print_dishes(dishes);
32 }
33
34 int main()
35 {
36 int c;
37 int dishes[41];
38 scanf("%d", &c);
39 for (int k = 0;k < c;k++)
40 {
41 for (int j = 0;j < 10;j++)
42 {
43 scanf("%d", &dna[j]);
44 }
45 for (int i = 0;i < 41;i++)
46 {
47 dishes = 0;
48 }
49 dishes[19] = 1;
50 print_dishes(dishes);
51 for (int j = 0;j < 49;j++)
52 {
53 calculate_and_print_dishes(dishes);
54 }
55 printf("\n");
56 }
57 return 0;
58 }
59

Why WA?

Tried with

0 0 0 0 0 0 0 0 0 0
3 3 3 3 3 3 3 3 3 3
0 1 2 0 1 3 3 2 3 0

off course before submitting changed 'b' to ' '.

WTF is wong with this problem?

sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

### Re: 457 wa

Use the search option to find out existing threads on this problem. You will find lots of discussion and i/o there.
Don't create e new thread for a problem that already exists!

Mahsa
New poster
Posts: 1
Joined: Tue Aug 24, 2010 4:14 pm

### Re: Linear Cellular Automata 457

can anyone explain me this problem?I can't understand the problem itself...

talz13
New poster
Posts: 5
Joined: Sat Oct 18, 2008 7:20 pm

### Re: Linear Cellular Automata 457

I'm getting WA right now:

Code: Select all

``````#include <iostream>

using namespace std;

const int DISH_ARRAY_SIZE = 40;
const int DNA_ARRAY_SIZE = 10;
const int DAYS = 50;

int calcK(int dishArray[], int index);
void printDishArray(int dishArray[]);

int main() {
int dishArray[DISH_ARRAY_SIZE] = {0};
int updatedDishArray[DISH_ARRAY_SIZE] = {0};
dishArray[19] = 1;

char casesStr[DNA_ARRAY_SIZE];
int DNA[DNA_ARRAY_SIZE];
char testChar = '\0';
int cases = 0;
int count = 0;

for (int i = 0; (testChar = cin.get()) != 10 && testChar != 13 && !cin.eof(); i++) {
casesStr[i] = testChar;
}
cases = atoi(casesStr);
cin.get();

while (count < cases && !cin.eof()) {
//for (int i = 0; i < DNA_ARRAY_SIZE; i++)
//    casesStr[i] = '\0';
//cases = 0;
//for (int i = 0; (testChar = cin.get()) != 10 && testChar != 13 && !cin.eof(); i++) {
//    casesStr[i] = testChar;
//}
//cases = atoi(casesStr);
//cin.get();
for (int i = 0; (testChar = cin.get()) != 10 && testChar != 13 && !cin.eof(); i++) {
if (testChar == ' ') {
i--;
continue;
}
DNA[i] = testChar - '0';
}
cin.get();

printDishArray(dishArray);
cout << endl;

for (int i = 0; i < DAYS; i++) {
for (int j = 0; j < DISH_ARRAY_SIZE; j++)
updatedDishArray[j] = DNA[calcK(dishArray, j)];
for (int j = 0; j < DISH_ARRAY_SIZE; j++)
dishArray[j] = updatedDishArray[j];
printDishArray(dishArray);
if (i < DAYS - 1)
cout << endl;
}

for (int i = 0; i < DISH_ARRAY_SIZE; i++) {
dishArray[i] = 0;
updatedDishArray[i] = 0;
}
dishArray[19] = 1;
if (count < cases - 1)
cout << endl << endl;
count++;
}
return 0;
}

int calcK(int dishArray[], int index) {
int returnVal = 0;
if (index > 0 && index < DISH_ARRAY_SIZE - 1)
returnVal = dishArray[index - 1] + dishArray[index] + dishArray[index + 1];
else if (index == 0)
returnVal = dishArray[index] + dishArray[index + 1];
else if (index == DISH_ARRAY_SIZE - 1)
returnVal = dishArray[index - 1] + dishArray[index];

return returnVal;
}

void printDishArray(int dishArray[]) {
char outputChar = '\0';
for (int i = 0; i < DISH_ARRAY_SIZE; i++)
{
switch (dishArray[i]) {
case 0:
outputChar = ' ';
break;
case 1:
outputChar = '.';
break;
case 2:
outputChar = 'x';
break;
case 3:
outputChar = 'W';
break;
}
cout << outputChar;
}
//cout << endl;
}``````

No.47WK
New poster
Posts: 4
Joined: Thu Mar 31, 2011 1:25 pm

### Help me with 457 WA

Code: Select all

``````#include<iostream>
#include<cstring>
using namespace std;
int arr[10];
void change(char *a,char *b)
{
int i=0,j,w;
char *p;
while(i<40)
{
//cout<<i<<' ';
for(p=a-1,j=0,w=0;j<3;++j,++p)
{
if(*p=='.')
++w;
else if(*p=='x')
w=w+2;
else if(*p=='w')
w=w+3;
}
//cout<<arr[w];
if(arr[w]==0)
*b=' ';
else if(arr[w]==1)
*b='.';
else if(arr[w]==2)
*b='x';
else if(arr[w]==3)
*b='w';
//cout<<*a;
++a;
++b;
++i;
}
//cout<<endl;
}
int main()
{
char a[43],b[43],*store=&a[1],*store1=&b[1],*t;
int te,tem,temp;
while(cin>>te)
{
while(te>0)
{
for(tem=0;tem<10;++tem)
cin>>arr[tem];
memset(a,' ',sizeof(a));
memset(b,' ',sizeof(b));
a[20]='.';
//cout<<a<<endl;
temp=50;
while(temp>0)
{
if(temp%2==0)
{
change(store,store1);
*(store1+40)=0;
cout<<store1<<endl;
*(store1+40)=' ';
}
else
{
change(store1,store);
*(store+40)=0;
cout<<store<<endl;
*(store+40)=' ';
}
//cout<<a<<endl;
//cout<<b<<endl;
--temp;
}
cout<<endl;
--te;
}
}
return 0;
}

``````
give me some I/O to help me get though this boring problem please.
I don't know which part of my code is wrong

lprell
New poster
Posts: 5
Joined: Fri Jun 17, 2011 8:56 am

### Re: Linear Cellular Automata 457

Dont know why im getting WA.
Can anybody help me please?

Code: Select all

``````
# include <stdio.h>

int main ()
{
int caso, aux, i, j;
int dna[10], cultura[50][40];
scanf("%d", &caso);
while (caso--)
{
for (aux=0; aux<10; aux++)
{
scanf("%d", &dna[aux]);
}
for (j=0; j<40; j++)
{
cultura[0][j]= 0;
}
cultura[0][19]=1;
for (i=1; i<50; i++)
{
cultura[i][0]= dna[cultura[i-1][0]+cultura[i-1][1]];
cultura[i][39]= dna[cultura[i-1][38]+cultura[i-1][39]];
for (j=1; j<39; j++)
{
cultura[i][j]= dna[cultura[i-1][j-1]+cultura[i-1][j]+cultura[i-1][j+1]];
}
}
for (i=0; i<50; i++)
{
for (j=0; j<40; j++)
{
if (cultura[i][j]== 0)
printf(" ");
else
if (cultura[i][j]== 1)
printf(".");
else
if (cultura[i][j]== 2)
printf("x");
else
if (cultura[i][j]== 3)
printf("W");
}
printf("\n");
}
}
return 0;
}
``````

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

### Re: Linear Cellular Automata 457

Mahsa wrote:can anyone explain me this problem?I can't understand the problem itself...
So, this is one of those problems where the description's long and windy - but the actual problem's more or less straightforward. Let's see if we can clear things up by walking through part of the sample input.

Assume that 40 dishes are all lying side by side numbered 1 through 40. Now, as we're told, on Day #1 Dish #20 has a population of 1 while the rest have a population density of 0. So, this is what the array of 40 dishes looks like on Day #1

Code: Select all

``0000000000000000000100000000000000000000``
Now, on Day #2, per the following lines
In any given culture dish, let K be the sum of that culture dish's density and the densities of the dish immediately to the left and the dish immediately to the right. Then, by the next day, that dish will have a population density of DNA[K]
observe that the population density of Dish #19 is

Code: Select all

``DNA[0 + 0 + 1] = DNA[1] = 1``
since Dish #18 has a population density of 0, Dish #19 has a population of 1 and Dish #20 has a population of density 1 and the DNA program array looks like so (with indices starting from 0 all the way through 9)

Code: Select all

``0 1 2 0 1 3 3 2 3 0``
and so DNA[1] is 1.

Reasoning in a similar manner, observe that Dish #20 has a population density of 1 (DNA[0 + 1 + 0] = DNA[1] = 1) and Dish #21 too has a population density of 1 (DNA[1 + 0 + 0] = DNA[1] = 1). So, it stands to reason that on Day #2, the array of 40 dishes look like so

Code: Select all

``0000000000000000001110000000000000000000``
Hopefully, this should make things clearer.
Check input and AC output for over 7,500 problems on uDebug!

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

### Re: Linear Cellular Automata 457

talz13 wrote:I'm getting WA right now:
Try the following input

Code: Select all

``````2

0 1 2 0 1 3 3 2 3 0

0 1 2 0 1 3 3 2 3 0``````
Check input and AC output for over 7,500 problems on uDebug!

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

### Re: Linear Cellular Automata 457

lprell wrote:Dont know why im getting WA.
Can anybody help me please?
You need to output an blank line in between cases. Also, do not output a blank line after the last test case.
Check input and AC output for over 7,500 problems on uDebug!

biplabks
New poster
Posts: 7
Joined: Sun Mar 09, 2014 11:20 am

### Re: Linear Cellular Automata 457

Hi:

Here is my code.I don't know why i am getting WA.
got accepted.
Last edited by biplabks on Mon Mar 10, 2014 10:47 am, edited 1 time in total.

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

### Re: Linear Cellular Automata 457

biplabks wrote:Here is my code.I don't know why i am getting WA.
You're printing extra newlines at the beginning of the output.
Check input and AC output for over 7,500 problems on uDebug!