fork download
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n, m, ok=0;
  6. int a[100];
  7.  
  8. void ktao()
  9. {
  10. for(int i=1;i<=m;++i)
  11. {
  12. a[i]=0;
  13. }
  14. }
  15.  
  16. void sinh()
  17. {
  18. int i=m;
  19. while(i>=1&&a[i]==1)
  20. {
  21. a[i]=0;
  22. --i;
  23. }
  24. if(i==0) ok=1;
  25. else a[i]=1;
  26. }
  27.  
  28. int count(int rented[], int n)
  29. {
  30. int sum=0;
  31. for(int i=1;i<=n;++i) if (rented[i]) sum++;
  32. return sum;
  33. }
  34.  
  35. int main()
  36. {
  37. cin>>m>>n;
  38. int k[m+1][n+1];
  39. for(int i=1;i<=m;++i)
  40. {
  41. for(int j=1;j<=n;++j) cin>>k[i][j];
  42. }
  43. int ans[100][100];
  44. int max=0, dem=0;
  45. ktao();
  46. int rented[n+1];
  47. while(!ok)
  48. {
  49. sinh();
  50. for(int i=1;i<=n;++i) rented[i]=0;
  51. bool valid=true;
  52. for(int i=1;i<=m;++i)
  53. {
  54. if(a[i])
  55. {
  56. for(int j=1;j<=n;++j)
  57. {
  58. if (k[i][j]&&rented[j]==0) rented[j]=1;
  59. else if(k[i][j]&&rented[j]==1)
  60. {
  61. valid=false;
  62. break;
  63. }
  64. }
  65. }
  66. if(!valid) break;
  67. }
  68. if (valid)
  69. {
  70. int v=count(rented,n);
  71. if(v>max)
  72. {
  73. dem=1;
  74. max=v;
  75. for(int i=1;i<=m;++i)
  76. {
  77. ans[dem][i]=a[i];
  78. }
  79. }
  80. else if (v==max)
  81. {
  82. dem++;
  83. for(int i=1;i<=m;++i)
  84. {
  85. ans[dem][i]=a[i];
  86. }
  87. }
  88. }
  89. }
  90. for(int i=1;i<=dem;++i)
  91. {
  92. for(int j=1;j<=m;++j) cout<<ans[i][j]<<" ";
  93. cout<<endl;
  94. }
  95. return 0;
  96. }
Success #stdin #stdout 0s 5284KB
stdin
10
20
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
0 1 0 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0
1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0
0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0
0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
stdout
0 0 0 0 1 1 1 1 1 1 
0 1 1 1 0 1 0 1 0 0