Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4770
The main idea is to let you put some lights to illuminate some room lights can not be taken to some special rooms but you can light the border lights must cover all the rooms can be put lights to find at least how many lights
Search Questions
It should be noted that there are only 15 rooms in the long and boring question that tell you that you can put the lights on.
So 2^15 to enumerate
You should also be aware of the lengthy and boring questions that tell you that up to 1 lights can be turned
So you should enumerate which light to turn to.
Turn and Enumerate 4 directions
and decide if it's true.
To simplify the problem, just take out the lights for up to 15 of them.
First-level Loop enumeration State 2^15
The second layer enumerates which lights are used to turn 15.
And then we'll start by judging if all the non-steering lights are not going to shine.
The third layer enumerates the four directions of the steering light
Determine the turn light to prevent legality later
Check whether the current light has covered all the rooms that should be illuminated (I use Set)
This question was written three times last year, and every time, it's not even a complete program.
Just silently knocked for 1 hours and then compiled AC once had a kind of reported the thrill of feud ...
In retrospect, there were two reasons why I couldn't write.
1. Simulation problems do not write sub-function habits and then loop nested more easily to write their own chaos
and the sub-function judge can be returned at any time. If you write it all again, you need to make a lot of flags easier to write.
2. Do not put the problem enough abstraction for the first time not to say if you can later realize that nothing is "15 lattice of the state of the search" may not be so afraid of writing it.
1000ms of the topic I 920ms pressure is really ashamed
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<Set>#include<stack>#include<queue>#include<vector>#include<cmath>#include<map>using namespaceStd;typedefLong Longll;typedef unsignedLong LongUll;typedef pair<int,int>P;typedefstruct{ intx, y;} Node;Const intMAXN = About;Const intwalk[][2] = {1,0,0,1, -1,0,0, -1};intN, M;CharA[MAXN][MAXN]; Node v[ -];BOOLJudgeintSintSumintTurnid) { intvid =0; Set<P>check; for(inti =0; i < sum; i++)//determine if the non-turning lights are legally placed and record the room they can illuminate { if(S & (1<< i) && i! =Turnid) { if(V[i].x-1>=1&& v[i].x-1<= N && a[v[i].x-1][V[I].Y]! ='.') return 0; if(V[i].y +1>=1&& V[i].y +1<= m && a[v[i].x][v[i].y +1] !='.') return 0; Check.insert (P (v[i].x, v[i].y)); if(V[i].x-1>=1) Check.insert (P (v[i].x-1, v[i].y)); if(V[i].y +1<=m) Check.insert (P (v[i].x, V[i].y+1)); } } for(inti =0; I <4; i++)//enumerate the four directions of the steering light { intCNT =check.size (); intX1 =v[turnid].x; intY1 =v[turnid].y; intx2 = x1 + walk[i][0]; inty2 = y1 + walk[i][1]; intx3 = x1 + walk[(i+1)%4][0]; intY3 = y1 + walk[(i+1)%4][1]; if(X2 >=1&& x2 <= n && y2 >=1&& y2 <= m && a[x2][y2]! ='.') Continue; if(X3 >=1&& X3 <= N && y3 >=1&& y3 <= m && a[x3][y3]! ='.') Continue; if(!Check.count (P (x1, y1)) CNT++; if(X2 >=1&& x2 <= n && y2 >=1&& y2 <= m &&!Check.count (P (x2, y2)) CNT++; if(X3 >=1&& X3 <= N && y3 >=1&& y3 <= m &&!Check.count (P (x3, y3)) CNT++; if(cnt = = SUM)//lighted room = = room to be illuminated return 1; } return 0;}intMain () {//freopen ("In.txt", "R", stdin); while(SCANF ("%d%d", &n, &m) = =2&&! (n = =0&& m = =0)) { for(inti =1; I <= N; i++) scanf ("%s", a[i]+1); intCNT =0;//There are several compartments for the record to put the lights on . for(inti =1; I <= N; i++)//first put the less than or equal to 15 squares. for(intj =1; J <= M; J + +) { if(A[i][j] = ='.') {v[cnt].x=i; V[cnt].y=J; CNT++; } } if(CNT = =0)//Special Award{printf ("0\n"); Continue; } intAns = -; for(inti =1; I < (1<< CNT); i++) { intlcnt =0;//record a few lights intltmp[ -];//v[] subscript for record light for(intj =0; J < CNT; J + +) if(I & (1<<j)) ltmp[lcnt++] =J; for(intj =0; J < lcnt; J + +) if(Judge (I, CNT, ltmp[j]) && lcnt <ans) ans=lcnt; } if(ans = = -) printf ("-1\n"); Elseprintf ("%d\n", ans); } return 0;}
HDU 4770 Lights against dudely violence search