题目传送门

题意:有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的更多相关文章

  1. HDOJ 4770 Lights Against Dudely

    状压+暴力搜索 Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  2. hdu 4770 Lights Against Dudely(回溯)

    pid=4770" target="_blank" style="">题目链接:hdu 4770 Lights Against Dudely 题 ...

  3. HDU 4770 Lights Against Dudely 暴力枚举+dfs

    又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...

  4. HDU 4770 Lights Against Dudely (2013杭州赛区1001题,暴力枚举)

    Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. HDU 4770 Lights Against Dudely

    Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. HDU 4770 Lights Against Dudely(暴力+状压)

    思路: 这个题完全就是暴力的,就是代码长了一点. 用到了状压,因为之前不知道状压是个东西,大佬们天天说,可是我又没学过,所以对状压有一点阴影,不过这题中的状压还是蛮简单的. 枚举所有情况,取开灯数最少 ...

  7. POJ 1753 Flip Game 状态压缩,暴力 难度:1

    Flip Game Time Limit: 1000MS  Memory Limit: 65536K  Total Submissions: 4863  Accepted: 1983 Descript ...

  8. HDU_4770 Lights Against Dudely 状压+剪枝

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 Lights Against Dudely Time Limit: 2000/1000 MS ( ...

  9. hdu 2489 最小生成树状态压缩枚举

    思路: 直接状态压缩暴力枚举 #include<iostream> #include<algorithm> #include<cstdio> #include< ...

随机推荐

  1. Python列表基础

    ==========列表基础=========== 列表中的数据是可以被修改的.字典,元组,集合是不能被修改的. >>> li1=['3edf','dafdas'] >> ...

  2. HDU 1062 Text Reverse(水题,字符串处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 解题报告:注意一行的末尾可能是空格,还有记得getchar()吃回车符. #include< ...

  3. change column to bigint

    今天存储数据的时候报错,发现是3435065640超出了常规int的存储长度, RangeError (3435065640 is out of range for ActiveRecord::Typ ...

  4. [OpenJudge 3063]罪犯问题

    [OpenJudge 3063]罪犯问题 试题描述 一天,警官抓获了N个嫌犯,审问N个罪犯的途中,作为警长助手的你突然发现其中被确定为罪犯的K号人是你曾经出生入死的兄弟,你不能眼睁睁看着他被抓进牢里. ...

  5. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  6. HXOI 2014 PSet 4 Day 1 一模04日1 题解

    1. 最小花费(money.pas/c/cpp) 问题描述 在n个人中,某些人的银行账号之间可以互相转账.这些人之间转账的手续费各不相同.给定这些人之间转账时需要从转账金额里扣除百分之几的手续费,请问 ...

  7. shell实现trim函数-去除字符串两侧的空格(包括tab,space键)

    shell实现trim函数效果去除字符串两侧的空格,以下三个命令等价,都能实现 sed 's/^\s*//' totrim.txt |sed 's/\s*$//'>trimed.txtsed ' ...

  8. 用chrome模拟微信浏览器访问需要OAuth2.0网页授权的页面

    现在很流行微信网页小游戏,用html5制作的小游戏移过来,可以放到微信浏览器中打开,关键是可以做成微信分享朋友圈的形式,大大提高游戏的传播,增强好友的游戏互动. 微信浏览器中打开网页游戏效果还不错,对 ...

  9. 1.python基础入门

    作者:刘耀 出处:http://www.yaomr.com 欢迎转载 提示: 语法基于python3.5版本(会提示2.7版本和3.5版本的区别) Python命令行将以>>>开始, ...

  10. ShortestPath:Layout(POJ 3169)(差分约束的应用)

                布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...