1092 - Lighted Panels

   PDF (English) Statistics Forum
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 }
时间: 2024-10-30 02:27:00

1092 - Lighted Panels的相关文章

Vijos——T 1092 全排列

https://vijos.org/p/1092 描述 输入两个自然数m,n 1<=n<=20,1<=m<=n!输出n个数的第m种全排列. 如 :输入 3 1输出 1 2 3 格式 输入格式 在一行中输入n m 输出格式 一个数列,既n个数的第m种排列每两个数之间空1格 样例1 样例输入1 3 2 Copy 样例输出1 1 3 2 Copy 限制 各个测试点1s 来源 lk 1 #include <cstdio> 2 3 #define LL long long 4 b

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

题目地址:http://www.hustoj.com/oj/problem.php?id=1092 题目描述 Dave以某种方法获取了未来几天美元对德国马克的兑换率.现在Dave只有100美元,请编程序,使Dave通过几天的美元与德国马克的兑换后能得到最多的美元. 输入 第1行包含一个自然数n(l≤n≤I00),表示Dave所知道的兑换率的天数.后面跟着n个自然数A (100≤A≤I000).第i+l行的数A表示第i天的兑换率.它告诉那天他能用100美元购买A马克,或用A马克购买100美元. 输

使用Bootstrap 3开发响应式网站实践04,使用Panels展示内容

在Bootstrap页面中,通常用Panels来展示主要功能的内容.该部分Html为: <div class="row" id="featureHeading"> <div class="col-md-12"> <h2>更多信息</h2> <p class="lead"> 广州恒大淘宝队的官方网站和微博同时发布了几张海报,预热本周三晚8点即将到来的亚冠1/4决赛第二回

1092 回文字符串(51nod)

原题链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1092 这题其实可以把字符串str反转一下然后再求两个字符串的最长公共子序列的长度,然后len(str)-那个长度就是答案了= = #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std;

hihoCoder_#1092 Have Lunch Together(最短路)

#1092 : Have Lunch Together 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Everyday Littile Hi and Little Ho meet in the school cafeteria to have lunch together. The cafeteria is often so crowded that two adjacent seats are hard to find. School cafeteria can

51Nod - 1092 回文字符串

51Nod - 1092 回文字符串 回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符串都可以通过向中间添加一些字符,使之变为回文字符串. 例如:abbc 添加2个字符可以变为 acbbca,也可以添加3个变为 abbcbba.方案1只需要添加2个字符,是所有方案中添加字符数量最少的. Input 输入一个字符串Str,Str的长度 <= 1000. Output 输出最少添加多少个字符可以使之变为回文字串. Input示例 abbc Output示例 2 题解

CSU 1092 Barricade

1092: Barricade Time Limit: 1 Sec  Memory Limit: 32 MBSubmit: 240  Solved: 71[Submit][Status][Web Board] Description GBQC国一共有N个城市,标号分别为1, 2, …, N.N个城市间一共有M条单向通行的道路. 不幸的是,GBQC国的城市1连续暴雨,使得整个城市淹没在汪洋洪水中,于是GBQC国领导人小明决定让城市1的居民暂时移居到城市N,于是一场浩浩荡荡的搬迁运动开始了. 但还有

九度OJ 1092 Fibonacci

题目1092:Fibonacci 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1576 解决:1158 题目描述: The Fibonacci Numbers{0,1,1,2,3,5,8,13,21,34,55...} are defined by the recurrence: F0=0 F1=1 Fn=Fn-1+Fn-2,n>=2 Write a program to calculate the Fibonacci Numbers. 输入: Each case contains

【bzoj3834】[Poi2014]Solar Panels 数论

题目描述 Having decided to invest in renewable energy, Byteasar started a solar panels factory. It appears that he has hit the gold as within a few days  clients walked through his door. Each client has ordered a single rectangular panel with specified w