poj 1780 Code (欧拉回路+非递归dfs)

Code
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1746   Accepted: 655

Description

KEY Inc., the leading company in security hardware, has developed a new kind of safe. To unlock it, you don't need a key but you are required to enter the correct n-digit code on a keypad (as if this were something new!). There are several models available, from toy safes for children (with a 2-digit code) to the military version (with a 6-digit code). 

The safe will open as soon as the last digit of the correct code is entered. There is no "enter" key. When you enter more than n digits, only the last n digits are significant. For example (in the 4-digit version), if the correct code is 4567, and you plan to enter the digit sequence 1234567890, the door will open as soon as you press the 7 key. 

The software to create this effect is rather simple. In the n-digit version the safe is always in one of 10 n-1 internal states. The current state of the safe simply represents the last n-1 digits that have been entered. One of these states (in the example above, state 456) is marked as the unlocked state. If the safe is in the unlocked state and then the right key (in the example above, 7) is pressed, the door opens. Otherwise the safe shifts to the corresponding new state. For example, if the safe is in state 456 and then you press 8, the safe goes into state 568. 

A trivial strategy to open the safe is to enter all possible codes one after the other. In the worst case, however, this will require n * 10 n keystrokes. By choosing a good digit sequence it is possible to open the safe in at most 10 n + n - 1 keystrokes. All you have to do is to find a digit sequence that contains all n-digit sequences exactly once. KEY Inc. claims that for the military version (n=6) the fastest computers available today would need billions of years to find such a sequence - but apparently they don't know what some programmers are capable of...

Input

The input contains several test cases. Every test case is specified by an integer n. You may assume that 1<=n<=6. The last test case is followed by a zero.

Output

For each test case specified by n output a line containing a sequence of 10 n + n - 1 digits that contains each n-digit sequence exactly once.

Sample Input

1
2
0

Sample Output

0123456789
00102030405060708091121314151617181922324252627282933435363738394454647484955657585966768697787988990

Source

Ulm Local 2004

题意:

破解一套1-6位长度密码的系统,寻找这样一个序列:对于N位的密码10^N+N-1长度的连续的长为N的串能够枚举完所有的密码。


思路:

建图后用非递归dfs(递归的dfs会爆栈),每次去环,如果到一点走不动了,则将这点为最后回到原点的路径,用另一个数组存起来(实际上对于这题,就是从n个9到n的0的路径),也可以不用保存,直接在后面添n-1个0就够了


感想:

这题如果不爆栈的话,直接用dfs都可以过得。但是系统栈太小了,没办法,纠结了好久写非递归的dfs,开始不想建图,写非递归dfs,发现怎么都处理不好,无赖,只好建图再dfs了。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#define maxn 1000050
using namespace std;

int n,m,ans,cnt,pos,pos1,xxc;
char s[maxn],s1[maxn];
bool vis[maxn];
int fac[10];
int pp[100005];
struct Node
{
    int r,next;
}edge[maxn];

void addedge(int u,int v)   //建图
{
    cnt++;
    edge[cnt].r=v;
    edge[cnt].next=pp[u];
    pp[u]=cnt;
}
void dfs()   // 非递归dfs
{
    int i,j,t,nx,flag;
    xxc=0;
    pos=pos1=-1;
    memset(vis,0,sizeof(vis));
    i=n-1;
    while(i--) s[++pos]='0';
    while(1)
    {
        if(xxc>=cnt) break ;  // 所有边访问完后就退出
        t=0;
        for(i=pos-n+2;i<=pos;i++)
        {
            t=t*10+s[i]-'0';
        }
        nx=t;
        flag=0;
        for(i=pp[nx];i;i=edge[i].next)
        {
            if(!vis[i])
            {
                xxc++;
                vis[i]=flag=1;
                s[++pos]=edge[i].r%10+'0';
            }
            if(flag) break ;
        }
        if(!flag)//发现走到这里不能走了则回去 此点为最后回到原点的路径 用数组保存下来
        {
            s1[++pos1]=s[pos];
            pos--;
        }
    }
}
int main()
{
    int i,j,u,v;
    fac[1]=10;
    for(i=2; i<=6; i++)
    {
        fac[i]=fac[i-1]*10;
    }
    while(scanf("%d",&n),n)
    {
        if(n==1)
        {
            printf("0123456789\n");
            continue ;
        }
        cnt=0;
        m=fac[n-1];
        memset(pp,0,sizeof(pp));
        for(i=0;i<m;i++)
        {
            for(j=9;j>=0;j--)    // 这样建边使得字典序最小
            {
                u=i;
                v=i%(m/10)*10+j;
                addedge(u,v);
            }
        }
        dfs();
        s[++pos]='\0';
        printf("%s",s);
        for(i=pos1;i>=0;i--)
        {
            printf("%c",s1[i]);
        }
        printf("\n");
    }
    return 0;
}




你可能感兴趣的:(poj 1780 Code (欧拉回路+非递归dfs))