Minimum Moves to Spread Stones Over Grid

Language Links

Java Solution |
Python Solution


class Solution {
 public:
  int minimumMoves(vector>& grid) {
    const int zeroCount = accumulate(grid.begin(), grid.end(), 0,
                                     [](int subtotal, const vector& row) {
      return subtotal + ranges::count(row, 0);
    });
    if (zeroCount == 0)
      return 0;

    int ans = INT_MAX;

    for (int i = 0; i < 3; ++i)
      for (int j = 0; j < 3; ++j)
        if (grid[i][j] == 0)
          for (int x = 0; x < 3; ++x)
            for (int y = 0; y < 3; ++y)
              if (grid[x][y] > 1) {
                --grid[x][y];
                ++grid[i][j];
                ans = min(ans, abs(x - i) + abs(y - j) + minimumMoves(grid));
                ++grid[x][y];
                --grid[i][j];
              }

    return ans;
  }
};
    

Problem Description

Welcome to the world of grid-based puzzles, where moving stones is the new Olympic sport! The problem at hand is to figure out the minimum number of moves required to spread stones evenly across a 3×3 grid. Imagine you have a bunch of stones, and they are all huddled together like introverts at a party. Your job is to make them mingle!

In this delightful scenario, each cell in the grid can hold a certain number of stones, and your goal is to ensure that every cell has at least one stone. If a cell is empty, it’s like that one friend who always shows up late to the party—nobody wants that!

So, how do we achieve this? By moving stones from one cell to another, of course! But beware, you can only move stones from cells that have more than one stone. It’s like trying to convince your friend to share their snacks—only if they have more than one bag!

Approach

The approach taken in this solution is a recursive one. The function minimumMoves counts the number of empty cells (cells with zero stones) and checks if there are any stones available to move. If there are no empty cells, it returns 0, indicating that no moves are needed.

For each empty cell, the function iterates through all other cells to find stones that can be moved. It decreases the stone count in the source cell and increases it in the target cell, calculating the distance moved. The function then recursively calls itself to check for further moves, keeping track of the minimum moves required.

Time and Space Complexity

Complexity Type Complexity
Time Complexity O(N^4), where N is the size of the grid (in this case, 3).
Space Complexity O(1), as we are using a constant amount of space for variables.

Real-World Example

Imagine you’re at a family gathering, and everyone is sitting in their own little corner, just like the stones in the grid. Your goal is to get everyone to the dinner table (the empty cells) so that they can enjoy the feast together. You can only move people who are sitting with someone else (cells with more than one stone). The fewer moves you make, the faster everyone can enjoy the food!

Similar Problems

If you enjoyed this problem, you might also like these: