-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfectStudents.cpp
More file actions
195 lines (189 loc) · 5.18 KB
/
InfectStudents.cpp
File metadata and controls
195 lines (189 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
void printGrid(vector<vector<int>> grid)
{
for(auto i : grid)
{
for(auto j : i)
{
cout<< "|" << j << "|";
}
cout << endl;
}
}
int countAdj(vector<vector<int>> grid, pair<int, int> coord)
{
int row = coord.first;
int col = coord.second;
int matrixSize = grid.size() - 1;
int count = 0;
//Top left corner
if(row == 0 && col == 0)
{
int right = grid[row][(col + 1)];
int down = grid[row + 1][col];
return right + down;
}
//Top right corner
else if(row == 0 && col == matrixSize)
{
int left = grid[row][col - 1];
int down = grid[row + 1][col];
return left + down;
}
//Bottom left corner
else if(row == matrixSize && col == 0)
{
int right = grid[row][(col + 1)];
int up = grid[row - 1][col];
return right + up;
}
//Bottom right corner
else if(row == matrixSize && col == matrixSize)
{
int up = grid[row - 1][col];
int left = grid[row][col - 1];
return up + left;
}
//Top row, any position, not top left or right
else if(row == 0)
{
int left = grid[row][col - 1];
int right = grid[row][(col + 1)];
int down = grid[row + 1][col];
return left + right + down;
}
//Leftmost column, any position except top/bottom left
else if(col == 0)
{
int down = grid[row + 1][col];
int up = grid[row - 1][col];
int right = grid[row][(col + 1)];
return down + up + right;
}
//Bottom row, any position except bottom left/right
else if(row == matrixSize)
{
int left = grid[row][col - 1];
int right = grid[row][(col + 1)];
int up = grid[row - 1][col];
return left + right + up;
}
//Right most column, not including top/bottom right
else if(col == matrixSize)
{
int up = grid[row - 1][col];
int down = grid[row + 1][col];
int left = grid[row][col - 1];
return up + down + left;
}
//All other interior elements
else
{
int up = grid[row - 1][col];
int down = grid[row + 1][col];
int left = grid[row][col - 1];
int right = grid[row][(col + 1)];
return up + down + left + right;
}
}
vector<vector<int>> infectGrid(vector<vector<int>> grid)
{
vector<vector<int>>pGrid = grid;
for(int row = 0; row < pGrid.size(); row++)
{
for(int col = 0; col < pGrid[row].size(); col++)
{
if(pGrid[row][col] != 1 && countAdj(pGrid, make_pair(row, col)) > 1)
{
grid[row][col] = 1;
}
}
}
return grid;
}
bool totalInfect(vector<vector<int>> grid)
{
for(int row = 0; row < grid.size(); row++)
{
for(int col = 0; col < grid[row].size(); col++)
{
if(grid[row][col] == 0)
{
return false;
}
}
}
return true;
}
int main() {
vector<vector<int>> grid;
int size;
cin>>size;
for(int row = 0; row < size; row++)
{
vector<int>tempRow;
for(int col = 0; col < size; col++)
{
tempRow.push_back(0);
}
grid.push_back(tempRow);
}
//Random number between size/4 and the full size
int numInitInfect = rand()%size + size/4;
cout<<"<----------------------------------------------------------------------------->" << endl;
cout << "Number of students infected initially: " <<numInitInfect << endl;
//Initial Grid being set up
for(int i = 0; i < numInitInfect; i++)
{
int randRow = rand() % size;
int randCol = rand() % size;
if(grid[randRow][randCol] == 1)
{
i--;
}
else
{
grid[randRow][randCol] = 1;
}
}
cout<<"Initial grid:" << endl;
printGrid(grid);
vector<vector<int>> prevGrid;
int countIter = 0;
bool canInfect = true;
while(!totalInfect(grid))
{
prevGrid = grid;
grid = infectGrid(grid);
if(prevGrid == grid)
{
canInfect = false;
break;
}
else
{
countIter++;
cout<<"Iteration " << countIter<<endl;
printGrid(grid);
}
}
if(canInfect)
{
cout<<"\n\n"<<"-----------------------------------------------------------------------------"<<endl;
cout<<"FINAL GRID"<<endl;
printGrid(grid);
cout<<"Total number of Iterations: "<< countIter <<endl;
}
else
{
cout<<"\n\n"<<"-----------------------------------------------------------------------------"<<endl;
cout<<"Could not totally infect grid"<<endl;
cout<<"FINAL GRID"<<endl;
printGrid(grid);
cout<<"Total number of Iterations: "<< countIter <<endl;
}
return 0;
}