YEZAELP

PROG-1011: Block Game

Jun 8th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using pii=pair<char,pair<int,int>>;
  4. int n,m,score;
  5. char ar[10][10];
  6. int dx[]={1,-1,0,0};
  7. int dy[]={0,0,1,-1};
  8. void print_block(void){
  9.     for(int i=1;i<=n;i++){
  10.         for(int j=1;j<=m;j++){
  11.             printf("%c ",ar[i][j]);
  12.         }
  13.         printf("\n");
  14.     }
  15. }
  16.  
  17. bool position(int i,int j){
  18.     if(i<1||i>n||j<1||j>m) return false;
  19.     return true;
  20. }
  21.  
  22. bool move_block(){
  23.     queue <pii> q;
  24.     for(int i=1;i<=n;i++){
  25.         for(int j=1;j<=m;j++){
  26.             if(65<=ar[i][j] and ar[i][j]<=90){
  27.                 q.push({ ar[i][j] , {i,j} });
  28.             }
  29.         }
  30.     }
  31.  
  32.     bool move_or_not=false;
  33.     while(q.size()>0){
  34.         int ui,uj;
  35.         char a;
  36.         a=q.front().first;
  37.         ui=q.front().second.first;
  38.         uj=q.front().second.second;
  39.         q.pop();
  40.         ar[ui][uj]=a;
  41.  
  42.         if( (position(ui+1,uj) or ui+1==n) and ar[ui+1][uj]=='-'){
  43.             move_or_not=true;
  44.             ar[ui][uj]='-';
  45.             q.push({a,{ui+1,uj}});
  46.         }
  47.     }
  48.  
  49.     if(move_or_not)
  50.         return true;
  51.     else
  52.         return false;
  53. }
  54. bool check_delete(int ui,int uj){
  55.     bool rt=false;
  56.     for(int d=0;d<4;d++){
  57.         int vi,vj;
  58.         vi=dx[d]+ui;
  59.         vj=dy[d]+uj;
  60.         if(position(vi,vj) && ar[vi][vj]==ar[ui][uj] )
  61.             rt=true;
  62.     }
  63.     if(rt)
  64.         return true;
  65.     else
  66.         return false;
  67. }
  68. bool delete_block(){
  69.     queue <pii> q;
  70.     for(int i=1;i<=n;i++){
  71.         for(int j=1;j<=m;j++){
  72.             if( (65<=ar[i][j] and ar[i][j]<=90) and check_delete(i,j) ){
  73.                 q.push({ar[i][j],{i,j} });
  74.             }
  75.         }
  76.     }
  77.  
  78.     bool can_delete=false;
  79.     while(q.size()>0){
  80.         int ui,uj;
  81.         char a;
  82.         a=q.front().first;
  83.         uj=q.front().second.second;
  84.         ui=q.front().second.first;
  85.         q.pop();
  86.  
  87.         if(ar[ui][uj]!=a)
  88.             continue;
  89.         can_delete=true;
  90.         ar[ui][uj]='-';
  91.         score+=5;
  92.  
  93.         for(int d=0;d<4;d++){
  94.             int vi,vj;
  95.             vi=ui+dx[d];
  96.             vj=uj+dy[d];
  97.             if(position(vi,vj) and ar[vi][vj]==a){
  98.                 q.push({a,{vi,vj}});
  99.             }
  100.         }
  101.  
  102.     }
  103.     if(can_delete)
  104.         return true;
  105.     else
  106.         return false;
  107. }
  108. int main(){
  109.     scanf("%d%d",&n,&m);
  110.  
  111.     for(int i=1;i<=n;i++){
  112.         for(int j=1;j<=m;j++){
  113.             scanf(" %c",&ar[i][j]);
  114.         }
  115.     }
  116.  
  117.     int t;
  118.     scanf("%d",&t);
  119.     while(t--){
  120.  
  121.         int ui,uj,drt;
  122.         char opr;
  123.         scanf("%d%d",&ui,&uj);
  124.         ui++;
  125.         uj++;
  126.         scanf(" %c",&opr);
  127.         if(opr=='L') drt=-1;
  128.         else drt=1;
  129.  
  130.         if(!position(ui,uj+drt) || ar[ui][uj+drt]!='-' || !(65<=ar[ui][uj] and ar[ui][uj]<=90)) {
  131.             score-=5;
  132.             continue;
  133.         }
  134.  
  135.         bool stop=false;
  136.         int c=1;
  137.         while(!stop){
  138.             if(c==1){
  139.                 ar[ui][uj+drt]=ar[ui][uj];
  140.                 ar[ui][uj]='-';
  141.                 uj=uj+drt;
  142.                 move_block();
  143.                 delete_block();
  144.             }
  145.  
  146.             if(!move_block()) break;
  147.             if(!delete_block()) break;
  148.  
  149.             c++;
  150.         }
  151.     }
  152.  
  153.     printf("%d\n",score);
  154.     print_block();
  155.  
  156.     return 0;
  157. }
  158. /*
  159.  
  160. 4 4
  161. #B-B
  162. BA-#
  163. AB-#
  164. #A-B
  165. 5
  166. 0 1 R
  167.  
  168.  
  169. 4 4
  170. #B-B
  171. BA-B
  172. AB-#
  173. #A-B
  174. 3
  175. 0 1 R
  176. 0 3 L
  177. 1 3 L
  178. */
Add Comment
Please, Sign In to add comment