HDU 4770 Lights Against Dudely(暴力枚举) 2013杭州现场赛
来源:互联网 发布:淘宝拒收可以退款吗 编辑:程序博客网 时间:2024/06/11 03:22
题目链接:点击打开链接
题意:输入一个n*m的矩阵,然后给你最多15个路灯,假设将路灯安装在(x,y)点,那么能照亮的地区为(x,y)(x-1,y)(x,y+1)三个位置,而这些路灯中有一个最特殊能照亮L对称的4个方向,求保证不照到#位置并能照亮所有"."位置需要的最小的路灯数。
分析:因为就15盏灯,那么我们考虑二进制枚举,一共2的15次方中情况,比如10001表示第一盏和最后一盏灯亮,然后我们在这些位置中枚举这特殊的一盏,在考虑那些一般的灯存放情况,取其中的最小值即可。
#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <ctime>#include <iostream>#include <algorithm>#include <string>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <numeric>#include <iomanip>#include <bitset>#include <sstream>#include <fstream>#include <limits.h>#define debug "output for debug\n"#define pi (acos(-1.0))#define eps (1e-4)#define inf (1<<28)#define sqr(x) (x) * (x)using namespace std;typedef long long ll;typedef unsigned long long ULL;int dir[4][2]= {{-1,0},{0,1},{1,0},{0,-1}};char mp[210][210];int vis[210][210];struct node{ int x,y;}pos[20];int num,n,m;int getsum(int x){ int sum=0; while(x) { sum+=x%2; x/=2; } return sum;}int C(int x,int y){ if(x<1||y<1||x>n||y>m) return 0; return 1;}int judge(int k,int tmp){ for(int i=0;i<num;i++) { int x=pos[i].x,y=pos[i].y; if(vis[x][y]!=tmp) vis[x][y]=0; } for(int i=0;i<num;i++) { if((k>>i)&1) { int x1=pos[i].x,y1=pos[i].y+1; int x2=pos[i].x-1,y2=pos[i].y; int x=pos[i].x,y=pos[i].y; if(C(x1,y1)&&mp[x1][y1]=='#')return 0; if(C(x2,y2)&&mp[x2][y2]=='#')return 0; if(!vis[x1][y1]) vis[x1][y1]=1; if(!vis[x2][y2]) vis[x2][y2]=1; if(!vis[x][y]) vis[x][y]=1; } } for(int i=0;i<num;i++) { int x=pos[i].x; int y=pos[i].y; if(!vis[x][y])return 0; } return 1;}int main(){ while(scanf("%d%d",&n,&m)&&(m+n)) { for(int i=1;i<=n;i++) scanf("%s",mp[i]+1); num=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(mp[i][j]=='.') { pos[num].x=i; pos[num++].y=j; } } } if(num==0) { puts("0"); continue; } int mx=1<<num,tmp=1; int ans=inf; memset(vis,0,sizeof vis); for(int i=0;i<num;i++) { int x=pos[i].x,y=pos[i].y; for(int j=0;j<4;j++) { for(int k=0;k<num;k++) { int xx=pos[k].x,yy=pos[k].y; vis[xx][yy]=0; } int x1=dir[j][0]+x,y1=dir[j][1]+y; int x2=dir[(j+1)%4][0]+x,y2=dir[(j+1)%4][1]+y; if((C(x1,y1)&&mp[x1][y1]=='#')||(C(x2,y2)&&mp[x2][y2]=='#'))//如果在地图里要保证不是#位置 continue; tmp++; vis[x][y]=tmp; vis[x1][y1]=tmp; vis[x2][y2]=tmp; for(int k=0;k<mx;k++) { if((k>>i)&1) continue; if(judge(k,tmp)){ if(ans>getsum(k)+1) ans=getsum(k)+1; else break; } } } } if(ans==inf) puts("-1"); else printf("%d\n",ans); } return 0;}/****4 4................3 3.........****/
0 0
- HDU 4770 Lights Against Dudely(暴力枚举) 2013杭州现场赛
- HDU 4770 Lights Against Dudely -- 2013 杭州赛区现场赛-A(状态压缩)
- hdu 4770 Lights Against Dudely【暴力枚举】
- hdu 4770 Lights Against Dudely(暴力枚举dfs)
- HDU 4770 Lights Against Dudely(暴力)
- #hdu4770# #2013杭州现场赛# A:Lights Against Dudely
- HDU 4770 Lights Against Dudely 【状态压缩+暴力枚举】
- HDU-4470-Lights Against Dudely(暴力枚举)
- hdu 4770 Lights Against Dudely (2013亚洲区域赛杭州站 A)
- HDU 4770 Lights Against Dudely(暴力)
- hdu 4770 Lights Against Dudely(暴力搜索)
- hdu 4770 Lights Against Dudely(暴力+状压)
- hdu 4770 Lights Against Dudely(二进制枚举情况)
- HDU 4770 Lights Against Dudely(二进制枚举子集)
- hdu 4770 Lights Against Dudely
- HDU:4770 Lights Against Dudely
- HDU 4770 Lights Against Dudely
- hdu 4770 Lights Against Dudely
- [每日一答] [20151011] 如何在Python2.7版本中安装pip程序呢?
- docker-registry2(docker-distribution)环境搭建
- POJ - 1426 Find The Multiple(15.10.10 搜索专题)bfs
- Linux内核工程导论——内核为何使用C语言
- android studio导入os库
- HDU 4770 Lights Against Dudely(暴力枚举) 2013杭州现场赛
- OpenCv:Mat矩阵的初始化
- matlab调用mysql
- 互信息 Mutual Information
- python 查询小程序
- yii视频小记
- 数据库SQL优化
- 基于HTML5实现3D监控应用流动效果
- 在swift中 @objc 、private 和事件问题