poj3740



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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值