YEZAELP

PROG-1074: Mravojed

Jun 19th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using pii=pair<int,int>;
  4. int n,m;
  5. char ar[102][102];
  6. int color[102][102];
  7. int line[102],col[102];
  8. int dx[]={1,-1,0,0};
  9. int dy[]={0,0,1,-1};
  10. int position(int i,int j){
  11.     return i>=1 and i<=n and j>=1 and j<=m;
  12. }
  13. bool all(int ui,int i,int uj,int j){
  14.     for(int k=uj;k<=j;k++) {
  15.         if(ar[i][k]!='x')
  16.             return false;
  17.     }
  18.     for(int k=ui;k<=i;k++){
  19.         if(ar[k][j]!='x')
  20.             return false;
  21.     }
  22.     return true;
  23. }
  24. int first_square(int ui,int uj){
  25.     int i=ui,j=uj,cnt=0;
  26.     while(ar[ui][j]=='x' and ar[i][uj]=='x' and all(ui,i,uj,j)){
  27.         i++;
  28.         j++;
  29.         cnt++;
  30.         if(!position(i,j) ) break;
  31.     }
  32.     return cnt;
  33. }
  34. pii second_square(int ui,int uj){
  35.     queue <pair<int,int>> q;
  36.     for(int i=n;i>=1;i--){
  37.         for(int j=m;j>=1;j--){
  38.             if(ar[i][j]=='x'){
  39.                 q.push({i,j});
  40.                 break;
  41.             }
  42.         }
  43.     }
  44.     int ui2,uj2;
  45.     ui2=0;
  46.     uj2=0;
  47.     vector < vector<bool> > visited(n+1,vector<bool> (m+1,false));
  48.     while(q.size()>0){
  49.         int ui,uj;
  50.         ui=q.front().first;
  51.         uj=q.front().second;
  52.         q.pop();
  53.         if(visited[ui][uj])
  54.             continue;
  55.         ui2=max(ui2,ui);
  56.         uj2=max(uj2,uj);
  57.         visited[ui][uj]=true;
  58.         for(int i=0;i<4;i++){
  59.             int vi,vj;
  60.             vi=ui+dx[i];
  61.             vj=uj+dy[i];
  62.             if(position(vi,vj) and !visited[vi][vj] and ar[vi][vj]=='x'){
  63.                 q.push({vi,vj});
  64.             }
  65.         }
  66.     }
  67.     return {ui2,uj2};
  68. }
  69. int sz(int ui,int uj){
  70.     int cnt=0;
  71.     /*while(line[ui]>0||col[uj]>0){
  72.         cnt=max(cnt,line[ui]);
  73.         cnt=max(cnt,col[uj]);
  74.         ui++;
  75.         uj++;
  76.         if(!position(ui,uj)) break;
  77.     }*/
  78.     while(line[ui]>0){
  79.         cnt=max(cnt,line[ui]);
  80.         ui++;
  81.         if(ui>n) break;
  82.     }
  83.     while(col[uj]>0){
  84.         cnt=max(cnt,col[uj]);
  85.         uj++;
  86.         if(uj>m) break;
  87.     }
  88.     return cnt;
  89. }
  90. int main(){
  91.  
  92.     scanf("%d%d",&n,&m);
  93.     int ui1=0,uj1=0;
  94.     for(int i=1;i<=n;i++){
  95.         for(int j=1;j<=m;j++){
  96.             scanf(" %c",&ar[i][j]);
  97.             if(ar[i][j]=='x' and ui1==0 and uj1==0){
  98.                 ui1=i;
  99.                 uj1=j;
  100.             }
  101.             if(ar[i][j]=='x'){
  102.                 line[i]++;
  103.                 col[j]++;
  104.             }
  105.         }
  106.     }
  107.  
  108.     int square1=first_square(ui1,uj1);
  109.     printf("%d %d %d\n",ui1,uj1,square1);
  110.  
  111.     for(int i=ui1;i<=ui1+square1-1;i++){
  112.         for(int j=uj1;j<=uj1+square1-1;j++){
  113.             ar[i][j]='.';
  114.             line[i]--;
  115.             col[j]--;
  116.         }
  117.     }
  118.  
  119.     int ui2,uj2,square2;
  120.     pii uij=second_square(0,0);
  121.     ui2=uij.first;
  122.     uj2=uij.second;
  123.     square2=sz(ui2,uj2);
  124.     if(square2==0) printf("%d %d %d",ui1,uj1,square1);
  125.     else {
  126.         if(uj2==1) printf("%d %d %d",ui2-square2+1,uj2,square2);
  127.         else printf("%d %d %d",ui2-square2+1,uj2-square2+1,square2);
  128.     }
  129.  
  130.  
  131.     return 0;
  132. }
  133. /*
  134. ----------
  135.  
  136. case :  PPPPPPPP-P
  137. 5 5
  138. .xxxx
  139. xxxxx
  140. xxxxx
  141. xxxxx
  142. .....
  143.  
  144. ----------
  145.  
  146. 3 5
  147. xxxxx
  148. xxxxx
  149. xxxxx
  150.  
  151. ----------
  152.  
  153. 5 4
  154. xxxx
  155. xxxx
  156. xxxx
  157. xxxx
  158. xxx.
  159. correct
  160.  
  161. 5 5
  162. .....
  163. xxx..
  164. xxxx.
  165. xxxx.
  166. .xxx.
  167. correct
  168.  
  169. ----------
  170.  
  171. 3 3
  172. xxx
  173. xxx
  174. xx.
  175.  
  176. 5 5
  177. .....
  178. .xxx.
  179. .xxx.
  180. .xxx.
  181. .x...
  182.  
  183. 3 3
  184. .xx
  185. xxx
  186. xx.
  187.  
  188. 3 3
  189. xx.
  190. xx.
  191. x..
  192.  
  193. 3 3
  194. xx.
  195. xxx
  196. ...
  197.  
  198. 3 3
  199. ..x
  200. .xx
  201. .xx
  202. */
Add Comment
Please, Sign In to add comment