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;
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 |
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 stub
Scanner 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-right
if(k-2>0 && j-2>0)f[k][j]=f[k][j]^f[k-2][j-2]; //up-left
f[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];//up
if(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);
}
//
//syso
co++;
System.out.print("Case "+co+": ");
if(sum!=Integer.MAX_VALUE/2)System.out.println(sum);else
System.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
*/