状态压缩 + 暴力 HDOJ 4770 Lights Against Dudely
题意:有n*m的房间,'.'表示可以被点亮,'#'表示不能被点亮,每点亮一个房间会使旁边的房间也点亮,有意盏特别的灯可以选择周围不同方向的房间点亮。问最少需要多少灯使得所有房间点亮
分析:需要被点亮的房间最多只有15个,所以考虑状压,然后暴力枚举选择哪一个当作特殊灯和枚举选择哪个方向使旁边的房间亮,注意清空vis数组需要优化,memset超时。上交6分钟1Y,Orz。。。额,看错榜了,最快的19分钟,而且这不是第一道水题,汗
/************************************************
* Author :Running_Time
* Created Time :2015/10/22 星期四 18:25:25
* File Name :A.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 2e2 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
char room[N][N];
struct Point {
int x, y;
Point (int x, int y) : x (x), y (y) {}
};
int n, m;
bool vis[N][N]; void back_nomal(int x, int y) {
vis[x][y] = false;
if (x - 1 >= 1) vis[x-1][y] = false;
if (y + 1 <= m) vis[x][y+1] = false;
} void back_special(int x, int y, int type) {
if (type == 1) {
back_nomal (x, y); return ;
}
else if (type == 2) {
if (x + 1 <= n) {
vis[x+1][y] = false;
}
if (y + 1 <= m) {
vis[x][y+1] = false;
}
}
else if (type == 3) {
if (x + 1 <= n) {
vis[x+1][y] = false;
}
if (y - 1 >= 1) {
vis[x][y-1] = false;
}
}
else if (type == 4) {
if (x - 1 >= 1) {
vis[x-1][y] = false;
}
if (y - 1 >= 1) {
vis[x][y-1] = false;
}
}
vis[x][y] = false;
} bool light_nomal(int x, int y) {
if (x - 1 >= 1 && room[x-1][y] != '.') return false;
if (y + 1 <= m && room[x][y+1] != '.') return false;
if (x - 1 >= 1) {
vis[x-1][y] = true;
}
if (y + 1 <= m) {
vis[x][y+1] = true;
}
vis[x][y] = true;
return true;
} bool light_special(int x, int y, int type) {
if (type == 1) {
return light_nomal (x, y);
}
else if (type == 2) {
if (x + 1 <= n && room[x+1][y] != '.') return false;
if (y + 1 <= m && room[x][y+1] != '.') return false;
if (x + 1 <= n) {
vis[x+1][y] = true;
}
if (y + 1 <= m) {
vis[x][y+1] = true;
}
}
else if (type == 3) {
if (x + 1 <= n && room[x+1][y] != '.') return false;
if (y - 1 >= 1 && room[x][y-1] != '.') return false;
if (x + 1 <= n) {
vis[x+1][y] = true;
}
if (y - 1 >= 1) {
vis[x][y-1] = true;
}
}
else if (type == 4) {
if (x - 1 >= 1 && room[x-1][y] != '.') return false;
if (y - 1 >= 1 && room[x][y-1] != '.') return false;
if (x - 1 >= 1) {
vis[x-1][y] = true;
}
if (y - 1 >= 1) {
vis[x][y-1] = true;
}
}
vis[x][y] = true;
return true;
} int main(void) {
while (scanf ("%d%d", &n, &m) == 2) {
if (!n && !m) break;
for (int i=1; i<=n; ++i) {
scanf ("%s", room[i] + 1);
}
int cnt = 0;
memset (vis, false, sizeof (vis));
vector<Point> V;
for (int i=1; i<=n; ++i) {
for (int j=1; j<=m; ++j) {
if (room[i][j] == '.') {
cnt++;
V.push_back (Point (i, j));
}
}
}
if (cnt == 0) {
puts ("0"); continue;
}
int tot = 1 << cnt;
int ans = INF;
for (int i=0; i<tot; ++i) {
for (int k=0; k<cnt; ++k) {
for (int l=1; l<=4; ++l) {
bool ok = true;
for (int j=0; j<cnt; ++j) {
if (i & (1 << j)) {
if (j == k) {
if (!light_special (V[j].x, V[j].y, l)) {
ok = false; break;
}
}
else {
if (!light_nomal (V[j].x, V[j].y)) {
ok = false; break;
}
}
}
}
if (!ok) {
for (int j=0; j<cnt; ++j) {
if (j == k) back_special (V[j].x, V[j].y, l);
else back_nomal (V[j].x, V[j].y);
}
continue;
}
bool flag = true;
for (int j=0; j<cnt; ++j) {
int x = V[j].x, y = V[j].y;
if (!vis[x][y]) {
flag = false; break;
}
}
if (flag) {
ans = min (ans, __builtin_popcount (i));
}
for (int j=0; j<cnt; ++j) {
if (j == k) back_special (V[j].x, V[j].y, l);
else back_nomal (V[j].x, V[j].y);
}
}
}
}
printf ("%d\n", ans == INF ? -1 : ans);
} return 0;
}
状态压缩 + 暴力 HDOJ 4770 Lights Against Dudely的更多相关文章
- HDOJ 4770 Lights Against Dudely
状压+暴力搜索 Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- hdu 4770 Lights Against Dudely(回溯)
pid=4770" target="_blank" style="">题目链接:hdu 4770 Lights Against Dudely 题 ...
- HDU 4770 Lights Against Dudely 暴力枚举+dfs
又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...
- HDU 4770 Lights Against Dudely (2013杭州赛区1001题,暴力枚举)
Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 4770 Lights Against Dudely
Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 4770 Lights Against Dudely(暴力+状压)
思路: 这个题完全就是暴力的,就是代码长了一点. 用到了状压,因为之前不知道状压是个东西,大佬们天天说,可是我又没学过,所以对状压有一点阴影,不过这题中的状压还是蛮简单的. 枚举所有情况,取开灯数最少 ...
- POJ 1753 Flip Game 状态压缩,暴力 难度:1
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4863 Accepted: 1983 Descript ...
- HDU_4770 Lights Against Dudely 状压+剪枝
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 Lights Against Dudely Time Limit: 2000/1000 MS ( ...
- hdu 2489 最小生成树状态压缩枚举
思路: 直接状态压缩暴力枚举 #include<iostream> #include<algorithm> #include<cstdio> #include< ...
随机推荐
- redis和memcached比较
1.Memcached采用客户端-服务器的架构,服务器维护了一个键-值关系的数据表,服务器之间相互独立,互相之间不共享数据也不做任何通讯操作.客户端需要知道所有的服务器,并自行负责管理数据在各个服务器 ...
- java笔记--适配器模式的运用
适配器模式的运用 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3884785.html "谢谢-- 主要应用: 可以在符合 ...
- 新一代 Tor发布,它牛在哪里?
导读 知名匿名搜索引擎Tor,最近发布了基于火狐浏览器45-ESR的6.0版本,增强了对HTML5的支持,并更新了用来保护加密流量及其更新机制的安全功能.火狐45-ESR版本的全称为Firefox E ...
- Resumable Media Uploads in the Google Data Protocol
Eric Bidelman, Google Apps APIs team February 2010 Introduction The Resumable Protocol Initiating a ...
- Backpack | & ||
Backpack | Given n items with size Ai, an integer m denotes the size of a backpack. How full you can ...
- PHP exit() 输出
2014年8月6日 10:41:00 exit($a); 当$a是bool类型,整形的时候,浏览器里是看不到任何输出的 当$a是字符串的时候浏览器里是可以看到输出的 $a = 1; exit($a); ...
- Windows下的cmd命令行中设置环境编码
我们都知道,Windows下的cmd命令行默认编码是Windows系统的编码,就是ANSI编码或者说是GBK编码的,这样我们编写的很多应用比如php编写utf-8编码的应用在命令行下面运行时都会出现乱 ...
- php接口和多态的概念以及简单应用
接口是面向对象中的一个重要特性,也是面向对象开发不可缺少的一个概念,下面简单说一下接口的概念,先看一段简单的代码: interface ICanEat { public function eat($f ...
- codeforces A. Flipping Game 解题报告
题目链接:http://codeforces.com/problemset/problem/327/A 题意是输入一个只有0和1的序列,要求找出一个合理的区间,在这个区间里面把0变成1,1变成0,使得 ...
- 细胞分裂(codevs 2952)
题目描述 Description 著名生物学家F博士发现了一种单细胞生物. 它长得像蚯蚓,分裂速度极快(每分钟一次),分裂也像蚯蚓一样,断成两段,再长成. 它很好斗,只要q只聚集在一起,就会q只一群打 ...