fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. map<string ,int> map1;
  4. void allPossible(int*arr,int len,int size,int m,int n)
  5. { int arr1[m][n];
  6. if(len==size) //if all the elements in the array is filled either with 0 or 1 function prints the array in matrix form
  7. { int counter=0;
  8. for(int i=0;i<m;i++)
  9. {
  10. for(int j=0;j<n;j++)
  11. {
  12. arr1[i][j]=arr[counter++];
  13.  
  14. }
  15.  
  16. }
  17.  
  18. string p;
  19. ostringstream oss("");
  20. for (int temp = 0; temp < m; temp++)
  21. for (int temp1 = 0; temp1 < n; temp1++)
  22. oss << arr1[temp][temp1];
  23.  
  24. p=oss.str();
  25.  
  26. map1.insert(pair<string,int>(p,0));
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37. return;
  38.  
  39. }
  40.  
  41.  
  42. arr[len]=0;
  43. allPossible(arr,len+1,size,m,n);
  44. arr[len]=1;
  45. allPossible(arr,len+1,size,m,n);
  46.  
  47.  
  48.  
  49.  
  50. }
  51.  
  52. int main(){
  53. int M,N,m,n;
  54. cin>>M>>N>>m>>n;
  55.  
  56. int matrix[M][N];
  57. srand(time(NULL));
  58.  
  59.  
  60. for(int i=0;i<M;i++)
  61. for(int j=0;j<N;j++)
  62. matrix[i][j]=rand()%2;
  63.  
  64. int t=1;
  65. while(t)
  66. {
  67. int arr[m*n];
  68. allPossible(arr,0,m*n,m,n);
  69.  
  70.  
  71.  
  72. map <string, int> :: iterator itr;
  73. for (itr = map1.begin(); itr != map1.end(); ++itr)
  74. {
  75.  
  76.  
  77. string q;
  78. q=itr->first;
  79. int p=0;
  80. int pp[m][n];
  81. for(int i=0;i<m;i++)
  82. for(int j=0;j<n;j++)
  83. pp[i][j]=q[p++]-48;
  84.  
  85.  
  86.  
  87.  
  88.  
  89. int count=0;
  90. for(int i=0;i<=M-m;i++)
  91. {for(int j=0;j<=N-n;j++)
  92. {bool flag=1;
  93. for(int k=i;k<i+m;k++)
  94. for(int l=j;l<j+n;l++)
  95. if(pp[k-i][l-j]!=matrix[k][l])
  96. flag=0;
  97.  
  98. if(flag==1)
  99. count++;
  100.  
  101.  
  102.  
  103. }}
  104.  
  105.  
  106.  
  107.  
  108. itr->second=count;
  109.  
  110.  
  111.  
  112. }
  113. //CHECKING WHETHER THE GENERATED NUMBER IS UNIFORM OR NOT
  114. bool signify=1;
  115. for (itr = map1.begin(); itr != map1.end(); ++itr)
  116. { int c=m*n;
  117.  
  118. double t=(double)((M-m+1)*(N-n+1)/pow(2,c));
  119. double p=(double)((M-m+1)*(N-n+1)/pow(2,c));
  120. double tt=floor(t),pp=ceil(p);
  121.  
  122. if(itr->second>tt&&itr->second<pp)
  123. ;
  124. else
  125. signify=0;
  126.  
  127. }
  128. if(signify==1)
  129. {cout<<"UNIFORM WORD";
  130. break;
  131. }
  132. map <string, int> :: iterator itt;
  133. itr=map1.begin();
  134. int max=itr->second;
  135. itt=itr;
  136.  
  137. for (itr = map1.begin(); itr != map1.end(); ++itr)
  138. {
  139. if(itr->second>max)
  140. {max=itr->second;
  141. itt=itr;
  142. }
  143. }
  144.  
  145. string pp=itt->first;
  146. int p=0;
  147. int ppp[m][n];
  148. for(int i=0;i<m;i++)
  149. for(int j=0;j<n;j++)
  150. ppp[i][j]=pp[p++]-48;
  151.  
  152.  
  153.  
  154.  
  155. itr=map1.begin();
  156.  
  157.  
  158. for(int i=0;i<=M-m;i++)
  159. {for(int j=0;j<=N-n;j++)
  160. {bool flag=1;
  161. for(int k=i;k<i+m;k++)
  162. for(int l=j;l<j+n;l++)
  163. if(ppp[k-i][l-j]!=matrix[k][l])
  164. flag=0;
  165.  
  166. if(flag==1)
  167. { string r;
  168. r=itr->first;
  169. int p=0;
  170. int pp[m*n];
  171. for(int i=0;i<m*n;i++)
  172.  
  173. pp[i]=r[p++]-48;
  174. int c=0;
  175. for(int k=i;k<i+m;k++)
  176. for(int l=j;l<j+n;l++)
  177. matrix[k][l]=pp[c++];
  178. itr++;
  179.  
  180.  
  181. }
  182. }}
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196. map1.clear();
  197.  
  198.  
  199. }
  200. }
Time limit exceeded #stdin #stdout 5s 15248KB
stdin
8 9 2 3
stdout
Standard output is empty