-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts.cc
141 lines (131 loc) · 3.45 KB
/
ts.cc
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cinttypes>
#ifndef __NMAX__
#define __NMAX__ 4
#endif
#ifndef __TVAR__
#define __TVAR__ int32_t
#endif
#ifndef __CVAR__
#define __CVAR__ uint32_t
#endif
#ifndef __TRIVIAL__
constexpr bool self_avoiding=true;
#else
constexpr bool self_avoiding=false;
#endif
constexpr __TVAR__ NMAX=__NMAX__;
template<class Y,Y x,Y y> struct grid
{
template<class...Nodes> static constexpr bool value(const Y u,const Y v,const Nodes...nodes)
{
#ifdef __GNUC__
#ifndef __G_SPY__
(void) u;
(void) v;
#endif
#endif
if(value(u,v) or value(nodes...)) return true;
else return false;
}
static constexpr bool value(const Y u,const Y v)
{
#ifdef __GNUC__
#ifndef __G_SPY__
(void) u;
(void) v;
#endif
#endif
if(u==x and v==y) return true;
else return false;
}
};
template<class X,class Y,Y x,Y y,Y n,Y N> struct walker
{
template<class...Nodes> static constexpr X move(X z,const Y u,const Y v,const Nodes...nodes)
{
#ifdef __GNUC__
#ifndef __G_SPY__
(void) u;
(void) v;
#endif
#endif
if constexpr(n==N) return z+static_cast<X>(1);
else if constexpr(self_avoiding)
{
if(!grid<Y,x+1,y>::value(u,v,nodes...)) z=walker<X,Y,x+1,y,n+1,N>::move(z,x,y,u,v,nodes...);
if(!grid<Y,x-1,y>::value(u,v,nodes...)) z=walker<X,Y,x-1,y,n+1,N>::move(z,x,y,u,v,nodes...);
if(!grid<Y,x,y+1>::value(u,v,nodes...)) z=walker<X,Y,x,y+1,n+1,N>::move(z,x,y,u,v,nodes...);
if(!grid<Y,x,y-1>::value(u,v,nodes...)) z=walker<X,Y,x,y-1,n+1,N>::move(z,x,y,u,v,nodes...);
return z;
}
else
{
z=walker<X,Y,x+1,y,n+1,N>::move(z,x,y,u,v,nodes...);
z=walker<X,Y,x-1,y,n+1,N>::move(z,x,y,u,v,nodes...);
z=walker<X,Y,x,y+1,n+1,N>::move(z,x,y,u,v,nodes...);
z=walker<X,Y,x,y-1,n+1,N>::move(z,x,y,u,v,nodes...);
return z;
}
}
static constexpr X move(X z,const Y u,const Y v)
{
#ifdef __GNUC__
#ifndef __G_SPY__
(void) u;
(void) v;
#endif
#endif
if constexpr(n==N) return z+static_cast<X>(1);
else if constexpr(self_avoiding)
{
if(!grid<Y,x+1,y>::value(u,v)) z=walker<X,Y,x+1,y,n+1,N>::move(z,x,y,u,v);
if(!grid<Y,x-1,y>::value(u,v)) z=walker<X,Y,x-1,y,n+1,N>::move(z,x,y,u,v);
if(!grid<Y,x,y+1>::value(u,v)) z=walker<X,Y,x,y+1,n+1,N>::move(z,x,y,u,v);
if(!grid<Y,x,y-1>::value(u,v)) z=walker<X,Y,x,y-1,n+1,N>::move(z,x,y,u,v);
return z;
}
else
{
z=walker<X,Y,x+1,y,n+1,N>::move(z,x,y,u,v);
z=walker<X,Y,x-1,y,n+1,N>::move(z,x,y,u,v);
z=walker<X,Y,x,y+1,n+1,N>::move(z,x,y,u,v);
z=walker<X,Y,x,y-1,n+1,N>::move(z,x,y,u,v);
return z;
}
}
static constexpr X move(X z)
{
if constexpr(n==N) return z+static_cast<X>(1);
else
{
z=walker<X,Y,x+1,y,n+1,N>::move(z,x,y);
z=walker<X,Y,x-1,y,n+1,N>::move(z,x,y);
z=walker<X,Y,x,y+1,n+1,N>::move(z,x,y);
z=walker<X,Y,x,y-1,n+1,N>::move(z,x,y);
return z;
}
}
};
template<class X,class Y,Y N> struct saw
{
const X z;
constexpr saw():z(walker<X,Y,NMAX,NMAX,static_cast<Y>(0),N>::move(static_cast<X>(0))){}
};
template<class X,class Y,Y N> struct saws
{
saws()
{
if constexpr(N>static_cast<Y>(0)) saws<X,Y,N-1> tmp;
constexpr saw<X,Y,N> sw;
std::printf("%" PRIuMAX ";%" PRIuMAX "\n",static_cast<uintmax_t>(N),static_cast<uintmax_t>(sw.z));
}
};
int main()
{
std::printf("#N[1];Z[2]\n");
saws<__CVAR__,__TVAR__,NMAX-1> sws;
return EXIT_SUCCESS;
}