1092 - Lighted Panels
| Time Limit: 3 second(s) | Memory Limit: 32 MB |
You are given an R x C 2D grid consisting of several light panels. Each cell contains either a '*' or a '.'. '*' means the panel is on, and '.' means it's off. If you touch a panel, its state will be toggled. That means, if you touch a panel that's on, it will turn off, and if you touch a panel that's off, it will turn on. But if we touch a panel, all its horizontal, vertical, and diagonal adjacent panels will also toggle their states.
Now you are given the configuration of the grid. Your goal is to turn on all the lights. Print the minimum number of touches required to achieve this goal.
Input
Input starts with an integer T (≤ 125), denoting the number of test cases.
Each test case starts with two integers R (1 ≤ R ≤ 8) and C (1 ≤ C ≤ 8). Then there will be R lines each containing C characters ('*' or '.').
Output
For each test case, print the case number and the minimum number of touches required to have all the light panels in the board on at the same time. If it is not possible then print "impossible".
Sample Input |
Output for Sample Input |
|
4 5 5 ***** *...* *...* *...* ***** 1 2 .* 3 3 **. **. ... 4 4 *... **.. ..** ...* |
Case 1: 1 Case 2: impossible Case 3: 2 Case 4: 10 |
思路:状压枚举;
由于是8个方向的所以不能像以前那样只枚举第一行,但是我们可以将第一行和第一列一起枚举,这样就可以通过dp[x-1][y-1]来断定dp[x][y]是否要翻过来。
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<string.h>
6 #include<queue>
7 #include<math.h>
8 using namespace std;
9 char str[20][20];
10 int ak[20][20];
11 int ap[20][20];
12 int minn=1e9;
13 int slove(int n,int m);
14 void Init(int n,int m);
15 int main(void)
16 {
17 int i,j,k;
18 scanf("%d",&k);
19 int s;
20 int n,m;
21 for(s=1; s<=k; s++)
22 {
23 scanf("%d %d",&n,&m);
24 for(i=0; i<n; i++)
25 {
26 scanf("%s",str[i]);
27 }
28 for(i=0; i<n; i++)
29 {
30 for(j=0; j<m; j++)
31 {
32 if(str[i][j]=='*')
33 {
34 ak[i][j]=1;
35 }
36 else ak[i][j]=0;
37 }
38 }
39 int ac=slove(n,m);
40 if(ac!=1e9)
41 printf("Case %d: %d\n",s,ac);
42 else printf("Case %d: impossible\n",s);
43 }
44 return 0;
45 }
46 void Init(int n,int m)
47 {
48 int i,j;
49 for(i=0; i<n; i++)
50 {
51 for(j=0; j<m; j++)
52 {
53 ap[i][j]=ak[i][j];
54 }
55 }
56 }
57 int slove(int n,int m)
58 {
59 int i,j,k;
60 int ask=0;
61 int minn=1e9;
62 for(i=0; i<(1<<(n+m-1)); i++)
63 {
64 ask=0;
65 Init(n,m);
66 int xx,yy;
67 for(j=0; j<n; j++)
68 {
69 if(i&(1<<j))
70 {
71 ask++;
72 ap[j][0]=ap[j][0]+1;
73 ap[j][0]%=2;
74 if(j>=1)
75 {
76 ap[j-1][0]=(ap[j-1][0]+1)%2;
77 ap[j-1][1]=(ap[j-1][1]+1)%2;
78 }
79 ap[j+1][0]=(ap[j+1][0]+1)%2;
80 ap[j+1][1]=(ap[j+1][1]+1)%2;
81 ap[j][1]=(ap[j][1]+1)%2;
82 }
83 }
84 for(j=n; j<(n+m-1); j++)
85 {
86 int s=j-n+1;
87 if(i&(1<<j))
88 {
89 ask++;
90 ap[0][s]=(ap[0][s]+1)%2;
91
92 if(s>=1)
93 {
94 ap[0][s-1]=(ap[0][s-1]+1)%2;
95 ap[1][s-1]=(ap[1][s-1]+1)%2;
96 }
97 ap[1][s]=(ap[1][s]+1)%2;
98 ap[1][s+1]=(ap[1][s+1]+1)%2;
99 ap[0][s+1]=(ap[0][s+1]+1)%2;
100 }
101 }
102 int x,y;
103 for(x=1; x<n; x++)
104 {
105 for(y=1; y<m; y++)
106 {
107 if(ap[x-1][y-1]==0)
108 {
109 ap[x-1][y-1]=1;
110 ask++;
111 ap[x][y]=(ap[x][y]+1)%2;
112 ap[x-1][y]=(ap[x-1][y]+1)%2;
113 ap[x][y-1]=(ap[x][y-1]+1)%2;
114 ap[x-1][y+1]=(ap[x-1][y+1]+1)%2;
115 ap[x+1][y+1]=(ap[x+1][y+1]+1)%2;
116 ap[x][y+1]=(ap[x][y+1]+1)%2;
117 ap[x+1][y]=(ap[x+1][y]+1)%2;
118 ap[x+1][y-1]=(ap[x+1][y-1]+1)%2;
119 }
120 }
121 }
122 int flag=0;
123 for(x=0;x<n;x++)
124 {
125 for(y=0;y<m;y++)
126 {
127 if(!ap[x][y])
128 {flag=1;break;}
129 }
130 if(flag)break;
131 }
132 if(!flag)
133 {
134 minn=min(minn,ask);
135 }
136 }
137 return minn;
138 }
1092 - Lighted Panels的更多相关文章
- Bootstrap <基础二十九>面板(Panels)
Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素添加 class .panel 和 class .pa ...
- panels能否包含views_block ////// panels -- content pane 参数传递
是可以的包含block,不管是手动在block后台创建的,还是通过views创建的block,都可以在Panel add content的时候添加. ------------ panels 和 con ...
- sprytabbedpanels.js库之在页面中插入Tabbed Panels
向页面加入sprytabbedpanels.js文件.<script src="SpryAssets/SpryTabbedPanels.js" type="text ...
- 51nod 1092 回文字符串 (dp)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092 这个题是poj-3280的简化版,这里只可以增加字符,设 dp[i ...
- Demo学习: Collapsible Panels
Collapsible Panels 设置TUniPanel布局属性,布局属性在Ext里是比较常用的属性,当前版本虽已经提供了布局功能,但很不完善,比如当Panel.TitlePosition=tpR ...
- [swustoj 1092] 二分查找的最大次数
二分查找的最大次数(1092) 问题描述 这里是一个在排序好的数组A(从小到大)中查找整数X的函数,返回值是查找次数. int binarySearch(inta[],int n,int x)//数组 ...
- 1092: 最大价值(dollars) 算法 动态规划
题目地址:http://www.hustoj.com/oj/problem.php?id=1092 题目描述 Dave以某种方法获取了未来几天美元对德国马克的兑换率.现在Dave只有100美元,请编程 ...
- 1042 数字0-9的数量 1050 循环数组最大子段和 1062 序列中最大的数 1067 Bash游戏 V2 1092 回文字符串
1042 数字0-9的数量 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 给出一段区间a-b,统计这个区间内0-9出现的次数. 比如 10-19,1出现11次 ...
- cdoj 1092 韩爷的梦
http://acm.uestc.edu.cn/#/problem/show/1092 题意:略 思路: 做的第一道字符串hash的题,真是菜啊,还是看了几篇题解才会做的.字符串hash感觉就是函数的 ...
随机推荐
- oracle中char],varchar,varchar2
VARCHAR.VARCHAR2.CHAR的区别 1.CHAR的长度是固定的,而VARCHAR2的长度是可以变化的, 比如,存储字符串"abc",对于CHAR (20),表示你存储 ...
- Git五个常见问题及解决方法
一.删除远程仓库上被忽略的文件 由于种种原因,一些本应该被忽略的文件被我们误操作提交到了远程仓库了.那么我们该怎么删除这些文件呢? 以误提交了.idea目录为例,我们可以通过下面的步骤处理: 1)我们 ...
- 前端知识,什么是BFC?
BFC全称是Block Formatting Context,即块格式化上下文.它是CSS2.1规范定义的,关于CSS渲染定位的一个概念.要明白BFC到底是什么,首先来看看什么是视觉格式化模型. 视觉 ...
- 栈常考应用之括号匹(C++)
思路在注释里.还是使用链栈的API,为啥使用链栈呢,因为喜欢链栈. //header.h #pragma once #include<iostream> using namespace s ...
- Assertion failure in -[UISectionRowData refreshWithSection:tableView:tableViewRowData:], /Source
1. *** Assertion failure in -[UISectionRowData refreshWithSection:tableView:tableViewRowData:], /Sou ...
- IDEA 超实用使用技巧分享
前言 工欲善其事 必先利其器 最近受部门的邀请,给入职新人统一培训IDEA,发现有很多新人虽然日常开发使用的是IDEA,但是还是很多好用的技巧没有用到,只是用到一些基本的功能,蛮浪费IDEA这个优 ...
- tomcat结合nginx
相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能很多人都听说,但具体什么是反向代理,很多人估计就不清楚了 ...
- Spring中的InitializingBean与DisposableBean
InitializingBean顾名思义,应该是初始化Bean相关的接口. 先看一下该接口都定义了哪些方法: public interface InitializingBean { void afte ...
- show processlist命令详解
1.show processlist; SHOW PROCESSLIST显示哪些线程正在运行.您也可以使用mysqladmin processlist语句得到此信息.如果您有SUPER权限,您可以看到 ...
- 【C#】【MySQL】C#连接MySQL数据库(一)代码
C#连接MySQL数据库 准备工作 1.环境安装 安装MySQL For Visual Studio<<点击进入官网下载 第一个要下载安装,第二个下载后将MySQL.data添加到Visu ...