Advertisement
Korotkodul

CF C

Aug 18th, 2022 (edited)
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. #define vec vector
  12. using namespace std;
  13. using ll = long long;
  14. using ld = long double;
  15. using db = double;
  16. void cv(vector <int> &v){
  17. for (auto x: v) cout<<x<<' ';
  18. cout<<"\n";
  19. }
  20.  
  21. void cvl(vector <ll> &v){
  22. for (auto x: v) cout<<x<<' ';
  23. cout<<"\n";
  24. }
  25.  
  26.  
  27. void cvv(vector <vector <int> > &v){
  28. for (auto x: v) cv(x);
  29. cout<<"\n";
  30. }
  31.  
  32. void cvb(vector <bool> v){
  33. for (bool x: v) cout<<x<<' ';
  34. cout<<"\n";
  35. }
  36.  
  37. void cvs(vector <string> v){
  38. for (auto a: v){
  39. cout<<a<<"\n";
  40. }
  41. }
  42.  
  43. void cvp(vector <pii> a){
  44. for (auto p: a){
  45. cout<<p.first<<' '<<p.second<<"\n";
  46. }
  47. cout<<"\n";
  48. }
  49.  
  50. int t=1,n,m;
  51. vector <vector <int> > v;
  52.  
  53. bool sh=0;
  54.  
  55. void d1(int x, int y){
  56. v[x][y]=0;
  57. v[x+1][y]=0;
  58. v[x][y+1]=0;
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. int f1(int x, int y){
  68. int r=0;
  69. if(v[x][y]==1)r++;
  70. if(v[x+1][y]==1)r++;
  71. if(v[x][y+1]==1)r++;
  72. return r;
  73. }
  74.  
  75. void d2(int x, int y){
  76. v[x][y] = 0;
  77. v[x+1][y]=0;
  78. v[x][y-1]=0;
  79. }
  80.  
  81. int f2(int x, int y){
  82. int r=0;
  83. if(v[x][y]==1)r++;
  84. if(v[x+1][y]==1)r++;
  85. if(v[x][y-1]==1)r++;
  86. return r;
  87. }
  88.  
  89. void d3(int x, int y){
  90. v[x][y]=0;
  91. v[x-1][y]=0;
  92. v[x][y+1]=0;
  93. }
  94.  
  95. int f3(int x, int y){
  96. int r=0;
  97. if(v[x][y]==1)r++;
  98. if(v[x-1][y]==1)r++;
  99. if(v[x][y+1]==1)r++;
  100. return r;
  101. }
  102.  
  103.  
  104. void d4(int x, int y){
  105. v[x][y]=0;
  106. v[x][y-1]=0;
  107. v[x-1][y]=0;
  108. }
  109.  
  110.  
  111. int f4(int x, int y){
  112. int r=0;
  113. if(v[x][y]==1)r++;
  114. if( v[x][y-1]==1)r++;
  115. if(v[x-1][y]==1)r++;
  116. return r;
  117. }
  118.  
  119. const int inf = 2e9;
  120.  
  121. bool cmp(pii a, pii b){//{nmb of 1, id oper}
  122. return a.first < b.first || a.first == b.first && a.second > b.second;
  123. }
  124.  
  125.  
  126. vector <pii> can;
  127. int ans=0;
  128.  
  129. void op(int i, int j){
  130. can.clear();
  131. if (i + 1 < n && j + 1 < m){
  132. can.push_back({f1(i,j),1});
  133. }
  134. if (i + 1 < n && j - 1 > 0){
  135. can.push_back({f2(i,j),2});
  136. }
  137. if (i - 1 > 0 && j + 1 < m){
  138. can.push_back({f3(i,j),3});
  139. }
  140. if (i - 1 > 0 && j - 1 > 0){
  141. can.push_back({f4(i,j),4});
  142. }
  143. if (can.empty()){
  144. return;
  145. }
  146. sort(can.begin(), can.end(), cmp);
  147. if (can[can.size() - 1].first == 0){
  148. return;
  149. }
  150. ans++;
  151. pii p = can[0];
  152. if (p.second == 1){
  153. d1(i,j);
  154. }
  155. else if (p.second == 2){
  156. d2(i,j);
  157. }
  158. else if (p.second == 3){
  159. d3(i,j);
  160. }
  161. else{
  162. d4(i,j);
  163. }
  164. }
  165.  
  166. void slv(){
  167. ans=0;
  168.  
  169.  
  170. if (f4(1,1) > 0){
  171. ans++;
  172. d4(1,1);
  173. }
  174. if (f3(1,m-2) > 0){
  175. d3(1,m-1);
  176. ans++;
  177. }
  178. if (f2(n-2, 1) > 0){
  179. d2(n-2, 1);
  180. ans++;
  181. }
  182. if (f1(n-2, m-2) > 0){
  183. d1(n-2, m-2);
  184. ans++;
  185. }
  186.  
  187. for (int j = 2; j < m - 2;++j){
  188. op(0,j);
  189. }
  190. for (int j = 2; j < m - 2;++j){
  191. op(n-1,j);
  192. }
  193. for (int i=2;i<n-2;++i){
  194. op(i, 0);
  195. }
  196. for (int i = 2; i < n - 2; ++i){
  197. op(i, m-1);
  198. }
  199.  
  200. for (int i = 2; i < n-2; ++i){
  201. for (int j = 2; j < m-2; ++j){
  202. op(i,j);
  203. }
  204. }
  205. if (v[0][0] == 1){
  206. ans++;
  207. d1(0, 0);
  208. }
  209. if (v[0][m-1] == 1){
  210. ans++;
  211. d2(0,m-1);
  212. }
  213. if (v[n-1][0] == 1){
  214. ans++;
  215. d3(n - 1, 0);
  216. }
  217. if (v[n-1][m-1] == 1){
  218. ans++;
  219. d4(n-1, m-1);
  220. }
  221. cout<<ans<<"\n";
  222. }
  223.  
  224. int main()
  225. {
  226. ios::sync_with_stdio(0);
  227. cin.tie(0);
  228. cout.tie(0);
  229.  
  230. if (!sh) cin>>t;
  231. for (int go = 0; go < t; ++go){
  232. cin>>n>>m;
  233. string s;
  234. v.assign(n, vector <int> (m, 0));
  235. for (int i= 0; i < n; ++i){
  236. cin>>s;
  237. for (int j = 0; j < m; ++j){
  238. v[i][j] = s[j] - '0';
  239. }
  240. }
  241. if (sh){
  242. cout<<"v\n"; cvv(v);
  243. }
  244. slv();
  245. }
  246. }
  247.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement