zoukankan      html  css  js  c++  java
  • Zombie 僵尸感染--BFS

      在一个map[100][100]中0代表空地,1代表人。僵尸能够上下左右感染,每五秒感染成功一次,求出最后感染需要的时间,即存活的人。

    #include<stdio.h>
    #include<stdlib.h>
    int next[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
    typedef struct{
        int x;
        int y;
    }point;
    typedef struct{
        point a[10000];
        int head;
        int rear;
    }queue;
    queue q;
    unsigned int run(int map[100][100],unsigned int x,unsigned int y,unsigned int &person){
        int t=0;
        q.rear=q.head=0;
        point temp;
        temp.x=x;
        temp.y=y;
        map[x][y]=2;
        q.a[q.rear++]=temp;
        while(q.head<q.rear){
            temp=q.a[q.head++];
            for(int i=0;i<4;i++){
                if(temp.x+next[i][0]>=0&&temp.x+next[i][0]<100&&temp.y+next[i][1]>=0&&temp.y+next[i][1]<100&&map[temp.x+next[i][0]][temp.y+next[i][1]]==1){
                    point ttemp;
                    ttemp.x=temp.x+next[i][0];
                    ttemp.y=temp.y+next[i][1];
                    map[ttemp.x][ttemp.y]=map[temp.x][temp.y]+1;
                    q.a[q.rear++]=ttemp;
                }
            }
        }
        for(int i=0;i<100;i++)
            for(int j=0;j<100;j++)
                if(map[i][j]==1)
                    person++;
        for(int i=0;i<100;i++)
            for(int j=0;j<100;j++){
                if(t<map[i][j])
                    t=map[i][j];
            }
                
        return (t-2)*5;
        
    }
    void main(){
        int map[100][100];
        unsigned int time;
        unsigned int person=0;
        for(int x=0;x<100;x++)
            for(int y=0;y<100;y++)
                map[x][y]=(rand()%3!=0) ? 1:0;
        time =run(map,rand()%100,rand()%100,person);
        printf("Time: %d, Person: %d
    ",time,person);
    }
  • 相关阅读:
    Charles的使用教程
    Sublime Text 3 无法使用package control安装插件解决办法
    sublime常用快捷键整理(未完待续)
    常用 Git 命令清单
    js判断的执行顺序
    使用CSS3 Media Queries实现网页自适应(转)
    移动前端开发之viewport的深入理解(转)
    javascript之函数节流
    String常用方法总结
    腾讯面试题1
  • 原文地址:https://www.cnblogs.com/lvcoding/p/6605007.html
Copyright © 2011-2022 走看看