hdu4770 状态压缩+枚举

news/2024/11/24 16:30:40/

http://acm.hdu.edu.cn/showproblem.php?pid=4770



Problem Description
Harry: "But Hagrid. How am I going to pay for all of this? I haven't any money." 
Hagrid: "Well there's your money, Harry! Gringotts, the wizard bank! Ain't no safer place. Not one. Except perhaps Hogwarts." 
— Rubeus Hagrid to Harry Potter. 
  Gringotts Wizarding Bank is the only bank of the wizarding world, and is owned and operated by goblins. It was created by a goblin called Gringott. Its main offices are located in the North Side of Diagon Alley in London, England. In addition to storing money and valuables for wizards and witches, one can go there to exchange Muggle money for wizarding money. The currency exchanged by Muggles is later returned to circulation in the Muggle world by goblins. According to Rubeus Hagrid, other than Hogwarts School of Witchcraft and Wizardry, Gringotts is the safest place in the wizarding world.
  The text above is quoted from Harry Potter Wiki. But now Gringotts Wizarding Bank is not safe anymore. The stupid Dudley, Harry Potter's cousin, just robbed the bank. Of course, uncle Vernon, the drill seller, is behind the curtain because he has the most advanced drills in the world. Dudley drove an invisible and soundless drilling machine into the bank, and stole all Harry Potter's wizarding money and Muggle money. Dumbledore couldn't stand with it. He ordered to put some magic lights in the bank rooms to detect Dudley's drilling machine. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:

  Some rooms are indestructible and some rooms are vulnerable. Dudely's machine can only pass the vulnerable rooms. So lights must be put to light up all vulnerable rooms. There are at most fifteen vulnerable rooms in the bank. You can at most put one light in one room. The light of the lights can penetrate the walls. If you put a light in room (x,y), it lights up three rooms: room (x,y), room (x-1,y) and room (x,y+1). Dumbledore has only one special light whose lighting direction can be turned by 0 degree,90 degrees, 180 degrees or 270 degrees. For example, if the special light is put in room (x,y) and its lighting direction is turned by 90 degrees, it will light up room (x,y), room (x,y+1 ) and room (x+1,y). Now please help Dumbledore to figure out at least how many lights he has to use to light up all vulnerable rooms.
  Please pay attention that you can't light up any indestructible rooms, because the goblins there hate light. 


Input
There are several test cases.
  In each test case:
  The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 200).
  Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, and '.' means a vulnerable room. 
  The input ends with N = 0 and M = 0

Output
For each test case, print the minimum number of lights which Dumbledore needs to put.
  If there are no vulnerable rooms, print 0.
  If Dumbledore has no way to light up all vulnerable rooms, print -1.

Sample Input
  
2 2 ## ## 2 3 #.. ..# 3 3 ### #.# ### 0 0

Sample Output
  
0 2 -1

Source
2013 Asia Hangzhou Regional Contest
/**
hdu4770 状态压缩+枚举
题目大意:给定一个n*m的矩阵有#和.,.可以放电筒,每个电筒可以照上和右加本身三个点,只有一个特殊电筒可以90,180,270度旋转,问最少用多少个电筒能把所有的.全覆盖。
题目大意:题目说了总共.的个数不会超过15个,我们要可以把所有的位置枚举一遍。先枚举特殊电筒,枚举其四个旋转方向。然后枚举其他的点。。值得一提的是:一点要注意char数组的初始化
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;vector<pair<int,int> >vec;
int n,m,t,flag,minn,Hash[350*350];
char a[305][305];
int dx[][3][2]= {0,0,-1,0,0,1,0,0,0,1,1,0,0,0,1,0,0,-1,0,0,0,-1,-1,0};bool get(int u,int v)
{for(int i=0; i<3; i++){int x=vec[u].first+dx[v][i][0];int y=vec[u].second+dx[v][i][1];if(a[x][y]=='#')return 0;if(a[x][y]=='.'&&x>0&&x<=n&&y>0&&y<=m){flag|=(1<<Hash[(x-1)*m+y-1]);}}return 1;
}bool judge(int ans,int flag)
{int num=1;for(int i=0; i<t; i++){if(ans&(1<<i)){num++;for(int j=0; j<3; j++){int x=vec[i].first+dx[0][j][0];int y=vec[i].second+dx[0][j][1];if(a[x][y]=='#')return 0;if(a[x][y]=='.'&&x>0&&x<=n&&y>0&&y<=m){flag|=(1<<Hash[(x-1)*m+y-1]);}}}}if(flag==(1<<t)-1)minn=min(minn,num);return 1;
}int main()
{while(~scanf("%d%d",&n,&m)){if(n==0&&m==0)break;memset(a,0,sizeof(a));for(int i=1; i<=n; i++){scanf("%s",a[i]+1);}vec.clear();int kk=0;for(int i=1; i<=n; i++){for(int j=1; j<=m; j++){if(a[i][j]=='.'){vec.push_back(make_pair(i,j));Hash[(i-1)*m+j-1]=kk++;}}}t=vec.size();if(t==0){puts("0");continue;}minn=0x3f3f3f;for(int i=0; i<t; i++)///特殊电筒{for(int j=0; j<4; j++)///旋转方向{flag=0;if(!get(i,j))continue;for(int k=0; k<(1<<t); k++){if(k&(1<<i))continue;judge(k,flag);}}}if(minn==0x3f3f3f)minn=-1;printf("%d\n",minn);}return 0;
}



http://www.ppmy.cn/news/147411.html

相关文章

BZOJ4770: 图样 (随机点值求异或最小生成树边权和)

题目描述&#xff1a; n ≤ 50 , m ≤ 8 n\le50,m\le8 n≤50,m≤8 题目分析&#xff1a; 根据最小生成树有小到大加入边可以将点按照二进制最高位分组&#xff0c;统计所有情况的边权和。 Code&#xff1a; #include<bits/stdc.h> #define maxn 55 #define maxm 9 u…

【JZOJ4770】闭门造车

Description 自从htn体验了一把飙车的快感&#xff0c;他就下定决心要闭门造车&#xff01;但是他两手空空怎么造得出车来呢?无奈的他只好来到了汽车零部件商店。 一走进商店&#xff0c;玲琅满目的各式零件看得htn眼花缭乱。但是他很快便反应过来:我只要买一套好的零件就行…

chmod 4770的第一位解释

权限标志通过三个“位”来定义&#xff0c;分别是&#xff1a; setuid&#xff1a;设置使文件在执行阶段具有文件所有者的权限。比如/usr/bin/passwd&#xff0c;如果一般用户执行该文件&#xff0c;则在执行过程中&#xff0c;该文件可以获得root权限&#xff0c;从而可以更改…

Jzoj4770 闭门造车

自从htn体验了一把飙车的快感&#xff0c;他就下定决心要闭门造车&#xff01;但是他两手空空怎么造得出车来呢?无奈的他只好来到了汽车零部件商店。 一走进商店&#xff0c;玲琅满目的各式零件看得htn眼花缭乱。但是他很快便反应过来:我只要买一套好的零件就行。首先它们的性…

BZOJ 4770: 图样(恶心概率期望DP)

题目 考试时推到 p p p就嫌麻烦&#xff0c;不想推了。。。 讲真写好DP预处理调一年。 时间复杂度 O ( n 4 m 2 m ) O(n^4m2^m) O(n4m2m)的代码 T E ( b u t c o r r e t ) C o d e \mathrm {TE (but \ corret)\ Code} TE(but corret) Code //#pragma GCC optimize(3) #inc…

bzoj 4770 图样 - 概率与期望 - 动态规划

题目传送门 传送门I 传送门II 题目大意 有一个$n$个点的完全图&#xff0c;每个点的权值是$[0, 2^{m})$中的随机整数&#xff0c;两点间的边的权值是两点点权的异或和&#xff0c;问它的最小异或生成树的边权和的期望。 考虑求最大异或生成树的分治做法&#xff0c;每次按最高位…

BZOJ4770 图样(概率期望+动态规划)

考虑求出所有MST的权值和再除以方案数&#xff0c;方案数显然是2mn。 按位考虑&#xff0c;显然应该让MST里的边高位尽量为0。那么根据最高位是0还是1将点集划分成两部分&#xff0c;整张图的MST就是由两部分各自的MST之间连一条最小边得到的。两部分的MST权值和可以dp得到&…

P4770-[NOI2018]你的名字【SAM,线段树合并】

正题 题目链接:https://www.luogu.com.cn/problem/P4770 题目大意 给出一个长度为 n n n的字符串 S S S。 q q q次询问给出一个串 T T T和一个区间 [ L , R ] [L,R] [L,R]&#xff0c;求 T T T有多少个本质不同的子串不是 S L ∼ R S_{L\sim R} SL∼R​的子串。 1 ≤ n ≤ 5 …