forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.cpp
62 lines (62 loc) · 942 Bytes
/
code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// 2008-06-22
#include <cstdio>
#ifdef _MSC_VER
#define PC putchar
#else
#define PC putchar_unlocked
#endif
static int adjlist[100000][10];
static int list[1100000];
int listsize;
void find_tour(int v)
{
bool b=false;
int i,j;
for(;;)
{
i=0;
bool found=false;
while (i<10&&!found)
{
j=adjlist[v][i];
if (j>=0)
found=true;
i++;
}
if (!found) break;
adjlist[v][i-1]=-1;
find_tour(j);
}
list[listsize++]=v%10;
}
int main()
{
int n,i,j;
//freopen("code.out","w",stdout);
for(;;)
{
scanf("%d",&n);
if (n==0)
return 0;
if (n==1)
{
printf("0123456789\n");
continue;
}
int p10=1;
for (i=1; i<n; i++)
p10*=10;
//use this many nodes, and create adjlists
for (i=0; i<p10; i++)
for (j=0; j<10; j++)
adjlist[i][j]=10*(i%(p10/10))+j;
listsize=0;
find_tour(0);
for (i=0; i<n-2; i++)
PC('0');
for (i=listsize-1; i>=0; i--)
PC(list[i]+'0');
PC('\n');
}
return 0;
}