hdu 4770(枚举 + dfs爆搜)
来源:互联网 发布:ftp防火墙端口设置 编辑:程序博客网 时间:2024/06/05 19:21
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770
思路:由于最多只有15个".",可以直接枚举放置的位置,然后判断是否能够全部点亮即可。需要注意的是,有一个特殊的light,也需要枚举它的位置以及放置的方向。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAX_N = (200 + 22);int N, M;struct Node {int x, y;Node() {}Node(int _x, int _y) : x(_x), y(_y) {}}node[17];int n;char g[MAX_N][MAX_N];/*if kind == 0: 0 degree.if kind == 1: 90 degree.if kind == 2: 180 degree.if kind == 3: 270 degree.*/bool Judge(int x, int y){if (x < 0 || x >= N || y < 0 || y >= M) return true;if (g[x][y] == '.') return true;return false;}int find(int x, int y){for (int i = 0; i < n; ++i) {if (node[i].x == x && node[i].y == y) return i;}return -1;}bool vis[17];bool gao(int s, int kind){for (int i = 0; i < n; ++i) if ((1 << i) & s) {memset(vis, false, sizeof(vis));vis[i] = true;int x = node[i].x, y = node[i].y;if (kind == 0) {if (!Judge(x - 1, y) || !Judge(x, y + 1)) continue;if (x - 1 >= 0)vis[find(x - 1, y)] = true;if (y + 1 < M) vis[find(x, y + 1)] = true;} else if (kind == 1) {if (!Judge(x, y + 1) || !Judge(x + 1, y)) continue;if (y + 1 < M) vis[find(x, y + 1)] = true;if (x + 1 < N) vis[find(x + 1, y)] = true;} else if (kind == 2) {if (!Judge(x + 1, y) || !Judge(x, y - 1)) continue;if (x + 1 < N) vis[find(x + 1, y)] = true;if (y - 1 >= 0) vis[find(x, y - 1)] = true;} else if (kind == 3) {if (!Judge(x, y - 1) || !Judge(x - 1, y)) continue;if (y - 1 >= 0) vis[find(x, y - 1)] = true;if (x - 1 >= 0) vis[find(x - 1, y)] = true;}for (int j = 0; j < n; ++j) {if (j != i && ((1 << j) & s)) {x = node[j].x, y = node[j].y;vis[j] = true;if (!Judge(x - 1, y) || !Judge(x, y + 1)) break;if (x - 1 >= 0)vis[find(x - 1, y)] = true;if (y + 1 < M) vis[find(x, y + 1)] = true;}}bool tag = true;for (int i = 0; i < n; ++i) if (!vis[i]) {tag = false; break;}if (tag) return true;}return false;}int main(){while (~scanf("%d %d", &N, &M)) {if (N == 0 && M == 0) break;n = 0;for (int i = 0; i < N; ++i) {scanf("%s", g[i]);for (int j = 0; j < M; ++j) if (g[i][j] == '.') {node[n++] = Node(i, j);}}if (n == 0) {puts("0");continue;}int ans = -1;for (int s = 0; s < (1 << n); ++s) {for (int kind = 0; kind < 4; ++kind) {if (gao(s, kind)) {int x = s, cnt = 0;while (x) {++cnt;x &= (x - 1);}ans = (ans == -1 ? cnt : min(cnt, ans));}}}printf("%d\n", ans);}return 0;}
0 0
- hdu 4770(枚举 + dfs爆搜)
- hdu 4770 Lights Against Dudely(暴力枚举dfs)
- hdu 4431 Mahjong (模拟,枚举+dfs)
- HDU 5301 Friends(DFS + 枚举)
- hdu 2208 DFS枚举
- hdu 4152 dfs 枚举
- hdu 2489 (dfs枚举 + prim)
- hdu 5339 Untitled(枚举 || DFS)
- HDU Untitled(状压DP OR dfs枚举子集)
- 杭电 HDU ACM 1015 Safecracker (dfs 枚举排列)
- HDU 4152 ZZYs Dilemma(枚举 or dfs)
- 暴力枚举,dfs(三角形篱笆,HDU 4277)
- 暴力枚举,dfs(连连看,HDU 4272)
- HDU 2208 唉,可爱的小朋友 (DFS枚举)
- HDU 2489 DFS枚举+最小生成树
- HDU 1427 速算24点 枚举+dfs
- HDU 1298 T9 // 字典树,枚举,dfs
- HDU 3768 Shopping spfa+dfs枚举
- 猩猩BOSS的故事系列二:让你的孩子远离成人化妆品
- JAVA基础6(代码剖析)
- 指针数组和数组指针
- 二进制历史
- C语言回调函数学习
- hdu 4770(枚举 + dfs爆搜)
- 黑洞数字495
- OpenSSL RSA 消息签名与验证
- First Missing Positive
- as3中为什么要用各种Listener实现?
- java读取web页面信息
- 伙伴算法
- OpenSSL Base64编码与解码
- 解释与编译