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

题目大意:在一个N*M的银行里。有N*M个房间,‘#’代表牢固的房间,‘.‘代表的是脆弱的房间。脆弱的房间个数不会超过15个,如今为了确保安全,要在若干个脆弱的房间上装灯。普通的灯是照亮{0, 0}, {-1, 0}, {0, 1}(和题目中坐标有点出入)。然后能够装一个特殊的,能够照耀

  • { {0, 0}, {0, 1}, {1, 0} },
  • { {0, 0}, {-1, 0}, {0, -1} },
  • { {0, 0}, {0, -1}, {1, 0} }

    同一个房间不能够装两栈灯,灯光不能照耀牢固的房间,问说最少须要多少栈灯。

解题思路:dfs+剪枝。暴力枚举放特殊灯的位置,然后将脆弱房间依照i坐标大放前面,相等的将j坐标小的方前面,这样做是为了dfs的时候剪枝,仅仅要碰到一个房间不能放就返回。

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = 200;
const int maxv = 20;
const int INF = 0x3f3f3f3f;
const int dir[4][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} }
}; int ans;
int n, N, M, x[maxv], y[maxv], c[maxv];
int v[maxn+5][maxn+5];
char g[maxn+5][maxn+5]; inline int judge (int xi, int yi, const int d[3][2]) { for (int i = 0; i < 3; i++) {
int p = xi + d[i][0];
int q = yi + d[i][1]; if (p <= 0 || p > N)
continue; if (q <= 0 || q > M)
continue; if (g[p][q] == '#')
return 0;
}
return 1;
} inline void set (int xi, int yi, const int d[3][2], int type) {
for (int i = 0; i < 3; i++) {
int p = xi + d[i][0];
int q = yi + d[i][1]; if (p <= 0 || p > N)
continue; if (q <= 0 || q > M)
continue; v[p][q] = type;
}
} void init () {
n = 0;
for (int i = 1; i <= N; i++)
scanf("%s", g[i] + 1); for (int i = N; i; i--) {
for (int j = 1; j <= M; j++) {
if (g[i][j] == '.') {
x[n] = i;
y[n] = j;
n++;
}
}
} memset(c, 0, sizeof(c));
for (int i = 0; i < n; i++)
c[i] = judge(x[i], y[i], dir[0]);
} /*
int solve (int spi, int id) { memset(v, 0, sizeof(v));
int ans = INF;
for (int s = 0; s < (1<<n); s++) { bool flag = true;
for (int i = 0; i < n; i++) {
if (s&(1<<i) && (c[i] == 0 || i == spi)) {
flag = false;
break;
}
} if (flag) {
int light = 0;
int tmp = set(x[spi], y[spi], dir[id], 1); for (int i = 0; i < n; i++) {
if (s&(1<<i)) {
light++;
tmp += set(x[i], y[i], dir[0], 1);
}
} if (tmp == n)
ans = min(ans, light); memset(v, 0, sizeof(v));
}
}
return ans+1;
}
*/ void dfs (int d, int f, int cnt) { if (cnt >= ans)
return; if (d == n) {
ans = cnt;
return;
} if (v[x[d]][y[d]])
dfs (d + 1, f, cnt); if (c[d] && d != f) {
set(x[d], y[d], dir[0], 1);
dfs (d + 1, f, cnt+1);
set(x[d], y[d], dir[0], 0);
}
} int main () {
while (scanf("%d%d", &N, &M) == 2 && N + M) {
init(); ans = INF;
if (n == 0)
ans = 0; for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
if (judge (x[i], y[i], dir[j])) {
memset(v, 0, sizeof(v)); set(x[i], y[i], dir[j], 1); dfs(0, i, 1); set(x[i], y[i], dir[j], 0);
}
}
} if (ans == INF)
printf("-1\n");
else
printf("%d\n", ans);
}
return 0;
}

hdu 4770 Lights Against Dudely(回溯)的更多相关文章

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

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

  2. HDU 4770 Lights Against Dudely

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

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

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

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

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

  5. HDOJ 4770 Lights Against Dudely

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

  6. HDU 4770 Lights Against DudelyLights

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

  7. 状态压缩 + 暴力 HDOJ 4770 Lights Against Dudely

    题目传送门 题意:有n*m的房间,'.'表示可以被点亮,'#'表示不能被点亮,每点亮一个房间会使旁边的房间也点亮,有意盏特别的灯可以选择周围不同方向的房间点亮.问最少需要多少灯使得所有房间点亮 分析: ...

  8. HDU_4770 Lights Against Dudely 状压+剪枝

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

  9. hdu4770:Lights Against Dudely(回溯 + 修剪)

    称号:hdu4770:Lights Against Dudely 题目大意:相同是n*m的矩阵代表room,房间相同也有脆弱和牢固之分,如今要求要保护脆弱的房间.须要将每一个脆弱的房间都照亮,可是牢固 ...

随机推荐

  1. html5上传图片php特殊处理一下

    $file=$_POST['file']; if($file) { preg_match('|data:image/([a-zA-Z]+);base64,|s',$file,$match); $fil ...

  2. UI Watcher 解决不定时弹窗问题

    缘来是你: 在基于UI谷歌库的测试系统对第三方APK测试例,存在不定时弹窗问题,对测试例的健壮性和稳定性产生了很大影响. 为了解决这个问题,谷歌开源了UIwatcher 类来解决此问题. 附谷歌官网类 ...

  3. POJ 3422 Kaka's Matrix Travels(费用流)

    Kaka's Matrix Travels Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6792   Accepted:  ...

  4. IOS Reachability判断所请求服务器是否超时?

    开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息.如果没有处理它们,是不会通过Apple的审查的. Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法. ...

  5. [Android Memory] Shallow Heap大小计算释疑

    转载自:http://blog.csdn.net/sodino/article/details/24186907 查看Mat文档时里面是这么描述Shallow Heap的:Shallow heap i ...

  6. PostgreSQL配置文件--QUERY TUNING

    5 QUERY TUNING 5.1 Planner Method Configuration. 下列参数控制查询优化器是否使用特定的存取方法.除非对优化器特别了解,一般情况下,使用它们默认值即可. ...

  7. mac下如何全量删除短信内容

    退出messages应用 执行命令:rm -r ~/Library/Messages/chat.* 重启messages 如果遇到下面问题:重启mac

  8. 兄弟连新版Linux视频教程

    兄弟连新版Linux视频教程目录:F:\linux\兄弟连新版Linux视频教程├(1)云计算是什么东西集群又是嘛玩意?.mp4├(2)兄弟连新版Linux教程 1.1.1 Linux系统简介-UNI ...

  9. python爬虫之scrapy框架

    Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的, 也可以 ...

  10. javat Itext实践 pdf

    1.简介 iText是著名的开放项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转化为PDF文件. 下载地址:https:/ ...