状态压缩 + 暴力 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< ...
随机推荐
- 如何用火车头采集当前页面url网址
首先创建一个标签为本文网址,勾选后面的“从网址中采集”. 选择下面的“正则提取”,点击通配符“(?<content>?)”,这样在窗口中就显示为(?<content>[\s\S ...
- The Dragon of Loowater
The Dragon of Loowater Once upon a time, in the Kingdom of Loowater, a minor nuisance turned into ...
- Maven 安装以及一些开发技巧
解压 apache-maven-3.2.5 在conf ->sites中配置repository 的路径. Eclipse 配置 maven 2. 3. 一些小BUG 或开发技巧 eclipse ...
- iPhone/iOS图片相关(读取、保存、绘制、其它相关)
http://blog.csdn.net/jerryvon/article/details/7526147 20:50:42 一.读取图片 1.从资源(resource)读取 UIImage* ima ...
- Dynamic Morphing Square(动态变形矩阵)
题目描述: 解题思路: 先对输入的N进行判断,是否不小于3,如果小于3,需要继续输入一个新的数,知道输入的N比3大. 第一个打印的矩阵,*号为最外面一圈,其余全为-. 第二个打印的矩阵,*号向内缩减了 ...
- 百度图片爬虫-python版-如何爬取百度图片?
上一篇我写了如何爬取百度网盘的爬虫,在这里还是重温一下,把链接附上: http://www.cnblogs.com/huangxie/p/5473273.html 这一篇我想写写如何爬取百度图片的爬虫 ...
- HLG2035广搜
Diablo Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 42(21 users) Total Accepted: 23(20 us ...
- raw格式镜像文件压缩并转换为qcow2格式
raw格式文件,这个比较占用空间,你可以用以下命令将其压缩并转换成qcow2格式. # virt-sparsify --compress --convert qcow2 ubuntu.img ubun ...
- Flatten Binary Tree to Linked List
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...
- Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right) Example Given &qu ...