Advertisement
vlatkovski

latest competitive programming failure

Mar 5th, 2021
1,227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.87 KB | None | 0 0
  1. /*
  2. http://usaco.org/index.php?page=viewproblem2&cpid=1065
  3.  
  4. I thought the robots all move in a single direction randomly chosen at the start.
  5. I didn't realize they could actually change direction at any point.
  6. This solution works for the first case.
  7. Two hours wasted FeelsBadMan
  8. */
  9.  
  10. #include <bits/stdc++.h>
  11. using namespace std;
  12. typedef long long ll;
  13. typedef pair<int, int> pii;
  14.  
  15. const int EMPTY = 0, WALL = 1, START = 2;
  16. const int MAXN = 1000;
  17.  
  18. int N, D;
  19. int grid[MAXN][MAXN];
  20. int cumres[MAXN][MAXN]; // CUMULATIVE, not the other word
  21.  
  22. typedef pair<pii, int> piii;
  23. int maxreach[MAXN][MAXN];
  24. piii dp[MAXN][MAXN]; // ((r, c), distance)
  25. pii status[MAXN][MAXN]; // (iterations left, reach at end)
  26. int pref[MAXN][MAXN];
  27.  
  28. void PRINT(string s, int a[MAXN][MAXN]) {
  29.     cout << s << ":\n";
  30.     for (int r = 0; r < N; ++r) {
  31.         for (int c = 0; c < N; ++c) {
  32.             cout << setw(2) << right << a[r][c] << ' ';
  33.         }
  34.         cout << '\n';
  35.     }
  36. }
  37.  
  38. string TOSTRING(pii a) {
  39.     return "(" + to_string(a.first) + "," + to_string(a.second) + ")";
  40. }
  41.  
  42. string TOSTRING(piii a) {
  43.     return "((" + to_string(a.first.first) + "," + to_string(a.first.second) + ")," + to_string(a.second) + ")";
  44. }
  45.  
  46. void DFS(int r, int c) {
  47.     cout << "DFS("<<r<<","<<c<<"),mr="<<maxreach[r][c]<<",dp="<<TOSTRING(dp[r][c])<<"\n";
  48.     if (maxreach[r][c] < D+1) return;
  49.     if (r > 0 && dp[r][c].first == dp[r-1][c+D].first) {
  50.         DFS(r-1, c+D);
  51.     } else if (r+1 < N && dp[r][c].first == dp[r+1][c+D].first) {
  52.         DFS(r+1, c+D);
  53.     } else {
  54.         DFS(r, c+D+1);
  55.     }
  56. }
  57.  
  58. inline void update_node(piii& a, piii b) {
  59.     if (b.second < a.second) {
  60.         a = b;
  61.     } else if (b.second == a.second) {
  62.         if (maxreach[b.first.first][b.first.second]
  63.             < maxreach[a.first.first][a.first.second]) {
  64.             a = b;
  65.         }
  66.     }
  67. }
  68.  
  69. inline void update_status(pii& a, pii b) {
  70.     if (b.first > a.first) {
  71.         a = b;
  72.     } else if (b.first == a.first && b.second > a.second) {
  73.         a = b;
  74.     }
  75. }
  76.  
  77. void solve() {
  78.     PRINT("grid", grid);
  79.     vector<int> nxt(N), nxt2(N);
  80.     for (int c = N-1; c >= 0; --c) {
  81.         for (int r = 0; r < N; ++r) {
  82.             nxt[r] = (grid[r][c] == WALL) ? c : nxt2[r];
  83.             if (nxt[r] <= c+D) { // not full strip
  84.                 maxreach[r][c] = nxt[r] - c;
  85.                 dp[r][c] = make_pair(make_pair(r, c), 0);
  86.             } else {
  87.                 maxreach[r][c] = D+1;
  88.                 dp[r][c] = dp[r][c+D+1];
  89.                 if (r > 0) update_node(dp[r][c], dp[r-1][c+D]);
  90.                 if (r+1 < N) update_node(dp[r][c], dp[r+1][c+D]);
  91.                 dp[r][c].second += 1; // INCREASE DISTANCE BY 1
  92.             }
  93.             status[r][c].first = -1;
  94.             pref[r][c] = 0;
  95.         }
  96.         swap(nxt, nxt2);
  97.     }
  98.     PRINT("maxreach", maxreach);
  99.     DFS(3, 1);
  100.     for (int c = 0; c < N; ++c) {
  101.         for (int r = 0; r < N; ++r) {
  102.             if (grid[r][c] == START) {
  103.                 int r1 = dp[r][c].first.first;
  104.                 int c1 = dp[r][c].first.second;
  105.                 update_status(  status[r][c],
  106.                                 make_pair(dp[r][c].second, maxreach[r1][c1]));
  107.                 cout<<"Start("<<r<<","<<c<<")\n";
  108.             }
  109.             int iter = status[r][c].first;
  110.             if (iter == -1) continue;
  111.             int reach = iter > 0 ? D+1 : status[r][c].second;
  112.             cout << "r="<<r<<" c="<<c<<" status="<<TOSTRING(status[r][c])<<"\n";
  113.             cout << "pref adding "<<r<<"["<<c<<","<<c+reach<<"]\n";
  114.             pref[r][c] += 1;
  115.             pref[r][c + reach] -= 1;
  116.             if (iter > 0) {
  117.                 update_status(status[r][c+D+1], make_pair(iter-1, status[r][c].second));
  118.                 update_status(status[r-1][c+D], make_pair(iter-1, status[r][c].second));
  119.                 update_status(status[r+1][c+D], make_pair(iter-1, status[r][c].second));
  120.             }
  121.         }
  122.     }
  123.     for (int r = 0; r < N; ++r) {
  124.         int x = 0;
  125.         for (int c = 0; c < N; ++c) {
  126.             x += pref[r][c];
  127.             cumres[r][c] += x;
  128.         }
  129.     }
  130.     PRINT("cumres", cumres);
  131.     cout<<"\n";
  132. }
  133.  
  134. void rotate_90(int g[MAXN][MAXN]) {
  135.     static int temp[MAXN][MAXN];
  136.     for (int i = 0; i < N; ++i) {
  137.         for (int j = 0; j < N; ++j) {
  138.             temp[j][N-1-i] = g[i][j];
  139.         }
  140.     }
  141.     for (int i = 0; i < N; ++i) {
  142.         for (int j = 0; j < N; ++j) {
  143.             g[i][j] = temp[i][j];
  144.         }
  145.     }
  146. }
  147.  
  148. int main() {
  149.     ios::sync_with_stdio(false);
  150.     cin.tie(0);
  151.     cin >> N >> D;
  152.     for (int r = 0; r < N; ++r) {
  153.         for (int c = 0; c < N; ++c) {
  154.             char ch;
  155.             cin >> ch;
  156.             if (ch == '.') grid[r][c] = EMPTY;
  157.             else if (ch == '#' || ch == 'X') grid[r][c] = WALL;
  158.             else grid[r][c] = START;
  159.             cumres[r][c] = 0;
  160.         }
  161.     }
  162.     for (int i = 0; i < 4; ++i) {
  163.         solve();
  164.         //break;
  165.         rotate_90(grid);
  166.         rotate_90(cumres);
  167.     }
  168.     int ans = 0;
  169.     for (int r = 0; r < N; ++r)
  170.         for (int c = 0; c < N; ++c)
  171.             if (cumres[r][c] > 0)
  172.                 ++ans;
  173.     cout << ans << '\n';
  174. }
  175.  
  176. /*
  177. (13)
  178. 7 2
  179. XXXXXXX
  180. X.....X
  181. X.....X
  182. XS....X
  183. X.....X
  184. X.....X
  185. XXXXXXX
  186.  
  187. (15)
  188. 10 1
  189. ##########
  190. #........#
  191. #S.......#
  192. #........#
  193. ##########
  194. #S....S..#
  195. ##########
  196. ##########
  197. ##########
  198. ##########
  199.  
  200. (28)
  201. 10 2
  202. ##########
  203. #.#......#
  204. #.#......#
  205. #S.......#
  206. #.#......#
  207. #.#......#
  208. ##########
  209. ##########
  210. ##########
  211. ##########
  212.  
  213. (10)
  214. 10 2
  215. ##########
  216. #.S#.....#
  217. #..#.....#
  218. #S.......#
  219. #..#.....#
  220. #..#.....#
  221. ##########
  222. ##########
  223. ##########
  224. ##########
  225. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement