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感觉就是函数的 ...
随机推荐
- STM32 BootLoader升级固件
一.知识点 1.BootLoader就是单片机启动时候运行的一段小程序,这段程序负责单片机固件的更新,也就是单片机选择性的自己给自己下程序.可以更新,也可以不更新,更新的话,BootLoader更新完 ...
- A Child's History of England.20
CHAPTER 7 ENGLAND UNDER HAROLD THE SECOND, AND CONQUERED BY THE NORMANS Harold was crowned King of E ...
- absorb
absorb 物理的absorb比较直观.被书本/知识absorb也好理解.涉及到money/time时有点抽象,但汉语也有"吸金"的说法,consume, use up.可以吸收 ...
- act
act的词源是do, 干着或干了的事情也可以叫act.抄全字典的话,抄的和看的都麻烦,在阅读中体会吧. act和action有啥区别?action: doing sth; act: n. action ...
- MBean代码例子
public class ServerImpl { public final long startTime; public ServerImpl() { startTime = System.curr ...
- 【HarmonyOS】【DevEco Studio】NOTE04:How to Jump to a Page(页面间的跳转)
页面创建与基本设置 创建页面 创建两个新页面,分别为AbilityPage1.AbilityPage2 设置页面基本内容 以AbilityPage1为例 导包 import com.example.m ...
- 使用Modbus批量读取寄存器地址
使用modbus单点读取地址是轮询可能会导致效率很低,频繁发送读取报文会导致plc响应时间拉长,批量读取可大大减少数据通信的过程,每次读取完成后,在内存中异步处理返回来的数据数组. modbus 功能 ...
- Snort 入侵检测系统
Snort 入侵检测系统 一.实验目的 1.掌握snort IDS工作原理 2.应用snort 三种方式工作 二.实验环境 系统环境:Windows环境, kali环境 三.实验原理 1.snort ...
- Redis操作命令合集
目录 一.客户端命令 二.sql命令 一.客户端命令 #读取配置文件启动 redis-server redis.conf #关闭 Redis,Redis服务器将断开与客户端链接,产生持久化文件,平滑关 ...
- 分布式事务之TCC事务模型
一.引言 在上篇文章<老生常谈--利用消息队列处理分布式事务>一文中留了一个坑,今天来填坑.如下图所示 如果服务A和服务B之间是同步调用,比如服务C需要按流程调服务A和服务B,服务A和服务 ...