给定一个整数N,它表示矩阵的大小为N*N。有一个机器人放置在矩阵的左上角 (0, 0) 上,机器人的运动方向为 (N, S, W, E, NE, NW, SE, SW 表示北,还给出了南、西、东、东北、西北、东南、西南)和特定方向运动的持续时间。任务是在所有风结束时机器人移动完成后,找到矩阵中未访问的单元格。
注意:机器人只能访问一个单元格一次。如果在任何时候机器人无法移动,则它将停留在当前位置。此外,机器人每秒可以移动一个。
例子:
Input: N = 3, move[] = {(0, SE), (2, N)}
Output: 4
Explanation:
Input:
N = 5, move[] =
{(0, SE),
(1, NE),
(2, E),
(6, SW),
(15, N),
(20, W)}
Output:
13
Explanation:
After the movements of the robot, there are 13 Cells unvisited.
方法:思路是用递归来解决这个问题。最初,将机器人的当前位置设置为 (0, 0)。按照给定的方向开始机器人的运动,并将单元格标记为矩阵的访问过。最后,在机器人标记完成移动后,计算矩阵中未被标记为已访问的单元格
下面的代码实现了上面讨论的方法:
C++
// C++ implementation to find the
// unvisited cells of the matrix
#include
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 >& 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 > periods)
{
// nXn matrix to store the
// visited state of positions
vector > visited;
// map to store the wind directions
unordered_map > 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{});
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 > 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;
}
输出:
13
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live