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. k8s集群中部署Rook-Ceph高可用集群

    先决条件 为确保您有一个准备就绪的 Kubernetes 集群Rook,您可以按照这些说明进行操作. 为了配置 Ceph 存储集群,至少需要以下本地存储选项之一: 原始设备(无分区或格式化文件系统) ...

  2. 1小时学会Git玩转GitHub

    版权声明:原创不易,本文禁止抄袭.转载,侵权必究! 本次教程建议一边阅读一边用电脑实操 目录 一.了解Git和Github 1.1 什么是Git 1.2 什么是版本控制系统 1.3 什么是Github ...

  3. day29并发编程

    day29并发编程 1.进程锁 一.使用锁维护执行顺序 代码: from multiprocessing import Process,Lock import os import time def t ...

  4. 【leetcode】1217. Minimum Cost to Move Chips to The Same Position

    We have n chips, where the position of the ith chip is position[i]. We need to move all the chips to ...

  5. ebs 初始化登陆

    BEGIN fnd_global.APPS_INITIALIZE(user_id => youruesr_id, esp_id => yourresp_id, resp_appl_id = ...

  6. sql技巧(增册改查)

    1 select * from wyl.t; 2 --将数据从t1导入t2 3 insert into t2(c1,c2) select c1,c2 from t1 where c1= xx and ...

  7. 【JAVA】【JVM】内存结构

    虽然jvm帮我们做了内存管理的工作,但是我们仍需要了解jvm到底做了什么,下面我们就一起去看一看 jvm启动时进行一系列的工作,其中一项就是开辟一块运行时内存.而这一块内存中又分为了五大区域,分别用于 ...

  8. react-native安卓运行报错:The number of method references in a .dex file cannot exceed 64K.

    错误原因:App里面方法数超过64K解决方法:在android/app/build.gradle中添加implementation 'com.android.support:multidex:1.0. ...

  9. 罗德与施瓦茨公司和TSN Systems公司为车载以太网提供纳秒级精度延时测量

    前言 随着毫米波雷达.激光雷达和摄像头等传感器的大量出现,并要求海量的传感器数据在几毫秒内传输完成并处理,使得网络延迟问题变得越发重要.测试和测量的专家Rohde&Schwarz(以下简称R& ...

  10. Iphone5, 6 and 6Plus尺寸

    1.iPhone5分辨率320x568,像素640x1136,@2x 2.iPhone6分辨率375x667,像素750x1334,@2x 3.iPhone6 Plus分辨率414x736,像素124 ...