在一个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); }