// C++ implementation to find the
// unvisited cells of the matrix
#include <bits/stdc++.h>
using namespace std;
// Dimension
// of the board
int n;
// Current location
// of the robot
int curr_i = 0, curr_j = 0;
// Function to move the robot
void moveRobot(
int n, int i,
int j, int dx,
int dy, int& duration,
vector<vector<bool> >& visited)
{
// if the robot tends to move
// out of the board
// or tends to visit an
// already visited position
// or the wind direction is changed
if (i < 0 || i >= n || j < 0 || j >= n
|| visited[i][j] == true
|| duration == 0) {
// the robot can't move further
// under the influence of
// current wind direction
return;
}
// Change the current location
// and mark the current
// position as visited
curr_i = i;
curr_j = j;
visited[i][j] = true;
// One second passed
// visiting this position
duration--;
moveRobot(n, i + dx, j + dy, dx,
dy, duration, visited);
}
// Function to find the unvisited
// cells of the matrix after movement
void findUnvisited(
int p,
vector<pair<int, string> > periods)
{
// nXn matrix to store the
// visited state of positions
vector<vector<bool> > visited;
// map to store the wind directions
unordered_map<string, vector<int> > mp
= { { "N", { -1, 0 } },
{ "S", { 1, 0 } },
{ "E", { 0, 1 } },
{ "W", { 0, -1 } },
{ "NE", { -1, 1 } },
{ "NW", { -1, -1 } },
{ "SE", { 1, 1 } },
{ "SW", { 1, -1 } } };
// Initially all of the
// positions are unvisited
for (int i = 0; i < n; i++) {
visited.push_back(vector<bool>{});
for (int j = 0; j < n; j++) {
visited[i].push_back(false);
}
}
for (int i = 0; i < p; i++) {
string dir = periods[i].second;
int dx = mp[dir][0];
int dy = mp[dir][1];
// duration for the which the
// current direction of wind exists
int duration;
if (i < p - 1) {
// difference of the start time
// of current wind direction
// and start time of the
// upcoming wind direction
duration
= periods[i + 1].first
- periods[i].first;
}
else {
// the maximum time for which
// a robot can move is
// equal to the diagonal
// length of the square board
duration = sqrt(2) * n;
}
// If its possible to move
// the robot once in the
// direction of wind, then
// move it once and call the
// recursive function for
// further movements
int next_i = curr_i + dx;
int next_j = curr_j + dy;
if (next_i >= 0
&& next_i < n
&& next_j >= 0
&& next_j < n
&& visited[next_i][next_j] == false
&& duration > 0) {
moveRobot(n, next_i,
next_j, dx, dy,
duration, visited);
}
}
// Variable to store the
// number of unvisited positions
int not_visited = 0;
// traverse over the matrix and
// keep counting the unvisited positions
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (visited[i][j] == false) {
not_visited++;
}
}
}
cout << not_visited << "\n";
}
// Driver Code
int main()
{
// Dimension of the board
n = 5;
// number of periods
int p = 6;
// vector of pairs
vector<pair<int, string> > periods(p);
periods[0] = { 0, "SE" };
periods[1] = { 1, "NE" };
periods[2] = { 2, "E" };
periods[3] = { 6, "SW" };
periods[4] = { 15, "N" };
periods[5] = { 20, "W" };
// Function Call
findUnvisited(p, periods);
return 0;
}