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. mysql 中@ 和 @@的区别

    @x 是 用户自定义的变量 (User variables are written as @var_name)@@x 是 global或session变量 (@@global @@session )@ ...

  2. 深度探讨 PHP 之性能

    1.缘起 关于PHP,很多人的直观感觉是PHP是一种灵活的脚本语言,库类丰富,使用简单,安全,非常适合WEB开发,但性能低下.PHP的性能是否真的就 如同大家的感觉一样的差呢?本文就是围绕这么一个话题 ...

  3. C/C++ Qt StandardItemModel 数据模型应用

    QStandardItemModel 是标准的以项数据为单位的基于M/V模型的一种标准数据管理方式,Model/View 是Qt中的一种数据编排结构,其中Model代表模型,View代表视图,视图是显 ...

  4. 动态滑动登陆框-Html+Css+Js

    动态滑动登陆框 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...

  5. DBeaver客户端工具连接Hive

    目录 介绍 下载安装 相关配置 1.填写主机名 2.配置驱动 简单使用 主题设置 字体背景色 介绍 在hive命令行beeline中写一些很长的查询语句不是很方便,急需一个hive的客户端界面工具 D ...

  6. c++string转const char*与char*

    #include <iostream> #include <string> #include <memory> using namespace std; const ...

  7. Oracle中创建DB LINK

    当用户要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中必须创建了远程数据库的dblink,通过dblink本地数据库可以像访问本地数据库一样访问远程数据库表中的数据.下面讲介绍如何在本地数 ...

  8. Activity 详解

    1.活动的生命周期 1.1.返回栈 Android是使用任务(Task)来管理活动的,一个任务就是一组存放在栈里的活动的集合,这个栈也被称作返回栈.栈是一种先进后出的数据结构,在默认情况下,每当我们启 ...

  9. 解决ViewPager与ScrollView 冲突

    ViewPager来实现左右滑动切换tab,如果tab的某一项中嵌入了水平可滑动的View就会让你有些不爽,比如想滑动tab项中的可水平滑动的控件,却导致tab切换. 因为Android事件机制是从父 ...

  10. OpenStack之八: network服务(端口9696)

    注意此处用的一个网络,暂时不用启动第二个网官网地址 https://docs.openstack.org/neutron/stein/install/controller-install-rdo.ht ...