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. Problem C: 更改大小写

    #include<stdio.h> int main() { ; ]; gets(a); while(a[i]!='\0') { ; ++i; } printf("%s" ...

  2. Codeforces Round #339 (Div. 1) B. Skills 暴力 二分

    B. Skills 题目连接: http://www.codeforces.com/contest/613/problem/B Description Lesha plays the recently ...

  3. 8VC Venture Cup 2016 - Elimination Round E. Simple Skewness 暴力+二分

    E. Simple Skewness 题目连接: http://www.codeforces.com/contest/626/problem/E Description Define the simp ...

  4. WinCE6.0 2012年补丁下载地址

    Windows CE6.0 2012年补丁包WinCEPB60-121231-Product-Update-Rollup-Armv4I.msi下载地址:http://www.microsoft.com ...

  5. HTTPS 信任证书

    使用HttpsURLConnection访问HTTPS链接时一般需要引入证书,否则会产生异常. 但是也可以使用信任所有证书的方式来达到访问的目的. 经上网查询资料发现一个很好用的类来实现信任所有证书的 ...

  6. Quartz_TimeJob例子(C#)

    执行入口: using System; using System.Collections.Generic; using log4net; using Quartz; using ypt_base.Co ...

  7. 面经 | 我是如何通过校招拿到京东的Offer的

    本文来自粉丝投稿,原作者:红鼻子熊. 版权归Hollis所有. OFFER:京东2018校招物流研发岗 个人:211小硕 面试时间:2017年秋天 整体:三轮面试,前两轮为技术面试,最后为hr面试 一 ...

  8. Google Chrome插件开发-Context Menus

    本节主要介绍如何在Google Chrome浏览器web页面上点击右键弹出自定义菜单,即如何使用谷歌Context Menus API接口.上节已经把主要流程介绍了,这节就直接上代码,代码都是官方例子 ...

  9. python matplotlib.pyplot学习记录

    matplotlib是python中很强大的绘图工具,在机器学习中经常用到 首先是导入 import matplotlib.pyplot as plt plt中有很多方法,记录下常用的方法 plt.p ...

  10. tag subshader shaderlab

    unity的黑科技 https://docs.unity3d.com/Manual/SL-SubShaderTags.html 这里 reflectCamera.RenderWithShader(re ...