📜  回溯生成n位格雷码的方法

📅  最后修改于: 2021-05-25 03:58:04             🧑  作者: Mango

给定数字n,任务是生成n位格雷码(生成从0到2 ^ n-1的位模式,以便连续的模式相差一位)
例子:

Input : 2 
Output : 0 1 3 2
Explanation : 
00 - 0
01 - 1
11 - 3
10 - 2

Input : 3 
Output : 0 1 3 2 6 7 5 4

我们已经讨论了生成n位格雷码的方法

本文提供了针对相同问题的回溯方法。我们的想法是,对于n位之外的每个位,我们都可以选择忽略它,也可以反转位,这意味着我们的灰度序列对于n位上升到2 ^ n。因此,我们进行了两个递归调用,以求反转位还是保持原样。

C++
// CPP program to find the gray sequence of n bits.
#include 
#include 
using namespace std;
  
/* we have 2 choices for each of the n bits either we 
   can include i.e invert the bit or we can exclude the 
   bit i.e we can leave the number as it is. */
void grayCodeUtil(vector& res, int n, int& num)
{
    // base case when we run out bits to process
    // we simply include it in gray code sequence.
    if (n == 0) {
        res.push_back(num);
        return;
    }
  
    // ignore the bit.
    grayCodeUtil(res, n - 1, num);
  
    // invert the bit.
    num = num ^ (1 << (n - 1));
    grayCodeUtil(res, n - 1, num);
}
  
// returns the vector containing the gray 
// code sequence of n bits.
vector grayCodes(int n)
{
    vector res;
  
    // num is passed by reference to keep
    // track of current code.
    int num = 0;
    grayCodeUtil(res, n, num);
  
    return res;
}
  
// Driver function.
int main()
{
    int n = 3;
    vector code = grayCodes(n);
    for (int i = 0; i < code.size(); i++) 
        cout << code[i] << endl;    
    return 0;
}


Java
// JAVA program to find the gray sequence of n bits.
import java.util.*;
  
class GFG
{
  
static int num;
  
/* we have 2 choices for each of the n bits either we 
can include i.e invert the bit or we can exclude the 
bit i.e we can leave the number as it is. */
static void grayCodeUtil(Vector res, int n)
{
    // base case when we run out bits to process
    // we simply include it in gray code sequence.
    if (n == 0)
    {
        res.add(num);
        return;
    }
  
    // ignore the bit.
    grayCodeUtil(res, n - 1);
  
    // invert the bit.
    num = num ^ (1 << (n - 1));
    grayCodeUtil(res, n - 1);
}
  
// returns the vector containing the gray 
// code sequence of n bits.
static Vector grayCodes(int n)
{
    Vector res = new Vector();
  
    // num is passed by reference to keep
    // track of current code.
    num = 0;
    grayCodeUtil(res, n);
  
    return res;
}
  
// Driver function.
public static void main(String[] args)
{
    int n = 3;
    Vector code = grayCodes(n);
    for (int i = 0; i < code.size(); i++) 
        System.out.print(code.get(i) +"\n"); 
}
}
  
// This code is contributed by Rajput-Ji


Python3
# Python3 program to find the 
# gray sequence of n bits. 
  
""" we have 2 choices for each of the n bits 
either we can include i.e invert the bit or 
we can exclude the bit i.e we can leave 
the number as it is. """
def grayCodeUtil(res, n, num):
      
    # base case when we run out bits to process
    # we simply include it in gray code sequence. 
    if (n == 0):
        res.append(num[0])
        return
          
    # ignore the bit.
    grayCodeUtil(res, n - 1, num)
      
    # invert the bit. 
    num[0] = num[0] ^ (1 << (n - 1)) 
    grayCodeUtil(res, n - 1, num) 
      
# returns the vector containing the gray
# code sequence of n bits. 
def grayCodes(n):
    res = []
      
    # num is passed by reference to keep 
    # track of current code. 
    num = [0]
    grayCodeUtil(res, n, num) 
    return res 
  
# Driver Code
n = 3
code = grayCodes(n) 
for i in range(len(code)):
    print(code[i])
  
# This code is contributed by SHUBHAMSINGH10


C#
// C# program to find the gray sequence of n bits.
using System;
using System.Collections.Generic;
  
class GFG
{
  
static int num;
  
/* we have 2 choices for each of the n bits either we 
can include i.e invert the bit or we can exclude the 
bit i.e we can leave the number as it is. */
static void grayCodeUtil(List res, int n)
{
    // base case when we run out bits to process
    // we simply include it in gray code sequence.
    if (n == 0)
    {
        res.Add(num);
        return;
    }
  
    // ignore the bit.
    grayCodeUtil(res, n - 1);
  
    // invert the bit.
    num = num ^ (1 << (n - 1));
    grayCodeUtil(res, n - 1);
}
  
// returns the vector containing the gray 
// code sequence of n bits.
static List grayCodes(int n)
{
    List res = new List();
  
    // num is passed by reference to keep
    // track of current code.
    num = 0;
    grayCodeUtil(res, n);
  
    return res;
}
  
// Driver function.
public static void Main(String[] args)
{
    int n = 3;
    List code = grayCodes(n);
    for (int i = 0; i < code.Count; i++) 
        Console.Write(code[i] +"\n"); 
}
}
  
// This code is contributed by 29AjayKumar


输出:

0
1
3
2
6
7
5
4