kk Blog —— 通用基础


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

插头 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;
}

插头 DP

陈丹琦的大作 http://www.docin.com/p-46797997.html

http://acm.hdu.edu.cn/showproblem.php?pid=1964

http://acm.timus.ru/problem.aspx?space=1&num=1519

http://poj.org/problem?id=1739

http://acm.hdu.edu.cn/showproblem.php?pid=3377

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3256

http://acm.hdu.edu.cn/showproblem.php?pid=1693

http://acm.fzu.edu.cn/problem.php?pid=1977

http://poj.org/problem?id=3133

// 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;
}