题目链接: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;
}

hdu 4770(枚举 + dfs爆搜)的更多相关文章

  1. HDU 4403 A very hard Aoshu problem(dfs爆搜)

    http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比 ...

  2. hdu 5506 GT and set(dfs爆搜)

    Problem Description You are given N sets.The i−th set has Ai numbers.You should divide the sets into ...

  3. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  4. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

  5. Ancient Go---hdu5546(dfs爆搜CCPC题目)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546 题意就是两个人下围棋,问在下一颗x是否能杀死o,'.'是空位子: 枚举所有的点,判断是否合法即可 ...

  6. 【BZOJ-1060】时态同步 树形DP (DFS爆搜)

    1060: [ZJOI2007]时态同步 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2101  Solved: 595[Submit][Statu ...

  7. POJ3185 The Water Bowls(反转法or dfs 爆搜)

    POJ3185 The Water Bowls 题目大意: 奶牛有20只碗摆成一排,用鼻子顶某只碗的话,包括左右两只在内的一共三只碗会反向,现在给出碗的初始状态,问至少要用鼻子顶多少次才能使所有碗都朝 ...

  8. 火车进栈(进出栈的模拟,dfs爆搜)

    这里有n列火车将要进站再出站,但是,每列火车只有1节,那就是车头. 这n列火车按1到n的顺序从东方左转进站,这个车站是南北方向的,它虽然无限长,只可惜是一个死胡同,而且站台只有一条股道,火车只能倒着从 ...

  9. 【csp模拟赛2】 爆搜 方格加数

    [题目描述] xyz1048576正在玩一个关于矩阵的游戏. 一个n*m的矩阵,矩阵中每个数都是[1,12]内的整数.你可以执行下列两个操作任意多次: (1)指定一行,将该行所有数字+1. (2)指定 ...

随机推荐

  1. ios 引入第三方库 运行时找不到函数实现

    今天引入webtrends 这个库,结果一直运行时找不到函数,纳闷了一下午! 后来发现,是other flag没有设置对,对于那些包含category用来扩展ios原始类型的库,链接的时候需要特别的链 ...

  2. ios 中使用https的知识

    先看文章,这篇文章说的是使用AFNetworing进行https时的事项,十分好!http://blog.cnbang.net/tech/2416/ ios中使用https,主要就是使用NSURLCr ...

  3. ios 跟踪UITextField更改的简单方法

    如图,用xib链接,用到的消息是Editing Changed 消息.

  4. perl 判断数组相等的三种方法

    1.数组相等,数组成员相同,位置也相同 一般的如果判断@array1 等于 @array2 a.数组长度相同 $#array1=$#array2, 比较数组长度,不能使用length函数,length ...

  5. Match:Cyclic Nacklace(KMP的next数组的高级应用)(HDU 3746)

    串珠子 题目大意:给定一个字串,要你找到如果要使之成为循环串,在末尾需要的最小的字数(只能添加字符,不能删减字符) 首先联动一下之前做过的动态规划问题POJ 3280,当然了3280这一题是用的LD, ...

  6. LeetCode 171 Excel Sheet Column Number

    Problem: Given a column title as appear in an Excel sheet, return its corresponding column number. F ...

  7. August 18th 2016 Week 34th Thursday

    Comedy is acting out optimism. 喜剧就是将乐观演绎出来. Being optimistic or pessimistic, that is all about your ...

  8. 43个优秀的Swift开源项目

    作为一门集百家之长的新语言,Swift拥有着苹果先天的生态优势,而其在GitHub上各种优秀的开源项目也层出不穷.本文作者@SwiftLanguage从2014年6月苹果发布Swift语言以来,便通过 ...

  9. XMPP框架下微信项目总结(1)环境配置

    xmpp介绍 xmpp基于模块开发的 无须自己写请求 (登陆,注册,获取好友列表,添加/删除好友等) ------>简介 ------------------------- ----->工 ...

  10. 通过NSString初始化OC类

    NSString *className = self.classNames[indexPath.section][indexPath.row];    BaseMapViewController *s ...