首页 技术 正文
技术 2022年11月6日
0 收藏 555 点赞 1,128 浏览 1880 个字

题目链接: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;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,286