HDU 4796 Winter's Coming (插头DP)

AC 了。 一个地方写错,导致产生了多余的环!!!!! 13年长沙的插头DP题。 //写了一下插头DP, WA到死,然后这题有天坑!!!! //留个坑吧! 记录下自己写的插头DP代码! 当年写得很熟悉的插头DP现在都快忘记了。 已经AC

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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* ***************

Author :kuangbin

Created Time :2014/9/8 19:22:58

File Name :tmp.cpp

************************************************ */

#include <stdio.h>

#include <string.h>

#include <iostream>

#include <algorithm>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <string>

#include <math.h>

#include <stdlib.h>

#include <time.h>

using namespace std;

const int MAXD = 15;

const int STATE = 1000010;

const int HASH = 10007;

int N,M;

int maze[24][MAXD];//-1禁止,-2左边,-3右边

int code[MAXD];

int ch[MAXD];

struct HASHMAP{

int head[HASH],next[STATE],size;

long long state[STATE];

int f[STATE];

void init(){

size = 0;

memset(head,-1,sizeof(head));

}

void push(long long st,int ans){

int h = st%HASH;

for(int i = head[h];i != -1;i = next[i])

if(state[i] == st){

f[i] = min(f[i],ans);

return;

}

state[size] = st;

f[size] = ans;

next[size] = head[h];

head[h] = size++;

}

}hm[2];

void decode(int *code,int m,long long st){

for(int i = m;i >= 0;i--){

code[i] = st&7;

st >>= 3;

}

}

long long encode(int *code,int m){

int cnt = 1;

memset(ch,-1,sizeof(ch));

ch[0] = 0;

long long st = 0;

for(int i = 0;i <= m;i++){

if(ch[code[i]] == -1)ch[code[i]] = cnt++;

code[i] = ch[code[i]];

st <<= 3;

st |= code[i];

}

return st;

}

void dpblank(int i,int j,int cur){

int k,left,up;

for(k = 0;k < hm[cur].size;k++){

decode(code,M,hm[cur].state[k]);

left = code[j-1];

up = code[j];

if(left && up){

if(left == up){

continue;//这种情况不能合并,否则会形成多余的环

code[j-1] = code[j] = 0;

hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]+maze[i][j]);

}

else {

code[j-1] = code[j] = 0;

for(int t = 0;t <= M;t++)

if(code[t] == up)

code[t] = left;

hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]+maze[i][j]);

}

}

else if(left || up){

int t ;

if(left) t = left;

else t = up;

if(i == N){

int cc = 0;

for(int p = 0;p < j-1;p++)

if(code[p])

cc++;

if(cc == 0){

code[j] = 0;

code[j-1] = t;

hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]+maze[i][j]);

}

}

else if(maze[i+1][j] >= 0){

code[j] = 0;

code[j-1] = t;

hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]+maze[i][j]);

}

if(maze[i][j+1] >= 0){

code[j-1] = 0;

code[j] = t;

hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]+maze[i][j]);

}

}

else {

code[j-1] = code[j] = 0;

hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]);

if(i == N){

int cc = 0;

for(int p = 0;p < j-1;p++)

if(code[p])

cc++;

if(cc == 0 && maze[i][j+1] >= 0){

code[j] = code[j-1] = 13;

hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]+maze[i][j]);

}

}

else if(maze[i+1][j] >= 0 && maze[i][j+1] >= 0){

code[j] = code[j-1] = 13;

hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]+maze[i][j]);

}

}

}

}

void dpblock(int i,int j,int cur){

int k = 0;

for(int k = 0;k < hm[cur].size;k++){

decode(code,M,hm[cur].state[k]);

if(code[j] || code[j-1])continue;

int cc = 0;

for(int p = 0;p < j-1;p++)

if(code[p])

cc++;

if(maze[i][j] == -2 && cc%2 == 0){

code[j-1] = code[j] = 0;

hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]);

}

else if(maze[i][j] == -3 && cc%2 == 1){

code[j-1] = code[j] = 0;

hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]);

}

else if(maze[i][j] == -1){

code[j-1] = code[j] = 0;

hm[cur^1].push(encode(code,j==M?M-1:M),hm[cur].f[k]);

}

}

}

char str[20];

void init(){

memset(maze,-1,sizeof(maze));

for(int i = 1;i <= N;i++){

scanf("%s",str);

for(int j = 1;j <= M;j++){

if(str[j-1] == '#')maze[i][j] = -1;

else if(str[j-1] == 'W')maze[i][j] = -2;

else if(str[j-1] == 'L')maze[i][j] = -3;

else maze[i][j] = str[j-1] - '0';

}

}

}

const int INF = 0x3f3f3f3f;

void solve(){

int i,j,cur = 0;

hm[cur].init();

for(i = 1;i <= M;i++){

if(maze[1][i] < 0)continue;

for(j = 0;j <= M;j++){

if(j == i)code[j] = 1;

else code[j] = 0;

}

hm[cur].push(encode(code,M),0);

}

for(i = 1;i <= N;i++)

for(j = 1;j <= M;j++){

hm[cur^1].init();

if(maze[i][j] >= 0)dpblank(i,j,cur);

else dpblock(i,j,cur);

cur ^= 1;

}

int ans = INF;

for(i = 0;i < hm[cur].size;i++){

decode(code,M,hm[cur].state[i]);

int cc = 0;

for(j = 1;j <= M;j++)

if(code[j])

cc++;

if(cc == 1)ans = min(ans,hm[cur].f[i]);

}

if(ans == INF)ans = -1;

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

}

int main()

{

//freopen("in.txt","r",stdin);

//freopen("out.txt","w",stdout);

while(scanf("%d%d",&N,&M) == 2){

init();

solve();

}

return 0;

}
------ 本文结束------
  • 本文作者: kuangbin
  • 本文链接: 376.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
0%