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);
    }
  • 相关阅读:
    Spring+redis整合遇到的问题集以及注意事项
    Map源码学习之HashMap
    评分---五星好评
    下拉复选框
    倒计时按钮—获取手机验证码按钮
    input上传文件个数控制
    ajax请求完之前的loading加载
    获取浏览器滚动距离
    获取浏览器可视区域宽高
    获取元素尺寸宽高
  • 原文地址:https://www.cnblogs.com/lvcoding/p/6605007.html
Copyright © 2011-2022 走看看