Corona Virus Codevita 9 Solutions

Page Contents

Corona Virus Codevita 9 Solutions

A city is represented as a two-dimensional rectangular matrix. The outer wall of the given matrix denotes the boundaries of the city. Citizens are dispersed in the city at different locations. They are either depicted by {a, c}. Coronavirus has already infected the city.

The Coronavirus enters the city from coordinate (0, 0) and traverses along a diagonal path until it encounters a human. If it encounters a human, designated as a, its trajectory rotates anti-clockwise (right to left) by 90 degrees. Similarly, if it encounters a human, designated as c, its trajectory rotates clockwise (left to right) by 90 degrees. After infecting the person, the virus continues to move along its diagonal path.

During its traversal, if it hits the boundary of the city for the first time, it rotates 90 degrees to reenter the city. However, if it hits any of the boundary walls, the second time, the virus gets destroyed.

You have to calculate the trajectory taken by the virus, print the city map where infected citizens can be found and finally report the number of safe and infected citizens.

Also Read: TCS CodeVita Season 10

Input:

An input matrix of 9 rows and 20 columns consisting of characters “*”“a”“c” and “.” where

  • “*” denotes an element on the boundaries of the city
  • “a” denotes citizen after encountering whom the virus trajectory changes by 90 degrees (anti-clockwise direction)
  • “c” denotes citizen after encountering whom the virus trajectory changes by 90 degrees (clockwise direction)
  • “.” (dot) denotes an empty location within the city 

Output:

A random number of lines each denoting the coordinates of the trajectory of the virus.

From the next line an output matrix of 9 rows and 20 columns consisting of characters “*”“a”“c”“.” and “-” where

  • “*” denotes an element on the boundaries of the city
  • “a” denotes citizen after encountering whom the virus trajectory changes by 90 degrees (anti-clockwise direction)
  • “c” denotes citizen after encountering whom the virus trajectory changes by 90 degrees (clockwise direction)
  • “.” (dot) denotes an empty location within the city
  • “-“ denotes the location of the infected citizen

And the next two lines print the number of safe and infected citizens in the city.

Constraints:

  • 0 <= x <= 20
  • 0 <= y <= 8
  • The virus cannot hit the three corners (20, 8) (20, 0) (0, 8)

Time Limit

1

Examples

Example 1

Input

********************

*….c………….*

*…c…………..*

*c……………..*

*………….a….*

*c.c……………*

*.a…………….*

*………..c……*

********************

Output

0 0

1 1

2 2

1 3

2 4

3 5

4 6

5 5

6 4

7 3

8 2

9 1

10 0

11 1

12 2

13 3

14 4

13 5

12 6

11 7

10 8

********************

*….c………….*

*…-…………..*

*c……………..*

*………….-….*

*-.c……………*

*.-…………….*

*………..c……*

********************

safe=4

infected=4

Explanation

The virus trajectory starts from (0,0) and crosses (1,1) (2,2). At (2,2) we have a citizen of type a causing the virus trajectory to be rotated by 90 degrees anti clockwise. It moves until the next citizen in its path is encountered at (1, 3) of type c causing the virus trajectory to be rotated by 90 degree clockwise. It continues on its path till it reaches the next human in its path at (4,6) of type c Virus trajectory is again rotated by 90 degrees clockwise until it hits the boundary at (10,0). Since this is the first time that the virus hits the boundary, it rotates by 90 degrees anticlockwise to reenter the city. The trajectory then continues towards (11,1) (12,2) (13,3) and finally a citizen at (14,4) of type a rotating the trajectory to 90 degree anticlockwise. From there it continues its trajectory and hits the boundary at (10,8).

Since this is the second time the virus hits the boundary, the virus is destroyed.

So, along its trajectory starting from (0,0) it has infected 4 citizens at location (2,2) (1,3) (4,6) (14,4). The other 4 citizens who did not come in the virus trajectory are deemed to be safe.

Example 2

Input

********************

*………………*

*..c……………*

*….c………….*

*………a……..*

*………………*

*…….a……c…*

*………………*

********************

Output

0 0

1 1

2 2

3 3

4 4

5 5

6 4

7 3

8 2

9 3

10 4

9 5

8 6

7 7

6 8

5 7

4 6

3 5

2 4

1 3

0 2

********************

*………………*

*..c……………*

*….-………….*

*………-……..*

*………………*

*…….-……c…*

*………………*

********************

safe=2

infected=3

Explanation

The virus trajectory starts from (0,0) and crosses (1,1) (2,2) (5,5). At (5,5) we have a citizen of type c causing the virus trajectory to be rotated by 90 degrees clockwise. It moves until the next citizen in its path is encountered at (8,2) of type a causing the virus trajectory to be rotated by 90 degree anti clockwise. It continues on its path till it reaches the next human in its path at (10,4) of type a Virus trajectory is again rotated by 90 degrees clockwise until it hits the boundary at (6,8). Since this is the first time that the virus hits the boundary, it rotates by 90 degrees anticlockwise to reenter the city. The trajectory then continues towards (9,5) (8,6) (7,7) (6,8) and renters the city by rotating the trajectory by 90 degrees anti clockwise to follow the trajectory (5,7) (4,6) (3,5) (2,4) (1,3) (0,2).

At (0,2) it again hits the boundary and since this is the second time the virus hits the boundary, the virus is destroyed. So along its trajectory starting from (0,0) it has infected 3 citizens at location (5,5) (10,4) (8,2). The other 2 citizens who did not come in the virus trajectory are deemed to be safe.

Solution

Program: Corona Virus Codevita 9 Solutions in C++

#include <bits/stdc++.h>
using namespace std;
#define row 9
#define col 20
void coronaVirus(char arr[row][col])
{

	char inverted[9][20];
	char temp[9][20];
	int count = 0;
	int total = 0;
	for (int i = 0; i <= 8; i++) {
		for (int j = 0; j < 20; j++) {
			inverted[i][j] = arr[8 - i][j];
		}
	}

	for (int i = 0; i <= 8; i++) {
		for (int j = 0; j < 20; j++) {
			if (inverted[i][j] == 'a'
				|| inverted[i][j] == 'c') {
				total++;
			}

			temp[i][j] = inverted[i][j];
		}
	}

	int bound = 0;


	int i = 1,j=1,direct=1,flag=0;

	cout << "0 0" << endl;

	while (bound <= 1 && flag < 1000) {
		flag++;
		cout << i << " " << j << endl;

		if (inverted[j][i] == '*' && bound == 1) {
			break;
		}

		if (inverted[j][i] == '*') {
			if (i == 0 && j == 8 || i == 20 && j == 8
				|| i == 20 && j == 0) {
				break;
			}
		}

		if (inverted[j][i] == '*' && bound == 0) {

			bound++;

			if (i == 0 && j != 8 && j != 0) {

				if (direct == 2) {
					direct = 1;
					i++;
					j++;
				}

				else if (direct == 4) {
					direct = 3;
					j--;
					i++;
				}
			}

			else if (i == 20 && j != 0 && j != 8) {
				if (direct == 1) {
					direct = 2;
					i--;
					j++;
				}

				else if (direct == 3) {
					direct = 4;
					i--;
					j--;
				}
			}

			else if (j == 8 && i != 0 && i != 20) {

				if (direct == 2) {
					direct = 4;
					i--;
					j--;
				}

				else if (direct == 1) {
					direct = 3;
					j--;
					i++;
				}
			}

			else if (j == 0 && i != 0 && i != 20) 
                                {
				if (direct == 4) {
					direct = 2;
					i--;
					j++;
				}

				else if (direct == 3) {
					direct = 1;
					i++;
					j++;
				}
			}
			continue;
		}

		if (inverted[j][i] == 'c') {
			temp[j][i] = '-';

			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++;
			}

			count++;
			continue;
		}

		if (inverted[j][i] == 'a') {
			temp[j][i] = '-';

			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++;
			}

			count++;
			continue;
		}

		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;
		}
	}


	for (int i = 0; i <= 8; i++) {
		for (int j = 0; j < 20; j++) {
			cout << temp[8 - i][j];
		}
		cout << endl;
	}


	cout << "safe=" << (total - count) << endl;
	cout << "infected=" << (count) << endl;
}


int main()
{

	char arr[row][col]
		= { { '*', '*', '*', '*', '*', '*', '*',
			'*', '*', '*', '*', '*', '*', '*',
			'*', '*', '*', '*', '*', '*' },
			{ '*', '.', '.', '.', '.', '.', '.',
			'.', '.', '.', '.', '.', '.', '.',
			'.', '.', '.', '.', '.', '*' },
			{ '*', '.', '.', 'c', '.', '.', '.',
			'.', '.', '.', '.', '.', '.', '.',
			'.', '.', '.', '.', '.', '*' },
			{ '*', '.', '.', '.', '.', 'c', '.',
			'.', '.', '.', '.', '.', '.', '.',
			'.', '.', '.', '.', '.', '*' },
			{ '*', '.', '.', '.', '.', '.', '.',
			'.', '.', '.', 'a', '.', '.', '.',
			'.', '.', '.', '.', '.', '*' },
			{ '*', '.', '.', '.', '.', '.', '.',
			'.', '.', '.', '.', '.', '.', '.',
			'.', '.', '.', '.', '.', '*' },
			{ '*', '.', '.', '.', '.', '.', '.',
			'.', 'a', '.', '.', '.', '.', '.',
			'.', 'c', '.', '.', '.', '*' },
			{ '*', '.', '.', '.', '.', '.', '.',
			'.', '.', '.', '.', '.', '.', '.',
			'.', '.', '.', '.', '.', '*' },
			{ '*', '*', '*', '*', '*', '*', '*',
			'*', '*', '*', '*', '*', '*', '*',
			'*', '*', '*', '*', '*', '*' } };

	coronaVirus(arr);
	return 0;
}

Codevita Season 9 All Questions Solution

Leave a Comment

1 × five =