Hdu4770: Lights against dudely (backtracking + subtraction)

Source: Internet
Author: User

Title: hdu4770: Lights against dudely


The N * M matrix also represents the room. The room is also fragile and sturdy. To protect the vulnerable room, We Need To illuminate every vulnerable room, however, solid rooms do not allow lighting. The lamp is in the L shape, that is, there is a lamp on X and Y, then (X-1, Y) and (X, Y + 1) can be illuminated, of course, this includes (x, y ). The topic also provides a special lamp, which can change the direction, but only one lamp can be used or not used. A room has at most one light. Ask how many lamps are needed to make these fragile rooms light.


Solution: If you have a question about 15 vulnerable rooms at most, you should first list which vulnerable rooms have special lights. special lights need to be enumerated in four directions, after that, they are all common lights. The complexity (15*4*2 ^ 14) is nearly 1000000. In addition, as long as a room is hit, it cannot be illuminated and returns. Considering the shape of a common light, in this way, we start from the lower left corner and go to the upper right corner to get a small number of lights in front of the comparison, then there is a pruning in the backtracing to cut off earlier non-conforming cases. Pruning: if the number of lights in the center is greater than or equal to the minimum, the result can be directly returned.


Note: the coordinates in the four directions of the lamp must be careful. I found the coordinates for one afternoon by mistake. There is also a state that is not marked with a room in the middle: whether the room is illuminated or not. Be careful when you go back to the original value. Because some lights may have been on. The original status is inconsistent. Therefore, records are recorded using the TEM array. If the light is in a solid room or a room where the light is already in place, this is not the case. But the light can shine on the boundary. The hard part is to restore the tracing status. Be careful.


Code:

# Include <stdio. h> # include <string. h> const int n = 205; const int INF = 0x3f3f3f3f; int n, m; char G [N] [N]; int visit [N] [N]; // mark whether the room is illuminated int mm; struct TEM {// The temporary array stores the int X, Y, V;} status to be restored after backtracking ;}; const int dir [4] [2] [2] ={{-}, {0, 1 }}, // Four Directions of the special lamp {1, 0 }, {0, 1 }},{ {0,-1}, {1, 0 }},{ {0,-1 },{-1, 0 }}}; int min (const int X, const int y) {return x <Y? X: Y;} bool influence (int x, int y, int D, TEM * TEM) {// D indicates the direction of the lamp. This function is used to handle the room affected by the lamp in this position. You can determine whether the lamp can be placed in this position. TEM [0]. V = visit [x] [Y]; TEM [0]. X = x; TEM [0]. y = y; visit [x] [Y] = 1; int Nx = x + dir [d] [0] [0]; int ny = Y + dir [d] [0] [1]; If (nx> = 0 & NX <n & ny> = 0 & ny <m) {If (G [NX] [NY] = '#') return false; else {TEM [1]. V = visit [NX] [NY]; TEM [1]. X = NX; TEM [1]. y = NY; visit [NX] [NY] = 1 ;}} int Nx1 = x + dir [d] [1] [0]; int NY1 = Y + dir [d] [1] [1]; if (Nx1> = 0 & Nx1 <n & NY1> = 0 & NY1 <m) {If (G [Nx1] [NY1] = '#') return false; else {TEM [2]. V = visit [Nx1] [NY1]; TEM [2]. X = Nx1; TEM [2]. y = NY1; visit [Nx1] [NY1] = 1 ;}} return true;} void undo (TEM * TEM) {// restore visit status back to for (INT I = 0; I <3; I ++) if (TEM [I]. v! =-1) visit [TEM [I]. x] [TEM [I]. y] = TEM [I]. v;} void DFS (int x, int y, int num) {// go to int newx, newy from the lower left corner to the upper right corner; If (x =-1) {mm = min (mm, num); return;} newy = Y + 1; newx = x; If (newy> = m) {newx = x-1; newy = 0;} If (G [x] [Y] = '. ') {// determine whether the light is needed for a vulnerable room bool flag = 0; If (visit [x] [Y]) {// For a highlighted room, consider not lighting DFS (newx, newy, num); flag = 1;} If (Num + 1> = mm) // return directly after pruning is greater than or equal to the minimum value; TEM [3]; // memset (TEM,-1, Sizeof (TEM); If (influence (X, Y, 0, TEM) {G [x] [Y] = 'X'; DFS (newx, newy, num + 1); UNDO (TEM); G [x] [Y] = '. ';} else {undo (TEM); // you need to restore the status no matter whether it can be put or not. This is because the influence function I wrote if (! Flag) // trim this room cannot be illuminated and return directly;} else DFS (newx, newy, num);} void solve () {// enumerate the position of the Special lamp, mm = inf; memset (visit, 0, sizeof (visit); bool flag = 1; TEM [3]; for (INT I = 0; I <n; I ++) for (Int J = 0; j <m; j ++) if (G [I] [J] = '. ') {flag = 0; G [I] [J] = 'X'; For (int K = 0; k <4; k ++) {memset (TEM, -1, sizeof (TEM); memset (visit, 0, sizeof (visit); If (influence (I, j, k, TEM) DFS (n-1, 0, 1); UNDO (TEM);} G [I] [J] = '. ';} If (Mm = inf) {If (FLAG) printf ("0 \ n"); elseprintf ("-1 \ n ");} elseprintf ("% d \ n", mm);} int main () {While (scanf ("% d", & N, & M ), N | M) {for (INT I = 0; I <n; I ++) scanf ("% s", G [I]); solve ();} return 0 ;}


Test example:

2 2
##
##
2 3
#..
..#
3 3
###
#.#
###
4 2
#.
..
.#
..
1 1
.
2 1
.
#
1 2
#.
1 5
.#...
1 4
.#..
2 5
...#.
####.
6 4
#.##
..#.
#.#.
#..#
....
....
5 5
#..#.
##.#.
####.
#.###
#..##
2 2
..
#.
2 2
..
..
2 2
##
.#
2 2
##
#.
2 3
###
#..
2 3
#.#
#.#
0 0


Output:

0
2
-1
2
1
1
1
3
2
3
7
4
1
2
1
1
1
-1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.