最多只有15个房间可以放置灯,二进制枚举一下,注意:灯可以照到边界外,但不可以照亮“坚不可摧”的房间,而且不要用set什么的,开个数组记录某个房间是否被照亮,是否有剩余的房间没被照亮,不然会T;
//Lights Against Dudely.cpp
#include <ios>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <cstdio>
#include <cwchar>
#include <iosfwd>
#include <limits>
#include <locale>
#include <memory>
#include <string>
#include <vector>
#include <cassert>
#include <ciso646>
#include <climits>
#include <clocale>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <cwctype>
#include <fstream>
#include <iomanip>
#include <istream>
#include <numeric>
#include <ostream>
#include <sstream>
#include <utility>
#include <iostream>
#include <iterator>
#include <valarray>
#include <algorithm>
#include <exception>
#include <stdexcept>
#include <streambuf>
#include <functional>
#define LL long long
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define PI 3.1415926535897932626
#define EXIT exit(0);
#define DEBUG puts("Here is a BUG");
#define CLEAR(name, init) memset(name, init, sizeof(name))
const double eps = 1e-8;
const int MAXN = (int)2e2 + 5;
using namespace std;
struct Pos {
int x, y;
Pos() { }
Pos(int x, int y): x(x), y(y) { }
bool operator < (const Pos& rhs) const {
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
}light[20];
int num;
char bank[MAXN][MAXN];
int light_seq[MAXN][MAXN];
const int dir[4][2][2] = {{{-1, 0}, {0, 1}}, {{0, 1}, {1, 0}}, {{0, -1}, {1, 0}}, {{0, -1}, {-1, 0}}};
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
freopen("D:\\Documents\\Disk_Synchronous\\Programs\\Acm\\input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
int n, m;
while(cin >> n >> m) {
if (n == 0 && m == 0) break;
num = 0;
CLEAR(light_seq, 0);
for(int i = 0; i < MAXN; i++) {
for(int j = 0; j < MAXN; j++) {
bank[i][j] = '$';
}
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
cin >> bank[i][j];
if (bank[i][j] == '.') {
light[num] = Pos(i, j);
light_seq[i][j] = num++;
}
}
}
if (num == 0) {
cout << num << endl;
continue;
}
int res = INT_MAX, cnt = 1 << num;
for(int i = 0; i < cnt; i++) {
vector<int> put;
int tmp = i, tt = 0;
while(tmp) {
if (tmp & 1) put.push_back(tt);
tt++; tmp >>= 1;
}
for(int j = 0; j < put.size(); j++) {
for(int k = 0; k < 4; k++) {
bool used[20];
for(int i = 0; i < num; i++) {
used[i] = false;
}
bool flag = false;
int put_len = put.size();
for(int i = 0; i < put_len; i++) {
used[light_seq[light[put[i]].x][light[put[i]].y]] = true;
if (i == j) continue;
if (bank[light[put[i]].x-1][light[put[i]].y] == '#' || bank[light[put[i]].x][light[put[i]].y+1] == '#') {
flag = true;
break;
}
if (bank[light[put[i]].x-1][light[put[i]].y] == '.') {
used[light_seq[light[put[i]].x-1][light[put[i]].y]] = true;
}
if (bank[light[put[i]].x][light[put[i]].y+1] == '.') {
used[light_seq[light[put[i]].x][light[put[i]].y+1]] = true;
}
}
if (flag) continue;
if (bank[light[put[j]].x+dir[k][0][0]][light[put[j]].y+dir[k][0][1]] == '#') continue;
if (bank[light[put[j]].x+dir[k][1][0]][light[put[j]].y+dir[k][1][1]] == '#') continue;
if (bank[light[put[j]].x+dir[k][0][0]][light[put[j]].y+dir[k][0][1]] == '.') {
used[light_seq[light[put[j]].x+dir[k][0][0]][light[put[j]].y+dir[k][0][1]]] = true;
}
if (bank[light[put[j]].x+dir[k][1][0]][light[put[j]].y+dir[k][1][1]] == '.') {
used[light_seq[light[put[j]].x+dir[k][1][0]][light[put[j]].y+dir[k][1][1]]] = true;
}
int tmp = 0;
for(int i = 0; i < num; i++) {
tmp += used[i];
}
if (tmp == num && res > put_len) res = put_len;
}
}
}
cout << (res == INT_MAX ? -1 : res) << endl;
}
return 0;
}