poj1129



package work;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class A1129singal {
 static int N,ans;
 static char[][] data;
 static boolean[][] map;
 static int mincol;
 static int area[];
 static boolean used;

 public static void main(String[] args) throws FileNotFoundException {
  /* Scanner sc=new Scanner(System.in); */
  Scanner sc = new Scanner(new File("src/11290"));
  while (true) {
   N = sc.nextInt();
   data = new char[N][N + 1];
   map = new boolean[N][N];
   area = new int[N];
   mincol = 0xfffffff;
   used=false;
   ans=1;
   int num = 0;
   
   if (N == 0) {
    break;
   }
   sc.nextLine();
   for (int i = 0; i < N; i++) {
    char[] line = sc.nextLine().toCharArray();
    for (int j = 2; j < line.length; j++) {
     data[i][j] = line[j];
     if (data[i][j] == 'A') {
      num = 0;
     }
     if (data[i][j] == 'B') {
      num = 1;
     }
     if (data[i][j] == 'C') {
      num = 2;
     }
     if (data[i][j] == 'D') {
      num = 3;
     }
     map[i][num] = true;
     //
    }
   }
   dfs(0, 1);
   if (ans == 1) {
    System.out.println(ans + " channel needed.");
   } else {
    System.out.println(ans + " channels needed.");
   }
  }
 }

 // 第几区域,总数
 private static void dfs(int a, int tol) {
  if(used){return;}
  if ( a>= N) { used = true; return; }
  /*if (a == N) {
   if (mincol > tol) {
    mincol = tol;
   }
   return;
  }*/
  for (int i = 1; i <= tol; i++) {
   if (isok(a, i)) {
    area[a] = i;
    dfs(a + 1, tol);
    area[a] = 0;
   }
  }
     if(!used){
  ans++;
  dfs(a, tol + 1);}
 }

 private static boolean isok(int a, int colnum) {
  for (int j = 0; j < N; j++) {
   if (map[a][j] && area[j] == colnum) {
    return false;
   }
  }
  return true;
 }
}


//

2
0 0
0 0
4
0 1 1 0
1 0 1 1
1 1 0 1
0 1 1 0
4
0 1 1 1
1 0 1 1
1 1 0 1
1 1 1 0
0


//

1 channel needed.
3 channels needed.
4 channels needed.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值