Open In App

C++ Program to Find Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings

Given a numeric string target of length N and a set of numeric strings blocked, each of length N, the task is to find the minimum number of circular rotations required to convert an initial string consisting of only 0‘s to target by avoiding any of the strings present in blocked at any step. If not possible, print -1.
Note: A single rotation involves increasing or decreasing a value at particular index by 1 unit. As rotations are circular, 0 can be converted to 9 or a 9 can be converted to 0.
Examples: 

Input: target = “7531”, blocked = {“1543”, “7434”, “7300”, “7321”, “2427” } 
Output: 12 
Explanation: “0000” -> “9000” -> “8000” -> “7000” -> “7100” -> “7200” -> “7210” -> “7310” -> “7410” -> “7510” -> “7520” -> “7530” -> “7531”



Input: target = “4231”, blocked = { “1243”, “4444”, “1256”, “5321”, “2222” } 
Output: 10 

Approach: In order to solve this problem, we are using the following BFS approach: 



Below is the implementation of the above logic:




// C++ Program to count the minimum
// number of circular rotations required
// to obtain a given numeric strings
// avoiding a set of blocked strings
  
#include <bits/stdc++.h>
using namespace std;
  
int minCircularRotations(
    string target,
    vector<string>& blocked,
    int N)
{
    string start = "";
    for (int i = 0; i < N; i++) {
        start += '0';
    }
  
    unordered_set<string> avoid;
  
    for (int i = 0; i < blocked.size(); i++)
        avoid.insert(blocked[i]);
  
    // If the starting string needs
    // to be avoided
    if (avoid.find(start) != avoid.end())
        return -1;
  
    // If the final string needs
    // to be avoided
    if (avoid.find(target) != avoid.end())
        return -1;
  
    queue<string> qu;
    qu.push(start);
  
    // Variable to store count of rotations
    int count = 0;
  
    // BFS Approach
    while (!qu.empty()) {
  
        count++;
  
        // Store the current size
        // of the queue
        int size = qu.size();
  
        for (int j = 0; j < size; j++) {
  
            string st = qu.front();
            qu.pop();
  
            // Traverse the string
            for (int i = 0; i < N; i++) {
  
                char ch = st[i];
  
                // Increase the
                // current character
                st[i]++;
  
                // Circular rotation
                if (st[i] > '9')
                    st[i] = '0';
  
                // If target is reached
                if (st == target)
                    return count;
  
                // If the string formed
                // is not one to be avoided
                if (avoid.find(st)
                    == avoid.end())
                    qu.push(st);
  
                // Add it to the list of
                // strings to be avoided
                // to prevent visiting
                // already visited states
                avoid.insert(st);
  
                // Decrease the current
                // value by 1 and repeat
                // the similar checkings
                st[i] = ch - 1;
  
                if (st[i] < '0')
                    st[i] = '9';
                if (st == target)
                    return count;
                if (avoid.find(st)
                    == avoid.end())
                    qu.push(st);
                avoid.insert(st);
  
                // Restore the original
                // character
                st[i] = ch;
            }
        }
    }
  
    return -1;
}
  
// Driver code
int main()
{
    int N = 4;
    string target = "7531";
    vector<string> blocked
        = { "1543",
            "7434",
            "7300",
            "7321",
            "2427" };
  
    cout << minCircularRotations(
                target,
                blocked, N)
         << endl;
  
    return 0;
}

Output: 
12

 

Time Complexity: O(N3)
Auxiliary Space: O(N)

Please refer complete article on Minimum circular rotations to obtain a given numeric string by avoiding a set of given strings for more details!


Article Tags :