Problem descriptionharry: "But Hagrid. How am I going to pay for all of this? I haven't any money ."
HAGRID: "Well there's your money, Harry! Gringotts, the wizard bank! Ain't no safer place. Not one. doesn t perhaps Hogwarts ."
-Rubeus Hagrid to Harry Potter.
Gringotts wizarding Bank is the only bank of the wizarding world, and is owned and operated by gobloud. it was created by a goblin called gringott. its main offices are located in the north side of Diagon Alley in London, England. in addition to storing money and valuables for wizards and witches, one can go there to exchange Muggle money for wizarding money. the currency exchanged by Muggles is later returned to circulation in the Muggle world by gobloud. according to Rubeus Hagrid, other than Hogwarts School of Witchcraft and Wizardry, Gringotts is the safest place in the wizarding world.
The text above is quoted from Harry Potter wiki. but now Gringotts wizarding bank is not safe anymore. the stupid Dudley, Harry Potter's cousin, just robbed the bank. of course, Uncle Vernon, the drill seller, is behind the curtain because he has the most advanced drills in the world. dudley drove an invisible and soundless drilling machine into the bank, and stole all Harry Potter's wizarding money and Muggle money. dumbledore couldn't stand with it. he ordered to put some magic lights in the bank rooms to detect Dudley's drilling machine. the bank can be considered as a n × m grid consisting of n × m rooms. each room has a coordinate. the coordinates of the upper-left room is (1, 1), the down-Right room is (n, m) and the room below the upper-left room is (2, 1 )..... a 3 × 4 bank grid is shown below:
Some rooms are indestructible and some rooms are vulnerable. dudely's machine can only pass the vulnerable rooms. so lights must be put to light up all vulnerable rooms. there are at most fifteen vulnerable rooms in the bank. you can at most put one light in one room. the light of the lights can penetrate the Wils. if you put a light in room (x, y), it lights up three rooms: Room (x, y), room (x-1, Y) and room (x, y, Y + 1 ). dumbledore has only one special light whose lighting ction can be turned by 0 degree, 90 degrees, 180 degrees or 270 degrees. for example, if the special light is put in room (x, y) and its lighting ction is turned by 90 degrees, it will light up room (x, y ), room (X, Y + 1) and room (x + 1, Y ). now please help Dumbledore to figure out at least how should lights he has to use to light up all vulnerable rooms.
Please pay attention that you can't light up any indestructible rooms, because the gob1_there hate light.
Input there are several test cases.
In each test case:
The first line are two integers n and M, meaning that the bank is a n x m grid (0 <n, m <= 200 ).
Then a n × M matrix follows. Each element is a letter standing for a room. '# 'means a indestructible room, And'. 'means a vulnerable room.
The input ends with n = 0 and m = 0
Output for each test case, print the minimum number of lights which Dumbledore needs to put.
If there are no vulnerable rooms, print 0.
If Dumbledore has no way to light up all vulnerable rooms, print-1.
Sample Input
2 2####2 3#....#3 3####.####0 0
Sample output
02-1
Source2013 Asia Hangzhou Regional Contest
Idea: brute-force enumeration of the positions of each style of special lights, but DFS is very strange. Details are in the code.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <vector> using namespace STD; # define n 205 # define MaxE 0x3f3f3fint step [4] [3] [2] = {,-,}, {,}, {0, 0,-, 0 },{,-, 0,-1 }}; int X [N], Y [N], num, C [N]; int n, m, vis [N] [N], ans; char a [n] [N]; int judge (int x, int y, int st [3] [2]) {int I, j; for (I = 0; I <3; I ++) {int xx = x + st [I] [0]; int YY = Y + st [I] [1]; If (XX> 0 & XX <= N & YY> 0 & YY <= M & A [XX] [YY] = '#') return 0 ;}return 1 ;} void change (int x, int y, int st [3] [2], int s) {int I; for (I = 0; I <3; I ++) {int xx = x + st [I] [0]; int YY = Y + st [I] [1]; If (XX <= 0 | XX> N) continue; If (yy <= 0 | YY> m) continue; vis [XX] [YY] = s ;}} void DFS (int d, int F, int temp) {If (temp> = ans) return; If (D = num) {ans = temp; return ;} if (vis [x [d] [Y [d]) {DFS (D + 1, F, temp ); // This is where no light is needed.} If (C [d] & F! = D) // you can place the light in this position, and the light has not been put off before. Here {change (X [d], Y [d], step [0], 1 ); DFS (D + 1, F, temp + 1); change (X [d], Y [d], step [0], 0) ;}} int main () {int I, j; while (~ Scanf ("% d", & N, & M), N + M) {for (I = 1; I <= N; I ++) scanf ("% s", a [I] + 1); num = 0; for (I = N; I --) for (j = 1; j <= m; j ++) if (a [I] [J] = '. ') {x [num] = I; y [num ++] = J ;}for (I = 0; I <num; I ++) c [I] = judge (X [I], Y [I], step [0]); ans = MaxE; If (num = 0) ans = 0; memset (VIS, 0, sizeof (VIS); for (I = 0; I <num; I ++) for (j = 0; j <4; j ++) if (Judge (X [I], Y [I], step [J]) {change (X [I], Y [I], step [J], 1); DFS (0, I, 1); change (X [I], Y [I], step [J], 0);} If (ANS = MaxE) printf ("-1 \ n"); else printf ("% d \ n", ANS);} return 0 ;}
HDU 4770 lights against dudely