// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
#define row 9
#define col 20
// Function to print trajectory of the virus
// and safe and infected citizens
void coronaVirus(char arr[row][col])
{
// To store Inverted Array
char inverted[9][20];
// Temporary array to store
// original array
char temp[9][20];
// To store number of infected citizens
int count = 0;
// To store total number of citizens ('a', 'c')
int total = 0;
// To invert the array
for (int i = 0; i <= 8; i++) {
for (int j = 0; j < 20; j++) {
// Invert the array row wise
inverted[i][j] = arr[8 - i][j];
}
}
// Count the number of citizens
for (int i = 0; i <= 8; i++) {
for (int j = 0; j < 20; j++) {
// Count total number of citizens.
if (inverted[i][j] == 'a'
|| inverted[i][j] == 'c') {
total++;
}
// Copy inverted array in temp
temp[i][j] = inverted[i][j];
}
}
// To store number of times,
// virus encountered a boundary
int bound = 0;
// Variable for row-wise traversal
int i = 1;
// Variable for column-wise traversal
int j = 1;
// Variable to store direction
int direct = 1;
// The virus starts from (0, 0) always
cout << "0 0" << endl;
// To break infinite looping
int flag = 0;
// Finding trajectory and safe
// and infected citizens
while (bound <= 1 && flag < 1000) {
flag++;
cout << i << " " << j << endl;
// Virus has striked the boundary twice.
// Therefore, end loop
if (inverted[j][i] == '*' && bound == 1) {
break;
}
// Virus striked the corners, end loop
if (inverted[j][i] == '*') {
if (i == 0 && j == 8 || i == 20 && j == 8
|| i == 20 && j == 0) {
break;
}
}
// First time for the virus
// to hit the boundary
if (inverted[j][i] == '*' && bound == 0) {
// Increment bound variable
bound++;
// Strikes the left wall
if (i == 0 && j != 8 && j != 0) {
// Turns 90 degree clockwise
if (direct == 2) {
direct = 1;
i++;
j++;
}
// Else turns 90 degree anticlockwise
else if (direct == 4) {
direct = 3;
j--;
i++;
}
}
// Strikes the right wall
else if (i == 20 && j != 0 && j != 8) {
// Turns 90 degree anticlockwise
if (direct == 1) {
direct = 2;
i--;
j++;
}
// Else turns 90 degree clockwise
else if (direct == 3) {
direct = 4;
i--;
j--;
}
}
// Strikes the upper wall
else if (j == 8 && i != 0 && i != 20) {
// Turns 90 degree anticlockwise
if (direct == 2) {
direct = 4;
i--;
j--;
}
// Else turns 90 degree clockwise
else if (direct == 1) {
direct = 3;
j--;
i++;
}
}
// Strikes the lower wall
else if (j == 0 && i != 0 && i != 20) {
// Turns 90 degree clockwise
if (direct == 4) {
direct = 2;
i--;
j++;
}
// Else turns 90 degree anticlockwise
else if (direct == 3) {
direct = 1;
i++;
j++;
}
}
continue;
}
// Make 'c' visited by replacing it by'-'
if (inverted[j][i] == 'c') {
temp[j][i] = '-';
// Turns all directions 90
// degree clockwise
if (direct == 1) {
direct = 3;
j--;
i++;
}
else if (direct == 2) {
direct = 1;
i++;
j++;
}
else if (direct == 3) {
direct = 4;
i--;
j--;
}
else if (direct == 4) {
direct = 2;
i--;
j++;
}
// Increment count of infected citizens
count++;
continue;
}
// Make 'a' visited by replacing it by'-'
if (inverted[j][i] == 'a') {
temp[j][i] = '-';
// Turns all directions by 90 degree
// anticlockwise
if (direct == 1) {
direct = 2;
i--;
j++;
}
else if (direct == 2) {
direct = 4;
i--;
j--;
}
else if (direct == 3) {
direct = 1;
i++;
j++;
}
else if (direct == 4) {
direct = 3;
j--;
i++;
}
// Increment count of
// infected citizens
count++;
continue;
}
// Increment the counter diagonally
// in the given direction.
if (inverted[j][i] == '.') {
if (direct == 1) {
i++;
j++;
}
else if (direct == 2) {
i--;
j++;
}
else if (direct == 3) {
j--;
i++;
}
else if (direct == 4) {
i--;
j--;
}
continue;
}
}
// Print the mirror of the array
// i.e. last row must be printed first.
for (int i = 0; i <= 8; i++) {
for (int j = 0; j < 20; j++) {
cout << temp[8 - i][j];
}
cout << endl;
}
// Print safe and infected citizens
cout << "safe=" << (total - count) << endl;
cout << "infected=" << (count) << endl;
}
// Driver Code
int main()
{
// Given 2D array
char arr[row][col]
= { { '*', '*', '*', '*', '*', '*', '*',
'*', '*', '*', '*', '*', '*', '*',
'*', '*', '*', '*', '*', '*' },
{ '*', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '*' },
{ '*', '.', '.', 'c', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '*' },
{ '*', '.', '.', '.', '.', 'c', '.',
'.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '*' },
{ '*', '.', '.', '.', '.', '.', '.',
'.', '.', '.', 'a', '.', '.', '.',
'.', '.', '.', '.', '.', '*' },
{ '*', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '*' },
{ '*', '.', '.', '.', '.', '.', '.',
'.', 'a', '.', '.', '.', '.', '.',
'.', 'c', '.', '.', '.', '*' },
{ '*', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '.', '.',
'.', '.', '.', '.', '.', '*' },
{ '*', '*', '*', '*', '*', '*', '*',
'*', '*', '*', '*', '*', '*', '*',
'*', '*', '*', '*', '*', '*' } };
// Function Call
coronaVirus(arr);
return 0;
}