题目链接: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. json和对象、list互转

    1.把对象,list转成json: import com.alibaba.fastjson.JSONObject; Object obj = new Object(); // ... String o ...

  2. python 3.5.2安装mysql驱动报错

    python 3.5.2安装mysql驱动报错 python 3.5.2安装mysql驱动时出现如下异常: [root@localhost www]# pip install mysql-connec ...

  3. android通过Canvas和Paint截取无锯齿圆形图片

    一个通过Canvas和Paint截取无锯齿圆形图片. /** * 根据原图和变长绘制圆形图片 * * @param source * @param min * @return */ public st ...

  4. H5 前端页面适配响应式

    辞职有半个月了,面试了几家公司,还在挣扎中.... 不废话,H5页面适配成响应式,可以用百分比或者rem. rem是相对于html根元素的单位,可以根据根元素的大小做出等比缩放, 通常,假如设置,ht ...

  5. 【leetcode】Flatten Binary Tree to Linked List (middle)

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  6. yum Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again

    今天使用yum时出现如上问题,解决办法: 1.编辑:/etc/yum.repos.d/epel.repo 将baseurl的注释取消, mirrorlist注释掉,如图:

  7. iOS MRC ARC 内存管理

    转自:http://www.jianshu.com/p/48665652e4e4 1. 什么是内存管理 程序在运行的过程中通常通过以下行为,来增加程序的的内存占用 创建一个OC对象 定义一个变量 调用 ...

  8. Hibernate单向多对一对象关系模型映射

    单向的many-to-one 案例: 班级和学生 父亲和子女 单向:只能由其中一方维护关系 Many-to-one中有many的一方法维护或者体现两者之间的关系. 单向的many-to-one描述学生 ...

  9. August 30th 2016 Week 36th Tuesday

    If you keep on believing, the dreams that you wish will come true. 如果你坚定信念,就能梦想成真. I always believe ...

  10. mongochef如何链接有权限的mongodb3.x数据库

    废话不多说,直接上图: 1.打开mongochef 2.打开的界面是这样的: 3.点击connect,上图红色框中的按钮,不要点下拉三角 4.点击New Connection按钮 5.1:上图标注1, ...