传染

 1 #include <iostream>
 2 #include <cstdlib>
 3 using namespace std;
 4 
 5 struct peo{
 6     int x;
 7     int y;
 8 };
 9 int front;
10 int rear;
11 
12 peo point[10000];
13 unsigned int run_test(int map[100][100], unsigned int x, unsigned int y, unsigned int* person);
14 int Dx[4]={0,0,1,-1};
15 int Dy[4]={1,-1,0,0};
16     
17 void main(void)
18 {
19     int map[100][100];
20     unsigned int time;
21     unsigned int person;
22 
23     for (int x = 0; x < 100; x++ )
24         for( int y = 0; y < 100; y++ )
25             map[x][y] = ((rand() % 3) != 0) ? 1 : 0;
26     
27     time = run_test(map, rand() % 100, rand() % 100, &person);
28     for(int i=0;i<100;i++)
29     {
30         for(int j=0;j<100;j++)
31         {
32             if(map[i][j]==1)
33             {
34                 person++;
35             }
36         }
37     }
38     printf("Time: %d, Person: %d\n", time, person);
39 }
40 
41 unsigned int run_test(int map[100][100], unsigned int x, unsigned int y, unsigned int* person)
42 {
43     *person = 0;// the number of people who are not infcted 
44     int mx;
45     int my;
46     rear=0;
47     front=0;
48     int count=0;
49     int a=0;
50     point[rear].x=x;
51     point[rear++].y=y;
52     map[x][y]=3;
53     while(rear>front)
54     {
55         a=rear-1;
56         while(front<=a) 
57         {
58             for(int j=0;j<4;j++)
59             {
60                 mx=point[front].x+Dx[j];
61                 my=point[front].y+Dy[j];
62                 if(mx<100&&mx>=0&&my<100&&my>=0&&map[mx][my]==1)
63                 {
64                     point[rear].x=mx;
65 
66                     point[rear++].y=my;
67 
68                     map[mx][my]=2;
69                 }
70             }
71             front++;
72          }
73         count++;
74     }
75     count--;
76     return count;// the total time for which all people are infected (second) 
77 }

 

posted @ 2017-01-24 14:49  jintg  阅读(118)  评论(0)    收藏  举报