# Solution Idea

## 參考解答

1. 邊界判斷
2. 所有cells在當回合結束時才會同時變換狀態
3. 盡可能減少if判斷次數，否則最後兩筆測資可能超時
4. 不要重複定義main()

game_of_cell.c

1234567891011121314151617181920212223242526272829303132333435void game_of_cell(int grid[50][50],int outcome[50][50],int N){  int changeTable[3][9]={  {0,0,0,2,0,0,0,0,0},  {1,1,2,1,1,1,1,1,1},  {0,0,2,2,1,1,1,1,1}};  int boundArray[52][52]={0};  for(int i=0;i<50;i++)        for(int j=0;j<50;j++)                boundArray[i+1][j+1]=grid[i][j];  for(int n=0;n<N;n++){      int numAlive[52][52]={0};      for(int i=1;i<51;i++)          for(int j=1;j<51;j++)          {              numAlive[i][j]+=(boundArray[i-1][j-1]/2);              numAlive[i][j]+=(boundArray[i-1][j]/2);              numAlive[i][j]+=(boundArray[i-1][j+1]/2);              numAlive[i][j]+=(boundArray[i][j-1]/2);              numAlive[i][j]+=(boundArray[i][j+1]/2);              numAlive[i][j]+=(boundArray[i+1][j-1]/2);              numAlive[i][j]+=(boundArray[i+1][j]/2);              numAlive[i][j]+=(boundArray[i+1][j+1]/2);           }      for(int i=1;i<51;i++)          for(int j=1;j<51;j++)             boundArray[i][j]=changeTable[boundArray[i][j]][numAlive[i][j]];   }        for(int i=0;i<50;i++)                for(int j=0;j<50;j++)                        outcome[i][j]=boundArray[i+1][j+1];       return;}


game_of_cell.h

1void game_of_cell(int grid[50][50],int outcome[50][50],int N);


### Note

Dev C++ Multi-file Compile Tutor