kk Blog —— 通用基础


date [-d @int|str] [+%s|"+%F %T"]
netstat -ltunp
sar -n DEV 1

插头 DP code5-6

五、zju_3256
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include<stdio.h>

#define N 7000 // 3^(n+1)
#define M 333  // all = 300+

int n,m;
int val[33],s[33],b[33],all,r[M],e[N];
int p[M][13],kk,H[M][13];

int A[M][M],B[M][M],tmp[M][M];

void mul(int A[M][M], int B[M][M])
{
	int i,j,k;
	long long w;
	for(i=0;i<all;i++)
	    for(j=0;j<all;j++)
	    {
	        w = 0;
	        for(k=0;k<all;k++) if(A[i][k] != 0 && B[k][j] != 0) w += (long long)A[i][k]*(long long)B[k][j];
	        if(w > 7777777) w = w-w/7777777*7777777;
	        tmp[i][j] = (int)(w);
	    }
	for(i=0;i<all;i++)
	    for(j=0;j<all;j++) A[i][j] = tmp[i][j];
}

int ok()
{
	int i,l,c[33];
	l = 0;
	for(i=1;i<=n+1;i++)
	{
	    b[i] = -1;
	    if(s[i] == 1) c[++l] = i;
	    else
	    if(s[i] == 2) {
	        if(l == 0) return 0;
	        b[c[l]] = i; b[i] = c[l]; l--;
	    }
	}
	if(l != 0) return 0;
	return 1;
}

void init()
{
	int i,j;
	val[1] = 1;
	for(i=2;i<=n+2;i++) val[i] = val[i-1]*3;

	for(i=0;i<=n+2;i++) s[i] = 0;

	all = 0;
	for(i=0;i<val[n+2];i++)
	{
	    e[i] = -1;
	    if(ok() == 1)
	    {
	        for(j=1;j<=n+1;j++) p[all][j] = b[j];
	        r[all] = i; e[i] = all;
	        all++;
	    }
	    s[1]++;
	    j = 1; while(s[j] > 2) { s[j] = 0; j++; s[j]++; }
	}
}

void abc()
{
	int i,j,k,LL,UU,sta;
	for(i=0;i<all;i++) for(j=0;j<all;j++) A[i][j] = (i==j)?1:0;

	for(k=0;k<all;k++) for(i=1;i<=n+1;i++) H[k][i] = r[k]/val[i]%3;

	for(i=1;i<=n;i++)
	{
	    for(j=0;j<all;j++) for(k=0;k<all;k++) B[j][k] = 0;

	    for(k=0;k<all;k++)
	    {
	        LL = H[k][i]; UU = H[k][i+1];

	        if(LL == 0 && UU == 0)
	        {
	            sta = r[k] + val[i] + val[i+1]*2;
	            if(e[sta] != -1) B[e[sta]][k] += 1;
	        }
	        else
	        if(LL == 0)
	        {
	            sta = r[k];
	            if(e[sta] != -1) B[e[sta]][k] += 1;

	            sta = r[k] + UU*(val[i]-val[i+1]);
	            if(e[sta] != -1) B[e[sta]][k] += 1;
	        }
	        else
	        if(UU == 0)
	        {
	            sta = r[k];
	            if(e[sta] != -1) B[e[sta]][k] += 1;

	            sta = r[k] + LL*(val[i+1]-val[i]);
	            if(e[sta] != -1) B[e[sta]][k] += 1;
	        }
	        else
	        if(LL == 2 && UU == 1)
	        {
	            sta = r[k] - val[i]*2 - val[i+1];
	            if(e[sta] != -1) B[e[sta]][k] += 1;
	        }
	        else
	        if(LL == 1 && UU == 1)
	        {
	            if(p[k][i+1] != -1)
	            {
	                sta = r[k]-val[i]-val[i+1]-val[p[k][i+1]];
	                if(e[sta] != -1) B[e[sta]][k] += 1;
	            }
	        }
	        else
	        if(LL == 2 && UU == 2)
	        {
	            if(p[k][i] != -1)
	            {
	                sta = r[k]-val[i]*2-val[i+1]*2+val[p[k][i]];
	                if(e[sta] != -1) B[e[sta]][k] += 1;
	            }
	        }
	    }

	    mul(B, A);
	    for(j=0;j<all;j++) for(k=0;k<all;k++) A[j][k] = B[j][k];
	}
	// change
	for(i=0;i<all;i++) for(j=0;j<all;j++)
	{
	    B[i][j] = 0;
	    if(e[r[i]/3] != -1)
	        B[i][j] = A[e[r[i]/3]][j];
	}

	int q[M],al=0;
	for(i=0;i<all;i++) if(r[i]%3 == 0) q[al++] = r[i];

	for(i=0;i<al;i++) for(j=0;j<al;j++)
	{
	    A[i][j] = B[e[q[i]]][e[q[j]]];
	}
	all = al;
	for(kk=0;;kk++) if(q[kk] == 3+val[n+1]*2) break;

}

void solve()
{
	int i,j;
	for(i=0;i<all;i++) for(j=0;j<all;j++) B[i][j]=(i==j)?1:0;
	while(m > 0)
	{
	    if(m%2 == 1) mul(B, A);
	    mul(A, A);
	    m /= 2;
	}

	if(B[kk][0] == 0)
	    printf("Impossible\n");
	else
	    printf("%d\n",B[kk][0]);
}

int main()
{
	while(scanf("%d %d",&n,&m) != EOF)
	{
	    init();
	    abc();
	    solve();
	}
	return 0;
}

六、hdu_1693
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
#include<stdio.h>
#include<string.h>

#define N 5000 // 2^(m+1)

long long dp[2][N];
int H[N][15];

class DP {

public:
	int cas;
	DP() {
	    cas = 0;
	}

	int n,m,can[33][33];
	int val[33],LL,UU,sta,next;

	void input()
	{
	    int i,j;
	    scanf("%d %d",&n,&m);
	    for(i=1;i<=n;i++)
	        for(j=1;j<=m;j++) scanf("%d", &can[i][j]);
	}

	void abc()
	{
	    int i,j,k,u,y;

	    input();

	    u = 0;
	    for(i=0;i<=(1<<(m+1));i++) dp[u][i] = 0;
	    dp[u][0] = 1;

	    for(i=1;i<=n;i++)
	        for(j=1;j<=m;j++)
	        {
	            y = u; u = 1-u;
	            if(j == 1)
	            {
	                for(k=(1<<(m+1))-1;k>=0;k--)
	                {
	                    dp[y][k] = dp[y][k/2];
	                    if(k%2==1) dp[y][k] = 0;
	                }
	            }

	            for(k=0;k<(1<<(m+1));k++) dp[u][k] = 0;

	            for(k=0;k<(1<<(m+1));k++)
	            if(dp[y][k] > 0)
	            {
	                LL = H[k][j];
	                UU = H[k][j+1];

	                if(can[i][j] == 0)               // sta = 0
	                {
	                    if(LL == 0 && UU == 0)
	                    {
	                        dp[u][k] += dp[y][k];
	                    }
	                    continue;
	                }
	                                                 // sta = 1;
	                if(UU == 0 && LL == 0)
	                {
	                    sta = k+(1<<(j-1))+(1<<j);
	                    dp[u][sta] += dp[y][k];
	                }
	                else
	                if(LL == 0)
	                {
	                    sta = k;
	                    dp[u][sta] += dp[y][k];

	                    sta = k+((1<<(j-1)) - (1<<j));
	                    dp[u][sta] += dp[y][k];
	                }
	                else
	                if(UU == 0)
	                {
	                    sta = k;
	                    dp[u][sta] += dp[y][k];

	                    sta = k+(-(1<<(j-1))+(1<<j));
	                    dp[u][sta] += dp[y][k];
	                }
	                else
	                if(LL == 1 && UU == 1)
	                {
	                    sta = k-(1<<(j-1))-(1<<j);
	                    dp[u][sta] += dp[y][k];
	                }
	            }
	        }
	        cas++;
	        printf("Case %d: There are %lld ways to eat the trees.\n",cas,dp[u][0]);
	}

	void solve()
	{
	    int k,j,T;

	    for(k=0;k<(1<<(11+1));k++)
	        for(j=1;j<=11+1;j++)
	            H[k][j] = k/(1<<(j-1))%2;

	    scanf("%d",&T);
	    while(T-- > 0)
	        abc();
	}
};

int main()
{
	    DP dp;
	    dp.solve();
}

插头 DP code3-4

三、pku_1739
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include<stdio.h>

class DP {

public:
	int n,m,can[33][33];
	int dp[200000],pre[200000],val[33],LL,UU,sta,next;
	int a[33],b[33],d[33],c[33];
	char p[200000][12];

	void input()
	{
	    int i,j;
	    char ch[13];

	    for(i=1;i<=n;i++)
	    {
	        scanf("%s",ch);
	        for(j=1;j<=m;j++) can[i][j] = ch[j-1]=='.'?1:0;
	    }
	}

	int ok()
	{
	    int i,l;
	    l = 0;
	    for(i=1;i<=m+1;i++)
	    {
	        b[i] = d[i] = -1;
	        if(a[i] == 1) c[++l] = i;
	        else
	        if(a[i] == 2)
	        {
	            if(l == 0) return 0;
	            b[c[l]] = i; d[i] = c[l];
	            l--;
	        }
	    }
	    if(l != 0) return 0;
	    return 1;
	}

	void init()
	{
	    int i,j,k;

	    val[1] = 1;
	    for(i=2;i<=m+2;i++) val[i] = val[i-1]*3;

	    for(i=0;i<=m+2;i++) a[i] = 0;
	    a[1] = -1;

	    for(i=0;i<=val[m+2];i++)
	    {
	        a[1]++;
	        k = 1; while(k <= m+1 && a[k]>2) { a[k]%=3; a[k+1]++; k++; }

	        if(ok() == 0) continue;

	        for(j=1;j<=m+1;j++) {
	            p[i][j] = -1;
	            if(b[j] != -1) p[i][j] = b[j];
	            if(d[j] != -1) p[i][j] = d[j];
	        }
	    }
	}

	void abc()
	{
	    int i,j,k;

	    input();
	    init();

	    for(i=0;i<=val[m+2];i++) dp[i] = 0;

	    dp[0] = 1;

	    for(i=1;i<=n;i++)
	        for(j=1;j<=m;j++)
	        {
	            if(j == 1)
	            {
	                for(k=val[m+2]-1;k>=0;k--)
	                {
	                    dp[k] = dp[k/3];
	                    if(k%3 != 0) dp[k] = 0;
	                }
	            }

	            for(k=0;k<val[m+2];k++)
	            {
	                pre[k] = dp[k];
	                dp[k] = 0;
	            }

	            for(k=0;k<val[m+2];k++)
	            if(pre[k] > 0)
	            {
	                LL = k/val[j]%3;
	                UU = k/val[j+1]%3;

	                if(can[i][j] == 0)
	                {
	                    if(LL == 0 && UU == 0)
	                    {
	                        dp[k] += pre[k];
	                    }
	                    continue;
	                }


	                if(UU == 0 && LL == 0)
	                {
	                    sta = k+val[j]+val[j+1]+val[j+1];

	                    dp[sta] += pre[k];
	                }
	                else
	                if(LL == 0)
	                {
	                    sta = k;
	                    dp[sta] += pre[k];

	                    sta = k+k/val[j+1]%3*(val[j]-val[j+1]);
	                    dp[sta] += pre[k];
	                }
	                else
	                if(UU == 0)
	                {
	                    sta = k;
	                    dp[sta] += pre[k];

	                    sta = k+k/val[j]%3*(-val[j]+val[j+1]);
	                    dp[sta] += pre[k];
	                }
	                else
	                if(LL == 2 && UU == 1)
	                {
	                    sta = k-val[j]-val[j]-val[j+1];
	                    dp[sta] += pre[k];
	                }
	                else
	                if(LL == 1 && UU == 1)
	                {
	                    if(p[k][j+1] > 0 && p[k][j+1] <= m+1)
	                    {
	                        sta = k-val[j]-val[j+1]-val[p[k][j+1]];
	                        dp[sta] += pre[k];
	                    }
	                }
	                else
	                if(LL == 2 && UU == 2)
	                {
	                    if(p[k][j] > 0)
	                    {
	                        sta = k-val[j]-val[j]-val[j+1]-val[j+1]+val[p[k][j]];
	                        dp[sta] += pre[k];
	                    }
	                }
	            }
	        }
	    printf("%d\n",dp[1+val[m]*2]);
	}

	void solve()
	{
	    while(true)
	    {
	        scanf("%d %d",&n,&m);
	        if(n == 0 && m == 0) break;
	        abc();
	    }
	}
};

int main() {
	    DP dp;
	    dp.solve();
	    return 0;
}

四、hdu_3377
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include<stdio.h>

int n,m,can[33][33];
int dp[200000],pre[200000],val[33],LL,UU,sta,next;
int a[33],b[33],d[33],c[33],cas=0;
char p[200000][12];


class DP {

public:
	void input()
	{
	    int i,j;

	    for(i=1;i<=n;i++)
	        for(j=1;j<=m;j++) scanf("%d",&can[i][j]);
	}

	int ok()
	{
	    int i,l;
	    l = 0;
	    for(i=1+m;i>=1;i--)
	    {
	        b[i] = d[i] = -1;
	        if(a[i] == 2) c[++l] = i;
	        else
	        if(a[i] == 1)
	        {
	            if(l == 0) return 0;
	            d[c[l]] = i; b[i] = c[l];
	            l--;
	        }
	    }
	    if(l != 1) return 0;
	    return 1;
	}

	void init()
	{
	    int i,j,k;

	    for(i=0;i<=12;i++) a[i] = 0;
	    a[1] = -1;

	    for(i=0;i<=val[m+2];i++)
	    {
	        a[1]++;
	        k = 1; while(k <= m+1 && a[k]>2) { a[k]%=3; a[k+1]++; k++; }

	        if(ok() == 0) continue;

	        for(j=1;j<=m+1;j++) {
	            p[i][j] = -1;
	            if(b[j] != -1) p[i][j] = b[j];
	            if(d[j] != -1) p[i][j] = d[j];
	        }
	    }
	}

	void abc()
	{
	    int i,j,k,ans=-1000000000;

	    val[1] = 1;
	    for(i=2;i<=12;i++) val[i] = val[i-1]*3;

	    input();
	    init();

	    for(i=0;i<=val[m+2];i++) dp[i] = -1000000000;

	    //dp[0] = 0;

	    for(i=1;i<=n;i++)
	        for(j=1;j<=m;j++)
	        {
	            if(j == 1)
	            {
	                for(k=val[m+2]-1;k>=0;k--)
	                {
	                    dp[k] = dp[k/3];
	                    if(k%3 != 0) dp[k] = -1000000000;
	                }
	            }

	            for(k=0;k<val[m+2];k++)
	            {
	                pre[k] = dp[k];
	                dp[k] = -1000000000;
	            }

	            if(i == 1 && j == 1)
	            {
	                dp[2] = dp[6] = can[1][1];
	                continue;
	            }

	            if(i == n && j == m)
	            {
	                k = val[m]*2;
	                if(ans < pre[k]+can[n][m])
	                        ans = pre[k]+can[n][m];

	                k = val[m+1]*2;
	                if(ans < pre[k]+can[n][m])
	                        ans = pre[k]+can[n][m];
	                continue;
	            }

	            for(k=0;k<val[m+2];k++)
	            if(pre[k] > -1000000000)
	            {
	                LL = k/val[j]%3;
	                UU = k/val[j+1]%3;

	                if(UU == 0 && LL == 0)
	                {
	                    sta = k+val[j]+val[j+1]+val[j+1];

	                    if(pre[k] + can[i][j] > dp[sta])
	                        dp[sta] = pre[k] + can[i][j];

	                    if(dp[k] < pre[k]) dp[k] = pre[k];
	                }
	                else
	                if(LL == 0)
	                {
	                    sta = k;
	                    if(pre[k] + can[i][j] > dp[sta])
	                        dp[sta] = pre[k] + can[i][j];

	                    sta = k+k/val[j+1]%3*(val[j]-val[j+1]);
	                    if(pre[k] + can[i][j] > dp[sta])
	                        dp[sta] = pre[k] + can[i][j];
	                }
	                else
	                if(UU == 0)
	                {
	                    sta = k;
	                    if(pre[k] + can[i][j] > dp[sta])
	                        dp[sta] = pre[k] + can[i][j];

	                    sta = k+k/val[j]%3*(-val[j]+val[j+1]);
	                    if(pre[k] + can[i][j] > dp[sta])
	                        dp[sta] = pre[k] + can[i][j];
	                }
	                else
	                if(LL == 2 && UU == 1)
	                {
	                    sta = k-val[j]-val[j]-val[j+1];
	                    if(pre[k] + can[i][j] > dp[sta])
	                        dp[sta] = pre[k] + can[i][j];
	                }
	                else
	                if(LL == 1 && UU == 1)
	                {
	                    if(p[k][j+1] > 0 && p[k][j+1] <= m+1)
	                    {
	                        sta = k-val[j]-val[j+1]-val[p[k][j+1]];
	                        if(pre[k] + can[i][j] > dp[sta])
	                          dp[sta] = pre[k] + can[i][j];
	                    }
	                }
	                else
	                if(LL == 2 && UU == 2)
	                {
	                    if(p[k][j] > 0)
	                    {
	                        sta = k-val[j]-val[j]-val[j+1]-val[j+1]+val[p[k][j]];
	                        if(pre[k] + can[i][j] > dp[sta])
	                            dp[sta] = pre[k] + can[i][j];
	                    }
	                }
	            }
	        }
	    if(n == 1 && m == 1) ans = can[1][1];

	    printf("Case %d: %d\n",++cas,ans);
	}

	void solve()
	{
	    while(scanf("%d %d",&n,&m) != EOF)
	    {
	        abc();
	    }
	}
};

int main() {
	    DP dp;
	    dp.solve();
	    return 0;

}

插头 DP code1-2

一、hdu_1964
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include<stdio.h>
#include<string.h>
#include<map>

int n,m,up[33][33],le[33][33];
int dp[200000],pre[200000],val[33],LL,UU,sta,next;
int a[33],b[33],d[33],c[33];
char p[200000][12];

int ok()
{
	int i,l;
	l = 0;
	for(i=1;i<=m+1;i++)
	{
	    b[i] = d[i] = -1;
	    if(a[i] == 1) c[++l] = i;
	    else
	    if(a[i] == 2)
	    {
	        if(l == 0) return 0;
	        b[c[l]] = i; d[i] = c[l];
	        l--;
	    }
	}
	if(l != 0) return 0;
	return 1;
}

void init()
{
	int i,j,k;

	val[1] = 1;
	for(i=2;i<=12;i++) val[i] = val[i-1]*3;

	for(i=0;i<=12;i++) a[i] = 0;
	a[1] = -1;

	for(i=0;i<=val[m+2];i++)
	{
	    a[1]++;
	    k = 1; while(k <= m+1 && a[k]>2) { a[k]%=3; a[k+1]++; k++; }

	    if(ok() == 0) continue;

	    for(j=1;j<=m+1;j++) {
	        p[i][j] = -1;
	        if(b[j] != -1) p[i][j] = b[j];
	        if(d[j] != -1) p[i][j] = d[j];
	    }
	}
}

void abc()
{
	int i,j,k,ans = 1000000;

	for(i=0;i<=val[m+2];i++) dp[i] = 1000000;

	dp[0] = 0;

	for(i=1;i<=n;i++)
	    for(j=1;j<=m;j++)
	    {
	        if(j == 1)
	        {
	            for(k=val[m+2]-1;k>=0;k--)
	            {
	                dp[k] = dp[k/3];
	                if(k%3 != 0) dp[k] = 1000000;
	            }
	        }

	        for(k=0;k<val[m+2];k++)
	        {
	            pre[k] = dp[k]; dp[k] = 1000000;
	        }

	        for(k=0;k<val[m+2];k++)
	        if(pre[k] < 1000000)
	        {
	            LL = k/val[j]%3;
	            UU = k/val[j+1]%3;

	            if(UU == 0 && LL == 0)
	            {
	                sta = k+val[j]+val[j+1]+val[j+1];
	                next = pre[k]+le[i][j+1]+up[i+1][j];
	                if(dp[sta] > next) dp[sta] = next;
	            }
	            else
	            if(LL == 0)
	            {
	                sta = k;
	                next = pre[k]+le[i][j+1];
	                if(dp[sta] > next) dp[sta] = next;

	                sta = k+k/val[j+1]%3*(val[j]-val[j+1]);
	                next = pre[k]+up[i+1][j];
	                if(dp[sta] > next) dp[sta] = next;
	            }
	            else
	            if(UU == 0)
	            {
	                sta = k;
	                next = pre[k]+up[i+1][j];
	                if(dp[sta] > next) dp[sta] = next;

	                sta = k+k/val[j]%3*(-val[j]+val[j+1]);
	                next = pre[k]+le[i][j+1];
	                if(dp[sta] > next) dp[sta] = next;
	            }
	            else
	            if(LL == 2 && UU == 1)
	            {
	                sta = k-val[j]-val[j]-val[j+1];
	                next = pre[k];
	                if(dp[sta] > next) dp[sta] = next;
	            }
	            else
	            if(LL == 1 && UU == 1)
	            {
	                if(p[k][j+1] > 0 && p[k][j+1] <= m+1)
	                {
	                    sta = k-val[j]-val[j+1]-val[p[k][j+1]];
	                    next = pre[k];
	                    if(dp[sta] > next) dp[sta] = next;
	                }
	            }
	            else
	            if(LL == 2 && UU == 2)
	            {
	                if(p[k][j] > 0)
	                {
	                    sta = k-val[j]-val[j]-val[j+1]-val[j+1]+val[p[k][j]];
	                    next = pre[k];
	                    if(dp[sta] > next) dp[sta] = next;
	                }
	            }
	            else
	            if(LL == 1 && UU == 2)
	            {
	                if(i == n && j == m)
	                {
	                    sta = k-val[j]-val[j+1]-val[j+1];
	                    next = pre[k];

	                    if(dp[sta] > next) dp[sta] = next;

	                    if(dp[sta] < ans) ans = dp[sta];
	                }
	            }

	        }
	    }

	printf("%d\n",ans);
}

int main()
{
	int i,j,T;
	char ch[1111];

	scanf("%d",&T);
	while(T--)
	{
	    scanf("%d %d",&n,&m);
	    gets(ch);
	    gets(ch);

	    for(i=0;i<=n+1;i++) for(j=0;j<=m+1;j++) up[i][j] = le[i][j] = 1000000;

	    for(i=1;i<=n;i++)
	    {
	        gets(ch);
	        for(j=2;j<=m;j++) le[i][j] = ch[j+j-2]-48;
	        gets(ch);

	        if(i<n)
	        {
	            for(j=1;j<=m;j++) up[i+1][j] = ch[j+j-1]-48;
	        }
	    }
	    init();
	    abc();
	}
	return 0;
}

二、timus_1519
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include<stdio.h>

long long dp[60000],pre[60000];
int all,r[60000],e[1600000];
int p[60000][13];

class DP {

public:
	int n,m,can[33][33],last;
	int val[33],LL,UU,sta,next;
	int a[33],b[33],d[33],c[33];

	void input()
	{
	    int i,j;
	    char ch[15];
	    last = 0;
	    for(i=1;i<=n;i++)
	    {
	        scanf("%s",ch);
	        for(j=1;j<=m;j++)
	        {
	            can[i][j] = ch[j-1]=='.'?(++last):-1;
	        }
	    }
	}

	int ok()
	{
	    int i,l;
	    l = 0;
	    for(i=1;i<=m+1;i++)
	    {
	        b[i] = d[i] = -1;
	        if(a[i] == 1) c[++l] = i;
	        else
	        if(a[i] == 2)
	        {
	            if(l == 0) return 0;
	            b[c[l]] = i; d[i] = c[l];
	            l--;
	        }
	    }
	    if(l != 0) return 0;
	    return 1;
	}

	void init()
	{
	    int i,j,k;

	    val[1] = 1;
	    for(i=2;i<=m+2;i++) val[i] = val[i-1]*3;;

	    for(i=0;i<=m+2;i++) a[i] = 0;
	    a[1] = -1;

	    all = 0;

	    for(i=0;i<=val[m+2];i++)
	    {
	        a[1]++;
	        k = 1; while(k <= m+1 && a[k]>2) { a[k]%=3; a[k+1]++; k++; }

	        e[i] = -1;

	        if(ok() == 0) continue;

	        r[all] = i; e[i] = all;

	        for(j=1;j<=m+1;j++) {
	            p[all][j] = -1;
	            if(b[j] != -1) p[all][j] = b[j];
	            if(d[j] != -1) p[all][j] = d[j];
	        }

	        all++;
	    }
	}

	void abc()
	{
	    int i,j,k,l;

	    input();
	    init();

	    for(i=0;i<all;i++) dp[i] = 0;

	    dp[r[0]] = 1;

	    for(i=1;i<=n;i++)
	        for(j=1;j<=m;j++)
	        {
	            if(j == 1)
	            {
	                for(k=all-1;k>=0;k--)
	                {
	                    l = r[k]; l = l/3; l = e[l];

	                    if(l != -1)
	                    {
	                        dp[k] = dp[l];
	                        if(r[k]%3 != 0) dp[k] = 0;
	                    }
	                    else
	                        dp[k] = 0;
	                }
	            }

	            for(k=0;k<all;k++)
	            {
	                pre[k] = dp[k]; dp[k] = 0;
	            }

	            for(k=0;k<all;k++)
	            if(pre[k] > 0)
	            {
	                LL = r[k]/val[j]%3;
	                UU = r[k]/val[j+1]%3;

	                if(can[i][j] == -1)
	                {
	                    if(LL == 0 && UU == 0)
	                    {
	                        dp[k] += pre[k];
	                    }
	                    continue;
	                }

	                if(UU == 0 && LL == 0)
	                {
	                    sta = r[k]+val[j]+val[j+1]+val[j+1];

	                    dp[e[sta]] += pre[k];
	                }
	                else
	                if(LL == 0)
	                {
	                    sta = r[k];
	                    dp[e[sta]] += pre[k];

	                    sta = r[k]+r[k]/val[j+1]%3*(val[j]-val[j+1]);
	                    dp[e[sta]] += pre[k];
	                }
	                else
	                if(UU == 0)
	                {
	                    sta = r[k];
	                    dp[e[sta]] += pre[k];

	                    sta = r[k]+r[k]/val[j]%3*(-val[j]+val[j+1]);
	                    dp[e[sta]] += pre[k];
	                }
	                else
	                if(LL == 2 && UU == 1)
	                {
	                    sta = r[k]-val[j]-val[j]-val[j+1];
	                    dp[e[sta]] += pre[k];
	                }
	                else
	                if(LL == 1 && UU == 1)
	                {
	                    if(p[k][j+1] > 0 && p[k][j+1] <= m+1)
	                    {
	                        sta = r[k]-val[j]-val[j+1]-val[p[k][j+1]];
	                        dp[e[sta]] += pre[k];
	                    }
	                }
	                else
	                if(LL == 2 && UU == 2)
	                {
	                    if(p[k][j] > 0)
	                    {
	                        sta = r[k]-val[j]-val[j]-val[j+1]-val[j+1]+val[p[k][j]];
	                        dp[e[sta]] += pre[k];
	                    }
	                }
	                else
	                if(LL == 1 && UU == 2)
	                {
	                    if(can[i][j] == last)
	                    {
	                        sta = r[k]-val[j]-val[j+1]-val[j+1];
	                        dp[e[sta]] += pre[k];
	                    }
	                }
	            }
	        }
	    printf("%lld\n",dp[r[0]]);
	}

	void solve()
	{
	    while(scanf("%d %d",&n,&m) != EOF)
	    {
	        abc();
	    }
	}
};

int main() {
	    DP dp;
	    dp.solve();
	    return 0;
}