Sicily 1721 Gray code[Special judge]

/*
* url : http://soj.me/show_problem.php?pid=1721&cid=
* Problem Name: Gray Code
* Statege: Brute Force
* Characteristics: the neighbourhood's code should have exzactly one different
*              So, use map<string, bool> to solve ;
*/

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;


int n ;


void solve ()
{
    int i, j, ans = 0, p = (int) pow (2, n) ;
    char str[20]  ; 
    memset (str, '0', sizeof (str)) ;
    str[n] = '\0' ;
    printf ("%s\n", str) ;
    map <string, bool> mp ;
    mp[str] = true ;
    
    while (ans != p-1)
    {
        for (i = n-1; i >= 0; i --)
        {
            char ch = str[i] ;
            if (str[i] == '0')
                str[i] = '1' ;
            else str[i] = '0' ;
            if (mp[str])
                str[i] = ch ;
            else {
                printf ("%s\n", str) ;
                mp[str] = true ;
                ans ++ ;
                break ;
            }
        }
    }
    printf ("\n") ;    
}

int main ()
{
    while (scanf ("%d", &n), n)
    {
        solve () ;
    }
    return 0 ;
}

你可能感兴趣的:(String,url)