题意:图中有最多有15个点 ,可以在点位置放置灯 (一个点只能放一个灯)
灯可以照亮(i,j) (i-1,j) (i,j+1)
其中(i-1,j),(i,j+1) 可以超出地图且不能为'#'
其中一个灯任意可以转动
求放置灯最少的数量。
思路:
状态压缩 枚举转动的点
再判断是否可覆盖所有点
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <float.h>
#include <stack>
#include <vector>
#include <bitset>
#include <deque>
#include <set>
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1<<29;
const LL mod = 1e9+7;
const int MAXN = 110100;//点数的最大值
const int MAXM = 520;//边数的最大值
const double pi = acos(-1.0);
int n,m;
char mp[222][222];
vector<int> have[17];
vector<pair<int,int > >dot;
int xx[4]= {1,-1,-1,1};
int yy[4]= {-1,-1,1,1};
int vis[17];
int inmap(int x,int y)
{
if(x<0 || x>=n) return 1;
if(y<0 || y>=m) return 1;
if(mp[x][y]=='#') return 0;
vis[mp[x][y]]=1;
return 1;
}
int doit(int hh,int fx,int now,int sz)
{
memset(vis,0,sizeof(vis));
int hhx=dot[hh].first+xx[fx];
int hhy=dot[hh].second+yy[fx];
vis[hh]=1;
if(!inmap(hhx,dot[hh].second)) return 0;
if(!inmap(dot[hh].first,hhy)) return 0;
for(int i=0; i<sz; i++)
{
if(i!=hh&&(now&(1<<i)))
{
int x=dot[i].first-1;
int y=dot[i].second+1;
vis[mp[dot[i].first][dot[i].second]]=1;
if(!inmap(x,dot[i].second)) return 0;
if(!inmap(dot[i].first,y)) return 0;
}
}
for(int i=0; i<sz; i++)
{
if(vis[i]==0)
return 0;
}
return 1;
}
int gao(int now,int sz)
{
for(int i=0; i<sz; i++)
{
if(now&(1<<i))//第i个点为可转动点
{
for(int j=0; j<4; j++)//枚举方向
{
if(doit(i,j,now,sz))
return 1;
}
}
}
return 0;
}
int main()
{
/*预处理点*/
for(int i=0; i<16; i++)
have[i].clear();
for(int i=1; i<(1<<16); i++)
{
int num=0;
for(int j=0; j<16; j++)
if(i&(1<<j)) num++;
have[num].push_back(i);
}
while(scanf("%d%d",&n,&m),n+m)
{
dot.clear();
for(int i=0; i<n; i++)
{
scanf("%s",mp[i]);
for(int j=0; j<m; j++)
if(mp[i][j]=='.')
{
mp[i][j]=dot.size();
dot.push_back(make_pair(i,j));//存点
}
}
if(dot.size()==0)
{
printf("0\n");
continue;
}
int sz=dot.size();
int ans=INF;
int flag=0;
for(int i=1; i<=sz; i++)//枚举点数
{
for(int j=0; j<have[i].size(); j++)
{
int now=have[i][j];//状态
if(gao(now,sz))//在该状态判断
{
ans=i;
flag=1;
break;
}
}
if(flag) break;
}
if(ans==INF) puts("-1");
else printf("%d\n",ans);
}
return 0;
}
/*
*/