枚举+状态压缩——2013 杭州区域赛A ( UVA 6454 )
2016-08-25 20:49
423 查看
题目链接:
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4465
参考链接:
http://blog.csdn.net/min_lala/article/details/15500613
分析:
给出一个N×M的网格,其中’#’表示不能被灯光照射,’.’表示空房间(最多15个空房间)需要被灯光照射,每盏灯只能照射它自己的位置,它左边的位置和它上边的位置且灯不可旋转,不过有一盏特殊灯可以旋转(比如右旋90°,即照射上边的和右边的位置)
题解:
因为空房间数量最多15个,那么最多只可能有15*2^15种情况,所以我们可以直接用状态压缩枚举每一种放灯情况,判断其是否可行,然后更新最小的放灯数量即可!
AC代码:
![](http://img.blog.csdn.net/20160825204518876)
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4465
参考链接:
http://blog.csdn.net/min_lala/article/details/15500613
分析:
给出一个N×M的网格,其中’#’表示不能被灯光照射,’.’表示空房间(最多15个空房间)需要被灯光照射,每盏灯只能照射它自己的位置,它左边的位置和它上边的位置且灯不可旋转,不过有一盏特殊灯可以旋转(比如右旋90°,即照射上边的和右边的位置)
题解:
因为空房间数量最多15个,那么最多只可能有15*2^15种情况,所以我们可以直接用状态压缩枚举每一种放灯情况,判断其是否可行,然后更新最小的放灯数量即可!
AC代码:
/************************************************************************* > File Name: A.cpp > Author: Akira > Mail: qaq.febr2.qaq@gmail.com > Created Time: 2016年08月25日 星期四 19时40分56秒 ************************************************************************/ #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cmath> #include <vector> #include <set> #include <list> #include <ctime> typedef long long LL; typedef unsigned long long ULL; typedef long double LD; #define MST(a,b) memset(a,b,sizeof(a)) #define CLR(a) MST(a,0) #define Sqr(a) ((a)*(a)) using namespace std; #define MaxN 100000 #define MaxM MaxN*10 #define INF 1000000000 #define bug cout<<88888888<<endl; struct Node { int x,y; }node[16]; int Map[233][233]; int dir[][2] = {{-1,0},{0,1},{1,0},{0,-1}}; int N,M; int main() { while(~scanf("%d%d", &N, &M) && (N+M) ) { memset(Map, '$', sizeof(Map)); int n = 0; for(int i=1; i<=N; i++) { getchar(); for(int j=1; j<=M; j++) { char ch; scanf("%c", &ch); Map[i][j] = ch; if(ch == '.') { node .x = i; node .y = j; n++; } } } if(n == 0) { printf("0\n"); continue; } int tot = 1<<n; //总共有n个空房间,所有有2^n次种状态 int flag[16]; //二进制数存储状态,0或者1代表某状态下每一个房间放灯还是不放 int Min = INF; //放灯的最小数量 int cnt; //记录每种状态下放灯数量 for(int i=1;i<tot;i++) { cnt = 0; for(int j=0;j<n;j++) { flag[j] = 0; //先把每个位置都清0 if( i & (1<<j) ) //判断i状态下j位置是否放灯并计算出i状态下的放灯数 { flag[j] = 1; cnt++; } } for(int j=0;j<n;j++) //判断特殊灯能否放置在j处 { if(flag[j] == 0) continue; int room = 0; for(int l=0; l<n; l++) //每次都把地图复原 { Map[node[l].x][node[l].y] = '.'; } for(int k=0; k<n; k++)//把除特殊灯所在位置j的其他位置都放好灯 { if(flag[k] == 1 && k!=j) { if(Map[node[k].x-1][node[k].y] == '#' || Map[node[k].x][node[k].y+1]=='#' ) continue; //跳过不能放灯的位置 //照亮三个房间,并在图中标成$ if(Map[node[k].x][node[k].y] == '.') { Map[node[k].x][node[k].y] = '$'; room++; } if(Map[node[k].x-1][node[k].y] == '.') { Map[node[k].x-1][node[k].y] = '$'; room++; } if(Map[node[k].x][node[k].y+1] == '.') { Map[node[k].x][node[k].y+1] = '$'; room++; } } } for(int d=0; d<4; d++) 通过遍历4种旋转的方法判断j位置能不能放特殊灯 { int Final = room; int a = d; int b = (d+1)%4; int x = node[j].x; int y = node[j].y; if(Map[x+dir[a][0]][y+dir[a][1]] == '#' || Map[x+dir[b][0]][y+dir[b][1]] == '#' ) continue; //该旋转状态下如果存在不可照射房间就跳过 //判断特殊灯能否照亮未照亮的房间 if(Map[x][y] == '.') { Final ++; } if(Map[x+dir[a][0]][y+dir[a][1]] == '.') { Final ++; } if(Map[x+dir[b][0]][y+dir[b][1]] == '.') { Final ++; } if(Final == n) //如果这种放灯状态能照亮所有空房间就更新Min值 Min = min(Min, cnt); } } } if(Min != INF) printf("%d\n", Min); else printf("-1\n"); } return 0; }
相关文章推荐
- 2013杭州站A题||hdu4770 状态 压缩+暴力枚举
- HDU 4771 2013亚洲区域赛杭州站 B题 状态压缩+bfs
- UVALive 3953 Strange Billboard (状态压缩+枚举)
- UVa --- 10464 Even Parity 偶数矩阵 【状态压缩 + 暴力枚举 + 思维】
- UVALive - 3693 Balancing the Scale 枚举 + 状态压缩
- HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)
- hdu4771_2013亚洲区域赛杭州站(状态压缩bfs)
- UVA 11825 集合枚举 状态压缩 dp
- uva1508(状态压缩 +枚举)
- HDU 4778 Gems Fight! (2013杭州赛区1009题,状态压缩,博弈)
- UVA 1252-Twenty Questions(状态压缩DP+子集枚举)
- HDU 4770 Lights Against Dudely -- 2013 杭州赛区现场赛-A(状态压缩)
- UVa 1508 - Equipment (状态压缩 + 枚举子集)
- 状态压缩+枚举 UVA 11464 Even Parity
- UVA1508-Equipment(状态压缩+暴力枚举)
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
- UVA 1508 - Equipment 状态压缩 枚举子集 dfs
- UVA 11825 - Hackers' Crackdown 状态压缩 dp 枚举子集
- UVA - 565 Pizza Anyone?(状态压缩+暴利枚举)
- uva 1047 - Zones-World finals 2005 状态压缩枚举