Topic Portal
Test instructions: There are n*m rooms, '. ' Indicates can be lit, ' # ' means not to be lit, each lit a room will be lit next to the room, the intention of a special lamp can be selected around the different directions of the room lit. Ask at least how many lights are needed to make all the rooms lit
Analysis: The room needs to be lit up to only 15, so consider the pressure, and then the violent enumeration choose which direction to use as a special lamp and enumerate which directions make the next room bright, note that emptying the VIS array needs to be optimized, memset timeout. 6 minutes 1y,orz ...
/************************************************ * author:running_time * Created TIME:2015/10/22 Thursday 18:25:25 * File name:a.cpp ************************************************/#include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string > #include <vector> #include <queue> #include <deque> #include <stack> #include <list># Include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime>using namespace std; #define Lson L, Mid, RT << 1#define Rson mid + 1, R, RT << 1 | 1typedef long ll;const int N = 2e2 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const Double EPS = 1e-8;ch Ar room[n][n];struct Point {int x, y; Point (int x, int y): x (x), Y (y) {}};int n, m;bool vis[n][n];void back_nomal (int x, int y) {vis[x][y] = false; if (x-1 >= 1) vis[x-1][y] =False if (y + 1 <= m) vis[x][y+1] = false;} void back_special (int x, int y, int type) {if (type = = 1) {back_nomal (x, y); return; } else if (type = = 2) {if (x + 1 <= N) {vis[x+1][y] = false; } if (y + 1 <= m) {vis[x][y+1] = false; }} else if (type = = 3) {if (x + 1 <= N) {vis[x+1][y] = false; } if (y-1 >= 1) {vis[x][y-1] = false; }} else if (type = = 4) {if (x-1 >= 1) {vis[x-1][y] = false; } if (y-1 >= 1) {vis[x][y-1] = false; }} Vis[x][y] = false;} BOOL Light_nomal (int x, int y) {if (x-1 >= 1 && room[x-1][y]! = '. ') return false; if (y + 1 <= m && room[x][y+1]! = '. ') return false; if (x-1 >= 1) {vis[x-1][y] = true; } if (y + 1 <= m) {vis[x][y+1] = true; } Vis[x][y] = true; return True;} BOOL Light_special (int x, int y, int type) {if (type = = 1) {return light_nomal (x, y); } else if (type = = 2) {if (x + 1 <= n && room[x+1][y]! = '. ') return false; if (y + 1 <= m && room[x][y+1]! = '. ') return false; if (x + 1 <= N) {vis[x+1][y] = true; } if (y + 1 <= m) {vis[x][y+1] = true; }} else if (type = = 3) {if (x + 1 <= n && room[x+1][y]! = '. ') return false; if (y-1 >= 1 && room[x][y-1]! = '. ') return false; if (x + 1 <= N) {vis[x+1][y] = true; } if (y-1 >= 1) {vis[x][y-1] = true; }} else if (type = = 4) {if (x-1 >= 1 && room[x-1][y]! = '. ') return false; if (y-1 >= 1 && room[x][y-1]! = '. ') return false; if (x-1 >= 1) {vis[x-1][y] = true; } if (y-1 >= 1) { Vis[x][y-1] = true; }} Vis[x][y] = true; return true;} int main (void) {while (scanf ("%d%d", &n, &m) = = 2) {if (!n &&!m) break; for (int i=1; i<=n; ++i) {scanf ("%s", Room[i] + 1); } int cnt = 0; Memset (Vis, false, sizeof (VIS)); Vector<point> V; for (int i=1, i<=n; ++i) {for (int j=1; j<=m; ++j) {if (room[i][j] = = '. ') {cnt++; V.push_back (Point (I, j)); }}} if (cnt = = 0) {puts ("0"); continue; } int tot = 1 << cnt; int ans = INF; for (int i=0, i<tot; ++i) {for (Int. k=0; k<cnt; ++k) {for (int l=1; l<=4; ++l) { bool OK = true; for (int j=0; j<cnt; ++j) {if (I & (1 << j)) { if (j = = k) {if (!light_special (v[j].x, V[J].Y, L)) { OK = false; Break }} else {if (!light_nomal (V[J). x, V[j].y)) {ok = false; break; }}}} if (!ok) { for (int j=0; j<cnt; ++j) {if (j = = k) back_special (v[j].x, V[J].Y, L); else Back_nomal (v[j].x, V[J].Y); } continue; } bool flag = TRUE; for (int j=0; j<cnt; ++j) {int x = v[j].x, y = v[j].y; if (!vis[x][y]) { Flag = false; Break }} if (flag) {ans = min (ans, __builtin_popcount (i)); } for (int j=0; j<cnt; ++j) {if (j = = k) back_special (V [J].x, V[J].Y, l]; else Back_nomal (v[j].x, V[J].Y); }}}} printf ("%d\n", ans = = INF. -1:ans); } return 0;}
State compression + violence hdoj 4770 Lights against dudely