Hdu 3502 bfs + status compression dp
The meaning is to give you a matrix of n * m each unit has a number, -1 indicates that you cannot go. "= 0 indicates how much energy you can obtain from the upper left corner to the lower right corner. (one unit of energy is consumed by one step)
Idea: First bfs to find the shortest distance between the cable (of course, only the vertex and start point and end point are useful) and then state compression dp to find the maximum energy value here there are several points of attention
The State should reduce the space of the array from 1 (once burst), followed by bfs where only the points with energy are searched;
# Include
# Include
# Include
# Include
Using namespaceStd
;# Define INF 0x3f3f3f
IntMap
[300
] [300
],Dis
[25
] [25
],Mark
[300
] [300
],Leap
[300
] [300
]; IntDir
[4
] [2
] = {0
,1
,0
,-1
,1
,0
,-1
,0
}; IntCoord
[25
] [3
],Dp
[1
<18
] [20
],N
,M
; StructNode
{IntX
,Y
; IntStep
;}A
,B
; IntMin
(IntA
, IntB
) {ReturnA
<B
?A
:B
;} IntBfs
(IntK
) {IntI
;A
.X
=Coord
[K
] [1
];A
.Y
=Coord
[K
] [2
];A
.Step
=0
;Queue
<Node
>Q
;Memset
(Mark
,0
, Sizeof (Mark
));Mark
[A
.X
] [A
.Y
] =1
;Q
.Push
(A
); While (!Q
.Empty
()){B
=Q
.Front
();Q
.Pop
(); (I
=0
;I
<4
;I
++ ){A
.X
=B
.X
+Dir
[I
] [0
];A
.Y
=B
.Y
+Dir
[I
] [1
];A
.Step
=B
.Step
+1
; If (A
.X
<=0
|A
.X
>N
|A
.Y
<=0
|A
.Y
>M
) Continue; if (Map
[A
.X
] [A
.Y
] =-1
) Continue; if (Mark
[A
.X
] [A
.Y
] =0
){Mark
[A
.X
] [A
.Y
] =1
;Dis
[K
] [Leap
[A
.X
] [A
.Y
] =Dis
[Leap
[A
.X
] [A
.Y
] [K
] =A
.Step
;Q
.Push
(A
) ;}} Return0
;} IntMax
(IntA
, IntB
) {ReturnA
>B
?A
:B
;} Int main () {intI
,J
; While (~Scanf
("% D"
,&N
,&M
) {(I
=1
;I
<=N
;I
++) (J
=1
;J
<=M
;J
++ ){Scanf
("% D"
,&Map
[I
] [J
]);Leap
[I
] [J
] =-1
;} If (Map
[1
] [1
] =0
&&(N
>1
|M
>1
)){Printf
("You loss! \ N"
); Continue;} else if (N
=1
&&M
=1
){Printf
("% D \ n"
,Map
[1
] [1
]); Continue;} intT
=-1
; (I
=1
;I
<=N
;I
++) (J
=1
;J
<=M
;J
++) {If (Map
[I
] [J
]>0
){Coord
[++T
] [1
] =I
;Coord
[T
] [2
] =J
;Leap
[I
] [J
] =T
; }} If (Map
[N
] [M
] =0
){Coord
[++T
] [1
] =N
;Coord
[T
] [2
] =M
;Leap
[N
] [M
] =T
;}Memset
(Dis
,INF
, Sizeof (Dis
); (I
=0
;I
<T
;I
++ ){Dis
[I
] [I
] =0
;Bfs
(I
);}Dis
[T
] [T
] =0
;Memset
(Dp
,-1
, Sizeof (Dp
));Dp
[1
] [0
] =Map
[1
] [1
]; IntK
= (1
<(T
+1
); IntMax
=-1
; (I
=1
;I
<K
;I
++) {(J
=0
;J
<=T
;J
++) {If ((I
&(1
<J
) =0
) Continue; if (Dp
[I
] [J
] <0
) Continue; // if this sentence is missing, it times out once for (intX
=0
;X
<=T
;X
++) {If (X
=J
) Continue; if (Dp
[I
] [J
] <Dis
[J
] [X
]) Continue; if ((I
&(1
<X
))! =0
) Continue;Dp
[I
| (1
<X
)] [X
] =Max
(Dp
[I
| (1
<X
)] [X
],Dp
[I
] [J
]-Dis
[X
] [J
] +Map
[Coord
[X
] [1
] [Coord
[X
] [2
]);Max
=Max
(Max
,Dp
[I
| (1
<X
)] [X
]-Dis
[X
] [T
]);// Printf ("^ % d \ n", Max );
}}} If (Max
<0
)Printf
("You loss! \ N"
); ElsePrintf
("% D \ n"
,Max
);} Return0
;}