function B = gameOfLife(A)
B=A;
C=[A,A,A;A,A,A;A,A,A];
for i=5:8
for j=5:8
cnt=0;
for m=i-1:i+1
for n=j-1:j+1
if m==i&&n==j
cnt=cnt;
else
if C(m,n)==1
cnt=cnt+1;
end
end
end
end
if C(i,j)==1 %该细胞现在存活
if cnt>3||cnt<2
B(i-4,j-4)=0;
end
else %该细胞现在死亡
if cnt==3
B(i-4,j-4)=1;
end
end
end
end
end
Test | Status | Code Input and Output |
---|---|---|
1 | Pass |
A = [ ...
1 1 0 0
0 1 0 0
1 1 0 0
0 0 0 0];
B = [ ...
1 1 0 0
0 0 1 0
1 1 0 0
0 0 0 0];
assert(isequal(gameOfLife(A),B))
|
2 | Pass |
A = [ ...
0 1 1 0
1 1 1 0
0 0 1 0
0 0 0 0];
B = [ ...
1 0 1 1
1 0 0 0
0 0 1 1
0 1 1 0];
assert(isequal(gameOfLife(A),B))
|
900 Solvers
284 Solvers
Find the maximum number of decimal places in a set of numbers
739 Solvers
Create matrix of replicated elements
321 Solvers
2060 Solvers
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!