Infect

#include <stdlib.h>
#include <stdio.h>
/*
#define SIZE 100
unsigned int run_test(int map[SIZE][SIZE], unsigned int x, unsigned int y, unsigned int* person)
{
    bool flag=false;
    if(x>0&&map[x-1][y]==1){map[x-1][y]=11;flag=true;}
    if(x+1<100&&map[x+1][y]==1){map[x+1][y]=11;flag=true;}
    if(y>0&&map[x][y-1]==1){map[x][y-1]=11;flag=true;}
    if(y+1<100&&map[x][y+1]==1){map[x][y+1]=11;flag=true;}
    int count=11;
    while(flag){
        flag=false;
        for(int i=0;i<SIZE;i++){
            for(int j=0;j<SIZE;j++){
                if(map[i][j]==count){
                    if(i>0&&map[i-1][j]==1){map[i-1][j]=count+1;flag=true;}
                    if(i+1<SIZE&map[i+1][y]==1){map[i+1][j]=count+1;flag=true;}
                    if(j>0&&map[i][j-1]==1){map[i][j-1]=count+1;flag=true;}
                    if(j+1<SIZE&&map[i][j+1]==1){map[i][j+1]=count+1;flag=true;}

                     if(j+1<SIZE&&map[i][j+1]==0&&j>0&&map[i][j-1]==0&&i+1<SIZE&&map[i+1][j]==0&&i>0&&map[i-1][j]==0)flag=false;

                }
            }
        }
        count++;
    }
    int a=0;
    for(int i=0;i<SIZE;i++){
        for(int j=0;i<SIZE;j++){
            if(map[i][j]==1){
                a++;
            }
        }
    }

    *person = a; // the number of people who are not infected 
    return count-11; // the total time for which all people are infected (second) 
}


void main(void)
{
    int           map[100][100];
    unsigned int time;
    unsigned int person;

    for (int x = 0; x < 100; x++ )
        for( int y = 0; y < 100; y++ )
            map[x][y] = ((rand() % 3) != 0) ? 1 : 0;
    
    time = run_test(map, rand() % 100, rand() % 100, &person);

    printf("Time: %d, Person: %d
", time, person);
}


#include <stdlib.h>
#include <stdio.h>
#define SIZE 100

unsigned int run_test(int map[SIZE][SIZE], unsigned int x, unsigned int y, unsigned int* person);
unsigned int run_test(int map[SIZE][SIZE], unsigned int x, unsigned int y, unsigned int* person)
{
    bool flag=false;
    if(y+1<SIZE&&map[x][y+1]==1){map[x][y+1]=11;flag=true;}
    if(y>0&&map[x][y-1]==1){map[x][y-1]=11;flag=true;}
    if(x+1<SIZE&&map[x+1][y]==1){map[x+1][y]=11;flag=true;}
    if(x>0&&map[x-1][y]==1){map[x-1][y]=11;flag=true;}
    int count=11;
    while(flag){
        flag=false;
        for(int i=0;i<SIZE;i++){
            for(int j=0;j<SIZE;j++){
                if(map[i][j]==count){
                    if(j<SIZE-1&&map[i][j+1]==1){map[i][j+1]=count+1;flag=true;}
                    if(j>0&&map[i][j-1]==1){map[i][j-1]=count+1;flag=true;}
                    if(i<SIZE-1&&map[i+1][j]==1){map[i+1][j]=count+1;flag=true;}
                    if(i>0&&map[i-1][j]==1){map[i-1][j]=count+1;flag=true;}
                    if(j+1<SIZE&&map[i][j+1]==0&&j>0&&map[i][j-1]==0&&i+1<SIZE&&map[i+1][j]==0&&i>0&&map[i-1][j]==0)flag=false;
          }
       }
   }
        count++;
    }
    count=count-11;
    int l=0;
    for(int i=0;i<SIZE;i++){
       for(int j=0;j<SIZE;j++){
           if(map[i][j]==1)l++;
       }
    }

    *person = l; // the number of people who are not infected 
    return count; // the total time for which all people are infected (second) 
}

*/
#define SIZE 100

class point{
public:
    int x,y;
};
point que[SIZE*SIZE];
int front,rear;
point c[4]={{1,0},{-1,0},{0,1},{0,-1}};//4个方向
void push(int x,int y){
    que[rear].x=x;
    que[rear].y=y;
    rear++;
}
point pop(){
    return que[front++];
}
bool isEmpty(){
    if(front==rear)return true;
    return false;
}
unsigned int run_test(int map[SIZE][SIZE], unsigned int y, unsigned int x, unsigned int* person){//换了下xy的位置,参考代码xy写反了
    front=0,rear=0;//头尾指针
    int sec=-1,tempx,tempy;//秒数
    push(x,y);
    while(!isEmpty()){
        sec++;
        int t=rear-front; //每一秒的节点数
        while(t--){//注意不能写成for(int i=front;i<rear;i++),因为push会改变rear的值.这样虽然能遍历完,但是sec会是0
            point p=pop();
            for(int j=0;j<4;j++){//4个方向
                tempx=p.x+c[j].x;
                tempy=p.y+c[j].y;
                if(tempx>=0&&tempx<SIZE&&tempy>=0&&tempy<SIZE&&map[tempy][tempx]==1){//数组没越界并且有人
                    push(tempx,tempy);
                    map[tempy][tempx]=0;
                }
            }
        }
    }
    int count=0;
    for(int i=0;i<SIZE;i++)
    for(int j=0;j<SIZE;j++)
        if(map[i][j]==1)count++;
    *person = count; 
    return sec;
}
void main(void)
{
    int           map[SIZE][SIZE];
    unsigned int time;
    unsigned int person;

    for (int x = 0; x < SIZE; x++ )
        for( int y = 0; y < SIZE; y++ )
            map[x][y] = ((rand() % 3) != 0) ? 1 : 0;
    
    time = run_test(map, rand() % 100, rand() % 100, &person);

    printf("Time: %d, Person: %d
", time, person);
}
原文地址:https://www.cnblogs.com/ZzznOoooo/p/6628075.html