MR.yan's homework3

C++输入写法:
#include <stdlib.h>

#include <memory.h>

#include <fstream>

#include <iostream>

#define MaxX 80//Max x of matrix

#define MaxY 22//Max y of matrix



using std::cin;

using std::cout;

using std::ifstream;

using std::ofstream;

using std::endl;

//using std::ios;

struct cell

{

    int neighbor;//the number of neighbors of this cell

    bool Alive;//the status of this cell ,true means alive.

};



void Cell_mark(cell item[MaxX][MaxY],int cell_x,int cell_y)

//mark the near cells' "The number of cells near this cell"

{

    item[cell_x][cell_y].neighbor++;

    if(cell_x+1<MaxX)

    {

        item[cell_x+1][cell_y].neighbor++;

        if(cell_y+1<MaxY)item[cell_x+1][cell_y+1].neighbor++;

        if(cell_y-1>-1)  item[cell_x+1][cell_y-1].neighbor++;

    }

    if(cell_x-1>-1)

    {

        item[cell_x-1][cell_y].neighbor++;

        if(cell_y+1<MaxY)item[cell_x-1][cell_y+1].neighbor++;

        if(cell_y-1>-1)  item[cell_x-1][cell_y-1].neighbor++;

    }

    if(cell_y+1<MaxY)item[cell_x][cell_y+1].neighbor++;

    if(cell_y-1>-1)  item[cell_x][cell_y-1].neighbor++;

}









void renew(cell c1[MaxX][MaxY],cell c2[MaxX][MaxY])//renew c1 by c2

{

    int i,j;

    for(i=0;i<MaxX;i++)

    {

        for(j=0;j<MaxY;j++)

        {

            if(c2[i][j].neighbor==3)

            {c1[i][j].Alive=true;

             Cell_mark(c1,i,j);

            }

            else

            c1[i][j].Alive=false;

        }

    }





}



//void neighbor_print(cell item[MaxX][MaxY])//just for debug,output the number of  this cell's neighbors

//{

//    int i,j;

//        printf("**********************\n");

//        for(i=0;i<MaxX;i++)

//    {

//        for(j=0;j<MaxY;j++)

//        {

//            cout<<item[i][j].neighbor;

//        }

//        cout<<endl;

//    }

//    printf("**********************\n");

//}



void display(cell world[MaxX][MaxY])//display the world

{//ofstream fout("out.txt",ios::app);

    ofstream fout("out.txt");

     int i,j;

        for(i=0;i<MaxX;i++)

    {

        for(j=0;j<MaxY;j++)

        {

            if(world[i][j].Alive)

            fout<<"*";

            else

            fout<<" ";

        }

        fout<<endl;

    }

}



int main()

{

    ifstream fin("in.txt");

    cell A[MaxX][MaxY],B[MaxX][MaxY];

    memset(A,0,sizeof(A));

    memset(B,0,sizeof(B));

    int SeedNum,Seed_X,Seed_Y;

    int i,j;

    fin>>SeedNum;

    for(i=0; i<SeedNum; i++)

    {

        fin>>Seed_X>>Seed_Y;

        A[Seed_X][Seed_Y].Alive=true;

        Cell_mark(A,Seed_X,Seed_Y);

    }

     //Num_print(A);





    int QAQ=0;

    int hehe;

    while(cin>>hehe)

    {

        //system("cls");

        QAQ++;

        if(hehe!=1)break;

        else

        {

          if(QAQ%2)//Odd time

          {

              memset(B,0,sizeof(B));

              renew(B,A);//renew B by A

              display(B);

              //neighbor_print(B);

          }

          else//Even  time

          {

              memset(A,0,sizeof(B));

              renew(A,B);//renew A by B

              display(A);

              //neighbor_print(A);

          }

        }

    }



return 0;

}

 

 

 

 

QAQ变量计算当前循环次数,通过判断奇偶可以知道到底现在A、B结构体谁是前一状态,谁是后一状态。

 

 

Cell_Mark函数是最核心的一个函数,通过这个函数来标记周围的空间,避免了每个空间又来主动搜索附近具体有几个Cell,这种保留前一时刻状态的算法我们称为“动态规划”(Dynamic Programming)

 通过这个算法,可以让在计算周围Cell数的步骤时,时间复杂度从O(n^2)下降到O(n*logn).

 

这次主要是关于函数传参进行了一些研究,注意到在函数调用过程中,结构体数组只写了一个数组名,其实这个就是一个指针,所以到了函数内部的时候的修改都会影响到那个数组本身。

不过在写功能函数本体的时候,必须要写清楚这个数组的维度以及实际大小。否则要报错。

 

因为读不懂英文,所以函数名、输入初始数据的方式以及逻辑循环过程可能不符合题目规范,希望谅解QAQ

你可能感兴趣的:(home)