fork download
  1. #include<iostream>
  2. #include <map>
  3. #include<string>
  4. using namespace std;
  5. int a,b;
  6. char arr[21][9];
  7. int findblackrow(){
  8. bool benar = true;
  9. int lastrow;
  10. for(int i=0;i<a;i++){
  11. benar = true;
  12. for(int j=0;j<b;j++){
  13. if(arr[i][j] == '0'){
  14. benar = false;
  15. }
  16. }
  17. if(benar){
  18. lastrow = i;
  19. for(int i=0;i<b;i++){
  20. arr[lastrow][i] = '0';
  21. }
  22. }
  23. }
  24. return lastrow;
  25. }
  26.  
  27. int main(){
  28. int count=0;
  29. cin>>a>>b;
  30. map <int,int> list;
  31.  
  32. for(int i=0;i<a;i++){
  33. for(int j=0;j<b;j++){
  34. cin>>arr[i][j];
  35. }
  36. }
  37. int blackrow = findblackrow();
  38. int firstblack[b];
  39. for(int i=0;i<b;i++){
  40. for(int j=a-1;j>=blackrow;j--){
  41. if(arr[j][i] =='1'){
  42. firstblack[i] = j;
  43. }
  44. }
  45. }
  46.  
  47. for(int i=0;i<b;i++){
  48. for(int j=firstblack[i];j>=0;j--){
  49. if(arr[j][i]=='1'){
  50. count++;
  51. }
  52. }
  53. list[i] = count;
  54. count =0;
  55. }
  56.  
  57. int temp;
  58. for(int i=0;i<b;i++){
  59. temp = list[i];
  60. for(int j=firstblack[i];j>=0;j--){
  61.  
  62. if(temp>0){
  63. arr[j][i] = '1';
  64. temp--;
  65. }
  66. else{
  67. arr[j][i]='0';
  68. }
  69. }
  70. }
  71. for(int i=0;i<a;i++){
  72. for(int j=0;j<b;j++){
  73. cout<<arr[i][j];
  74. }
  75. cout<<endl;
  76. }
  77.  
  78. }
Success #stdin #stdout 0.01s 5304KB
stdin
11 6
000000
000000
011100
110011
111011
111000
110111
111011
111001
001100
111011
stdout
000000
000000
011100
110011
111011
111000
110111
111011
111001
001100
111011