zoukankan      html  css  js  c++  java
  • 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);
    }
  • 相关阅读:
    vue单文件组件形成父子(子父)组件之间通信(vue父组件传递数据给子组件,子组件传递数据给父组件)
    appium 问题四的解决办法(模拟器打开的页面弹出框与脚本打开页面的弹出框不一致)
    appium 自动化问题三--键盘关键字的使用
    RF+appium自动化问题二解决思路
    appium自动化滑动鼠标滚动条的用法
    appium自动化中元素定位碰到的问题一
    appium自动化时,输入中文不显示的问题解决
    appium自动化模拟器使用
    pycharm 无法识别selenium,appium等工具时的解决办法
    Mysql基础学习(二)之DQL的使用
  • 原文地址:https://www.cnblogs.com/ZzznOoooo/p/6628075.html
Copyright © 2011-2022 走看看