📌  相关文章
📜  在矩阵中查找机器人未进行给定运动的单元格

📅  最后修改于: 2021-05-17 23:31:23             🧑  作者: Mango

给定整数N,它表示矩阵的大小,即N * N。在矩阵的左上角(0,0)上放置了一个机器人,该机器人的移动方向为(N,S,W,E,NE,NW,SE,SW,表示北,并分别给出了向南,向西,向东,向东北,向西北,向东南,向西南方向移动的时间,并给出了在特定方向上移动的持续时间。任务是在所有风结束时完成机器人的移动之后,找到矩阵中未访问的单元。

注意:机器人只能访问一个单元一次。如果机器人在任何时候都无法移动,它将停留在当前位置。而且,机器人每秒可以移动一步。

例子:

方法:想法是使用递归来解决此问题。首先,将机器人的当前位置设置为(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