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的更多相关文章

  1. Bootstrap <基础二十九>面板(Panels)

    Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素添加 class .panel 和 class .pa ...

  2. panels能否包含views_block ////// panels -- content pane 参数传递

    是可以的包含block,不管是手动在block后台创建的,还是通过views创建的block,都可以在Panel add content的时候添加. ------------ panels 和 con ...

  3. sprytabbedpanels.js库之在页面中插入Tabbed Panels

    向页面加入sprytabbedpanels.js文件.<script src="SpryAssets/SpryTabbedPanels.js" type="text ...

  4. 51nod 1092 回文字符串 (dp)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092 这个题是poj-3280的简化版,这里只可以增加字符,设 dp[i ...

  5. Demo学习: Collapsible Panels

    Collapsible Panels 设置TUniPanel布局属性,布局属性在Ext里是比较常用的属性,当前版本虽已经提供了布局功能,但很不完善,比如当Panel.TitlePosition=tpR ...

  6. [swustoj 1092] 二分查找的最大次数

    二分查找的最大次数(1092) 问题描述 这里是一个在排序好的数组A(从小到大)中查找整数X的函数,返回值是查找次数. int binarySearch(inta[],int n,int x)//数组 ...

  7. 1092: 最大价值(dollars) 算法 动态规划

    题目地址:http://www.hustoj.com/oj/problem.php?id=1092 题目描述 Dave以某种方法获取了未来几天美元对德国马克的兑换率.现在Dave只有100美元,请编程 ...

  8. 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次 ...

  9. cdoj 1092 韩爷的梦

    http://acm.uestc.edu.cn/#/problem/show/1092 题意:略 思路: 做的第一道字符串hash的题,真是菜啊,还是看了几篇题解才会做的.字符串hash感觉就是函数的 ...

随机推荐

  1. 同步阻塞IO模型

    同步阻塞IO模型 有上篇IO模型中的,同步阻塞IO模型,我们能够知道,用户线程发起请求后就一直阻塞的等待 内核完成准备数据.数据拷贝的工作.并且返回成功的指示. 实现 使用java来实现同步阻塞IO模 ...

  2. 如何删除苹果电脑垃圾文件-7个高级技巧释放大量苹果Mac

    硬盘空间用尽是一件很让人头疼的事情,尤其是MacBook Air等设备上的固态硬盘可用的储存空间很少.下面[微IT]为大家介绍7个高级技巧来释放大量的硬盘空间,当然这些高级技巧更改了系统功能和文件,必 ...

  3. ORACLE 获取执行计划的方法

    一.获取执行计划的6种方法(详细步骤已经在每个例子的开头注释部分说明了): 1. explain plan for获取: 2. set autotrace on : 3. statistics_lev ...

  4. Linux基础命令---get获取ftp文件

    get 使用lftp登录ftp服务器之后,可以使用get指令从服务器获取文件.   1.语法       get [-E]  [-a]  [-c] [-O base]  rfile  [-o lfil ...

  5. tomcat 之 session 集群

    官网地址 https://tomcat.apache.org/tomcat-8.5-doc/cluster-howto.html #:配置各tomcat节点 [root@node1 ~]# vim / ...

  6. Vue中的8种组件通信方式

    Vue是数据驱动视图更新的框架,所以对于vue来说组件间的数据通信非常重要. 常见使用场景可以分为三类: 父子组件通信: props / $emit $parent / $children provi ...

  7. ctypes与numpy.ctypeslib的使用

    numpy ctypeslib 与 ctypes接口使用说明 作者:elfin 目录 一.numpy.ctypeslib使用说明 1.1 准备好一个C++计算文件 1.2 ctypeslib主要的五个 ...

  8. Redis集群断电恢复

    再集群整体断点或关闭后,默认启动集群后,会成为孤立的单点,需要删除每个节点的pid文件,node.conf.并将RDB和AOF文件移动出来,再挨个启动每个节点,并用create创建集群脚本,重新创建集 ...

  9. Linux下安装gbd

    目录 一.简介 二.部署 一.简介 gdb是Linux环境下的代码调试工具 二.部署 1.首先检查系统中有没有安装过,有的话用一下命令卸载gdb旧版本 2.安装依赖 yum -y install gc ...

  10. HyperSnips:VSCode上的自动补全神器

    发现一个小众但是巨好用的VSCode自动补全插件:HyperSnips. 作者显然受到了 这位小哥 的启发,将 Vim Ultisnips 的大部分功能搬到了VSCode上.并用 JavaScript ...