http://poj.org/problem?id=1129

  1. import java.util.*;
  2. import java.math.*;
  3. public class Main {
  4. public static boolean flag=false;
  5. public static int ans=0;
  6. public static void main(String []args)
  7. {
  8. Scanner cin=new Scanner(System.in);
  9. int n;
  10. String str;
  11. while(cin.hasNext())
  12. {
  13. ans=1;
  14. int [][]g=new int[100][100];
  15. int []hash=new int[100];
  16. n=cin.nextInt();
  17. if(n==0) break;
  18. for(int i=0; i<n; i++)
  19. {
  20. str=cin.next();
  21. for(int j=2; j<(int)str.length(); j++)
  22. {
  23. g[str.charAt(0)-'A'][str.charAt(j)-'A']=1;
  24. }
  25. }
  26. flag=false;
  27. dfs(0,1,g,n,hash);
  28. if(ans==1)
  29. {
  30. System.out.println("1 channel needed.");
  31. }
  32. else
  33. {
  34. System.out.println(ans+" channels needed.");
  35. }
  36. }
  37. }
  38. public static int deal(int id,int co,int n,int g[][],int hash[])
  39. {
  40. for(int i=0; i<n; i++)
  41. {
  42. if(g[id][i]==1&&co==hash[i])
  43. {
  44. return 0;
  45. }
  46. }
  47. return 1;
  48. }
  49. public static void dfs(int num,int m1,int g[][],int n,int hash[])
  50. {
  51. if(flag) return;
  52. if(num>=n)
  53. {
  54. flag=true;
  55. return;
  56. }
  57. for(int i=1; i<=m1; i++)
  58. {
  59. if(deal(num,i,n,g,hash)==1)
  60. {
  61. hash[num]=i;
  62. dfs(num+1,m1,g,n,hash);
  63. hash[num]=0;
  64. }
  65. }
  66. if(flag==false)
  67. {
  68. ans++;
  69. dfs(num,m1+1,g,n,hash);
  70. }
  71. }
  72. }

poj 1129 Channel Allocation的更多相关文章

  1. 迭代加深搜索 POJ 1129 Channel Allocation

    POJ 1129 Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14191   Acc ...

  2. POJ 1129 Channel Allocation(DFS)

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13173   Accepted: 67 ...

  3. POJ 1129 Channel Allocation DFS 回溯

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15546   Accepted: 78 ...

  4. poj 1129 Channel Allocation ( dfs )

    题目:http://poj.org/problem?id=1129 题意:求最小m,使平面图能染成m色,相邻两块不同色由四色定理可知顶点最多需要4种颜色即可.我们于是从1开始试到3即可. #inclu ...

  5. POJ 1129 Channel Allocation 四色定理dfs

    题目: http://poj.org/problem?id=1129 开始没读懂题,看discuss的做法,都是循环枚举的,很麻烦.然后我就决定dfs,调试了半天终于0ms A了. #include ...

  6. poj 1129 Channel Allocation(图着色,DFS)

    题意: N个中继站,相邻的中继站频道不得相同,问最少需要几个频道. 输入输出: Sample Input 2 A: B: 4 A:BC B:ACD C:ABD D:BC 4 A:BCD B:ACD C ...

  7. PKU 1129 Channel Allocation(染色问题||搜索+剪枝)

    题目大意建模: 一个有N个节点的无向图,要求对每个节点进行染色,使得相邻两个节点颜色都不同,问最少需要多少种颜色? 那么题目就变成了一个经典的图的染色问题 例如:N=7 A:BCDEFG B:ACDE ...

  8. Channel Allocation (poj 1129 dfs)

    Language: Default Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12 ...

  9. POJ 1129:Channel Allocation 四色定理+暴力搜索

    Channel Allocation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13357   Accepted: 68 ...

随机推荐

  1. php生成json和js解析json

    php生成json $value=array("name"=>"dfas");echo json_encode($value); js解析json var ...

  2. eclipse安装github插件egit

    http://jingyan.baidu.com/article/4853e1e529483c1909f726c3.html help->InstallNew Software,这是安装插件推荐 ...

  3. 用VS2013+VELT进行Linux开发

    快乐虾 http://blog.csdn.net/lights_joy/(QQ群:Visual EmbedLinux Tools 375515651) 欢迎转载,但请保留作者信息 1.1.1  什么是 ...

  4. UESTC_方老师和农场 2015 UESTC Training for Graph Theory<Problem L>

    L - 方老师和农场 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  5. Zigzag Iterator 解答

    Question Given two 1d vectors, implement an iterator to return their elements alternately. For examp ...

  6. Hdu2437-Jerboas(取余数判重搜索)

    Jerboas are small desert-living animals, which resemble mice with a long tufted tail and very long h ...

  7. Ajax_post发送

    $('#img_file_del_3').click(function() { var data={name:$('#img_file_del_3').attr('name')}; var url=' ...

  8. java框架BeanUtils及路径问题练习

    内省----->一个变态的反射    BeanUtils主要解决 的问题: 把对象的属性数据封装 到对象中.  使从文件中读取的数据往对象中赋值更加简单:   BeanUtils的好处:  1. ...

  9. 用window.print()打印指定div里面的内容

    用window.print()打印指定div里面的内容 今天客户让添加个打印证照功能,直接用window.print()打印的是整个页面,而用以下方法就可以只打印证明了 <!--window.p ...

  10. Git 上传本地命令

    1.首先建立一个文件夹用以测试 2.在test中写入一个main.c的文件 其内容如下: 3.然后就建立一个git仓库了 4.然后就是把内容加进去了,上传上去 5.然后我们看下git log的信息 6 ...