package work;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class A3740 {
static int[][] S;
static int M, N;
static boolean isfound;
static boolean[] row;
static int [] col;
public static void main(String[] args) throws FileNotFoundException {
/* Scanner sc=new Scanner(System.in); */
Scanner sc = new Scanner(new File("src/3740"));
while (true) {
M = sc.nextInt();
N = sc.nextInt();
S = new int[M][N];
if (M == 0 && N == 0) {
break;
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
S[i][j] = sc.nextInt();
}
}
isfound = false;
row = new boolean[M];
col = new int[N];
for (int m = 0; m < M; m++) {
dfs(0,m);
if(isfound){break;}
}
if (isfound) {
System.out.println("Yes, I found it");
} else {
System.out.println("It is impossible");
}
//
/*
* for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) {
* System.out.print(S[i][j]); } System.out.println(); }
*/
//
}
}
private static void dfs(int step,int m) {
if (step==M) {
return ;
}
row[step]=true;
if(check(step,m)){
if(isfound==true){return;}
dfs(step+1,m);
}
else{
row[step]=false;
dfs(step+1,m);
}
}
private static boolean check(int step,int m) {
int num =0;
boolean ret=false;
for (int i = m; i <=step; i++) {
if(row[i]){
for (int j = 0; j < N; j++) {
col[j]+=S[i][j];
if(col[j]==1){num++;}
else if(col[j]>1){ret =false;return ret;}
}
if(num==N){isfound=true;}
}
}
return ret;
}
}
//
package work;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class A3740 {
static int[][] S;
static int M, N;
static boolean isfound;
static boolean[] row;
static int [] col;
public static void main(String[] args) throws FileNotFoundException {
/* Scanner sc=new Scanner(System.in); */
Scanner sc = new Scanner(new File("src/3740"));
while (true) {
M = sc.nextInt();
N = sc.nextInt();
S = new int[M][N];
if (M == 0 && N == 0) {
break;
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
S[i][j] = sc.nextInt();
}
}
isfound = false;
row = new boolean[M];
col = new int[N];
for (int m = 0; m < M; m++) {
dfs(0,m);
if(isfound){break;}
}
if (isfound) {
System.out.println("Yes, I found it");
} else {
System.out.println("It is impossible");
}
//
/*
* for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) {
* System.out.print(S[i][j]); } System.out.println(); }
*/
//
}
}
private static void dfs(int step,int m) {
if (step==M) {
return ;
}
row[step]=true;
if(check(step,m)){
if(isfound==true){return;}
dfs(step+1,m);
}
else{
row[step]=false;
dfs(step+1,m);
}
}
private static boolean check(int step,int m) {
int num =0;
boolean ret=false;
for (int i = m; i <=step; i++) {
if(row[i]){
for (int j = 0; j < N; j++) {
col[j]+=S[i][j];
if(col[j]==1){num++;}
else if(col[j]>1){ret =false;return ret;}
}
if(num==N){isfound=true;}
}
}
return ret;
}
}
//input
3 3
0 1 0
0 0 1
1 0 0
4 4
0 0 0 1
1 0 0 0
1 1 0 1
0 1 0 0
0 0
//output
Yes, I found it
It is impossible
本文介绍了一个关于矩阵覆盖的问题,并通过深度优先搜索算法进行求解。对于给定的M×N矩阵,每行包含若干个1,目标是通过选择部分行进行叠加,使得每一列恰好出现一次1,实现这一目标则输出成功信息。
4514

被折叠的 条评论
为什么被折叠?



