Home BOJ. Goody Bye Find Dust! (17144)
Post
Cancel

BOJ. Goody Bye Find Dust! (17144)

[Link] https://www.acmicpc.net/problem/17144


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
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i <= n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
typedef long long ll;
const int INF = 987654321;

int r, c, t;
int airi, airj;
int mm[50][50];
int update[50][50];
int didown[4] = { 0, 1, 0, -1 };
int djdown[4] = { 1, 0, -1, 0 };
int diup[4] = { 0, -1, 0, 1 };
int djup[4] = { 1, 0, -1, 0 };
vector<string> str_vec;

#define out_of_range ai < 0 || aj < 0 || ai >= r || aj >= c

void solve() {
  for0(iter, t) {
    for0(i, r) for0(j, c) update[i][j] = 0;

    for0(i, r) {
      for0(j, c) {
        if(!mm[i][j] || mm[i][j] == -1) continue;
        int adj_cnt = 0;
        for0(k, 4) {
          int ai = i + diup[k], aj = j + djup[k];
          if(out_of_range || mm[ai][aj] == -1) continue;
          adj_cnt++;
          update[ai][aj] += mm[i][j] / 5;
        }
        update[i][j] -= mm[i][j] / 5 * adj_cnt;
      }
    }
    for0(i, r) {
      for0(j, c) {
        mm[i][j] += update[i][j];
      }
    }

    int cairi = airi - 1 , cairj = airj + 1;
    int prev = mm[cairi][cairj];
    mm[cairi][cairj] = 0;
    for0(dir, 4) {
      while(true) {
        int ai = cairi + diup[dir], aj = cairj + djup[dir];
        if(out_of_range || (ai == airi - 1&& aj == airj)) break;
        int swap = mm[ai][aj];
        mm[ai][aj] = prev;
        prev = swap;
        cairi = ai, cairj = aj;
      }
    }

    cairi = airi, cairj = airj + 1;
    prev = mm[cairi][cairj];
    mm[cairi][cairj] = 0;
    for0(dir, 4) {
      while(true) {
        int ai = cairi + didown[dir], aj = cairj + djdown[dir];
        if(out_of_range || (ai == airi&& aj == airj)) break;
        int swap = mm[ai][aj];
        mm[ai][aj] = prev;
        prev = swap;
        cairi = ai, cairj = aj;
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
  cin >> r >> c >> t;

  for0(i, r) {
    for0(j, c) {
      cin >> mm[i][j];
      if(mm[i][j] == -1) airi = i, airj = j;
    }
  }
  solve();
  int sum = 0;
  for0(i, r) {
    for0(j, c) {
      if(mm[i][j] && mm[i][j] != -1) sum += mm[i][j];
    }
  }
  cout << sum;
  return 0;
}
This post is licensed under CC BY 4.0 by the author.