Light OJ 1092 Lighted Panels (状压DP)

编程入门 行业动态 更新时间:2024-10-26 12:26:54

Light <a href=https://www.elefans.com/category/jswz/34/1768308.html style=OJ 1092 Lighted Panels (状压DP)"/>

Light OJ 1092 Lighted Panels (状压DP)


可参考POJ3279 本题方向更多,增加对角线。 亮点是使用二进制进行状态枚举(第一行和第一列)。
for(int s=0;s<(1<<m);s++)
for(int j=1;j<=c;j++){f[1][j]=(s>>(j-1))&1;}

使用
(s>>(j-1))&1;
判断每个点为0或1.

1092 - Lighted Panels
   PDF (English)StatisticsForum
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

 


PROBLEM SETTER: JANE ALAM JAN


package D20160711;import java.util.Arrays;
import java.util.Scanner;public class B {static int[][] a = new int[10][10];static int[][] f = new int[10][10];static int sum=Integer.MAX_VALUE/2;public static void main(String[] args) {// TODO Auto-generated method stubScanner in = new Scanner(System.in);int t=in.nextInt();int co = 0;while(t-->0){sum=Integer.MAX_VALUE/2;int r  =in.nextInt();int c  =in.nextInt();for(int i = 0;i<=r+1;i++){Arrays.fill(a[i],0);Arrays.fill(f[i],0);}for(int i =1;i<=r;i++){String s =in.next();for(int j=0;j<c;j++){if(s.charAt(j)=='.')a[i][j+1]=1;}}///long inte = 1<<(c+r-1);for(int s=0;s<inte;s++){int time = 0;for(int i = 0;i<=r+1;i++){Arrays.fill(f[i],0);}for(int j=1;j<=c;j++){f[1][j]=(s>>(j-1))&1;
//					System.out.print(f[1][j]+" ");if(f[1][j]==1)time++;}for(int i=2;i<=r;i++){f[i][1]=(s>>(c+i-2))&1;
//					System.out.print(f[i][1]+" ");if(f[i][1]==1)time++;}
//				System.out.println("");for(int k=2;k<=r;k++){for(int j=2;j<=c;j++){f[k][j]=f[k-1][j-1]^a[k-1][j-1];if(j-2>0)f[k][j]=f[k][j]^f[k-1][j-2]^f[k][j-2];	//left+left-down				if(k-2>0)f[k][j]=f[k][j]^f[k-2][j-1]^f[k-2][j];  //up+ up-rightif(k-2>0 && j-2>0)f[k][j]=f[k][j]^f[k-2][j-2];  //up-leftf[k][j]=f[k][j]^f[k-1][j]^f[k][j-1];				if(f[k][j]==1)time++;}}
//				System.out.println(time);
//				if(time==3){
//					for(int j=2;j<=r;j++){
//						for(int k=2;k<=c;k++){
//							System.out.print(f[j][k]);
//						}
//						System.out.println();
//					}
//				}boolean ok  = true;for(int j=1;j<=c;j++){int x = f[r][j]^a[r][j];if(r>1){x=x^f[r-1][j];//upif(j>1)x=x^f[r-1][j-1];if(j<c)x=x^f[r-1][j+1];}if(j>1)x=x^f[r][j-1];if(j<c)x=x^f[r][j+1];if(x==1){ok=false;break;}}if(ok){for(int i=1;i<=r;i++){int x = f[i][c]^a[i][c];if(i>1)x=x^f[i-1][c];if(c>1){x=x^f[i][c-1];if(i>1)x=x^f[i-1][c-1];if(i<r)x=x^f[i+1][c-1];}if(i<r)x=x^f[i+1][c];if(x==1){ok=false;break;}}}if(ok)sum = Math.min(sum, time);}////sysoco++;System.out.print("Case "+co+": ");if(sum!=Integer.MAX_VALUE/2)System.out.println(sum);elseSystem.out.println("impossible");}}}/*
</pre><pre name="code" class="java">可以使用的测试数据
3
3 5
**.**
...**
..*..
8 8
****..*.
****..*.
..*.....
*...**..
*...*.**
...**.**
*****...
********
6 8
******..
******..
***...**
......**
......**
........Output
Case 1: 3
Case 2: 13
Case 3: 7*/


更多推荐

Light OJ 1092 Lighted Panels (状压DP)

本文发布于:2024-02-12 15:53:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1688428.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:OJ   Light   Lighted   DP   状压

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!