#include <bits/stdc++.h> using namespace std; int board[501][501]; list<int> stream; void print_stream() { for (const int x: stream) { if(x==0) cout << "G"; if(x==1) cout << "P"; if(x==2) cout << "D"; if(x==3) cout << "L"; } cout << '\n'; } int typ(int x) { if(x==0 || x==2) return 1; return 2; } bool rule1(list<int>::iterator it) { list<int>::iterator tempit=it; ++it; bool applied=false; if(it!=stream.end()) { if(*tempit == *it) { stream.erase(it); applied=true; } } it=tempit; return applied; } bool rule2(list<int>::iterator &it) { list<int>::iterator tempit=it; ++it; if(it!=stream.end()) { if( typ(*tempit)==typ(*it) ) { stream.erase(tempit); return true; } } --it; return false; } bool rule3(list<int>::iterator it) { list<int>::iterator temp1=it; ++it; if(it!=stream.end()) { list<int>::iterator temp2=it; ++it; if(it!=stream.end()) { if((*temp1)==(*it) && (typ(*temp1)!=typ(*temp2))) { stream.erase(it); it=temp1; return true; } } } return false; } void print_board(int n, int m) { for(int i=0; i< n; i++) { for(int j=0; j<m; j++) { if(board[i][j]==0) printf("."); if(board[i][j]==1) printf("B"); if(board[i][j]==2) printf("C"); } printf("\n"); } } void boardG(int n, int m) { for(int j=0; j<m; j++) { int free=0; while(free < n && board[free][j]!=0) free++; for(int i=0; i<n; i++) { if(board[i][j]!=0 && free < i) { board[free][j]=board[i][j]; board[i][j]=0; ++free; while(free < n && board[free][j]!=0) free++; } } } } void boardD(int n, int m) { for(int j=0; j<m; j++) { int free=n-1; while(free >=0 && board[free][j]!=0) free--; for(int i=n-1; i >= 0; i--) { if(board[i][j]!=0 && free > i) { board[free][j]=board[i][j]; board[i][j]=0; --free; while(free > -1 && board[free][j]!=0) free--; } } } } void boardL(int n, int m) { for(int i=0; i<n; i++) { int free=0; while(free < m && board[i][free]!=0) free++; for(int j=0; j<m; j++) { if(board[i][j]!=0 && free < j) { board[i][free]=board[i][j]; board[i][j]=0; ++free; while(free < m && board[i][free]!=0) free++; } } } } void boardP(int n, int m) { for(int i=0; i<n; i++) { int free=m-1; while(free >=0 && board[i][free]!=0) free--; for(int j=m-1; j >= 0; j--) { if(board[i][j]!=0 && free > j) { board[i][free]=board[i][j]; board[i][j]=0; --free; while(free > -1 && board[i][free]!=0) free--; } } } } int main() { int n,m; char a; scanf("%d", &n); scanf("%d", &m); for(int i=0; i<n; ++i){ scanf("%c", &a); // czytaj karetke for (int j = 0; j < m; j++) { scanf("%c", &a); if(a=='.') board[i][j]=0; if(a=='B') board[i][j]=1; if(a=='C') board[i][j]=2; } } int l; scanf("%d",&l); scanf("%c",&a); for(int i=0; i < l; ++i) { scanf("%c",&a); if(a=='G') stream.push_back(0); if(a=='P') stream.push_back(1); if(a=='D') stream.push_back(2); if(a=='L') stream.push_back(3); } // print_stream(); list<int>::iterator it=stream.begin(); while(it!=stream.end()) { while(rule1(it)); ++it; } // print_stream(); it=stream.begin(); while(it!=stream.end()) { while(rule2(it)); ++it; } // print_stream(); it=stream.begin(); while(it!=stream.end()) { while(rule3(it)) { list<int>::iterator tempit=it; ++it; if(!rule1(it)) rule2(it); it=tempit; if(it!=stream.begin()) --it; } ++it; } // printf("last stream:\n"); // print_stream(); // printf("\n"); it=stream.begin(); while(it!=stream.end()) { if((*it)==0) boardG(n,m); if((*it)==1) boardP(n,m); if((*it)==2) boardD(n,m); if((*it)==3) boardL(n,m); ++it; } print_board(n,m); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | #include <bits/stdc++.h> using namespace std; int board[501][501]; list<int> stream; void print_stream() { for (const int x: stream) { if(x==0) cout << "G"; if(x==1) cout << "P"; if(x==2) cout << "D"; if(x==3) cout << "L"; } cout << '\n'; } int typ(int x) { if(x==0 || x==2) return 1; return 2; } bool rule1(list<int>::iterator it) { list<int>::iterator tempit=it; ++it; bool applied=false; if(it!=stream.end()) { if(*tempit == *it) { stream.erase(it); applied=true; } } it=tempit; return applied; } bool rule2(list<int>::iterator &it) { list<int>::iterator tempit=it; ++it; if(it!=stream.end()) { if( typ(*tempit)==typ(*it) ) { stream.erase(tempit); return true; } } --it; return false; } bool rule3(list<int>::iterator it) { list<int>::iterator temp1=it; ++it; if(it!=stream.end()) { list<int>::iterator temp2=it; ++it; if(it!=stream.end()) { if((*temp1)==(*it) && (typ(*temp1)!=typ(*temp2))) { stream.erase(it); it=temp1; return true; } } } return false; } void print_board(int n, int m) { for(int i=0; i< n; i++) { for(int j=0; j<m; j++) { if(board[i][j]==0) printf("."); if(board[i][j]==1) printf("B"); if(board[i][j]==2) printf("C"); } printf("\n"); } } void boardG(int n, int m) { for(int j=0; j<m; j++) { int free=0; while(free < n && board[free][j]!=0) free++; for(int i=0; i<n; i++) { if(board[i][j]!=0 && free < i) { board[free][j]=board[i][j]; board[i][j]=0; ++free; while(free < n && board[free][j]!=0) free++; } } } } void boardD(int n, int m) { for(int j=0; j<m; j++) { int free=n-1; while(free >=0 && board[free][j]!=0) free--; for(int i=n-1; i >= 0; i--) { if(board[i][j]!=0 && free > i) { board[free][j]=board[i][j]; board[i][j]=0; --free; while(free > -1 && board[free][j]!=0) free--; } } } } void boardL(int n, int m) { for(int i=0; i<n; i++) { int free=0; while(free < m && board[i][free]!=0) free++; for(int j=0; j<m; j++) { if(board[i][j]!=0 && free < j) { board[i][free]=board[i][j]; board[i][j]=0; ++free; while(free < m && board[i][free]!=0) free++; } } } } void boardP(int n, int m) { for(int i=0; i<n; i++) { int free=m-1; while(free >=0 && board[i][free]!=0) free--; for(int j=m-1; j >= 0; j--) { if(board[i][j]!=0 && free > j) { board[i][free]=board[i][j]; board[i][j]=0; --free; while(free > -1 && board[i][free]!=0) free--; } } } } int main() { int n,m; char a; scanf("%d", &n); scanf("%d", &m); for(int i=0; i<n; ++i){ scanf("%c", &a); // czytaj karetke for (int j = 0; j < m; j++) { scanf("%c", &a); if(a=='.') board[i][j]=0; if(a=='B') board[i][j]=1; if(a=='C') board[i][j]=2; } } int l; scanf("%d",&l); scanf("%c",&a); for(int i=0; i < l; ++i) { scanf("%c",&a); if(a=='G') stream.push_back(0); if(a=='P') stream.push_back(1); if(a=='D') stream.push_back(2); if(a=='L') stream.push_back(3); } // print_stream(); list<int>::iterator it=stream.begin(); while(it!=stream.end()) { while(rule1(it)); ++it; } // print_stream(); it=stream.begin(); while(it!=stream.end()) { while(rule2(it)); ++it; } // print_stream(); it=stream.begin(); while(it!=stream.end()) { while(rule3(it)) { list<int>::iterator tempit=it; ++it; if(!rule1(it)) rule2(it); it=tempit; if(it!=stream.begin()) --it; } ++it; } // printf("last stream:\n"); // print_stream(); // printf("\n"); it=stream.begin(); while(it!=stream.end()) { if((*it)==0) boardG(n,m); if((*it)==1) boardP(n,m); if((*it)==2) boardD(n,m); if((*it)==3) boardL(n,m); ++it; } print_board(n,m); return 0; } |