SHARE
    TWEET
    Vince14

    What's Up With Gravity

    Feb 25th, 2023 (edited)
    93
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    C++ 6.68 KB | Source Code | 0 0
    1. #include <iostream>
    2. #include <string>
    3. #include <cstring>
    4. #include <algorithm>
    5. #include <cmath>
    6. #include <vector>
    7. #include <set>
    8. #include <map>
    9. #include <stack>
    10. #include <queue>
    11. #include <deque>
    12. #include <unordered_map>
    13. #include <numeric>
    14. #include <iomanip>
    15. using namespace std;
    16. #define pii pair<int, int>
    17. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
    18. const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
    19. const int dl[2] = {1, -1};
    20. const int MOD = 1e9;
    21. const int MAX = 505;
    22. int n, m;
    23. int world[MAX][MAX]; // 1 = Blocked, 0 = Empty, 2 = Start, 3 = End;
    24. int dist[MAX][MAX][2];
    25. pii spos, epos;
    26. pii fall[MAX][MAX][2];
    27. void print_world(){
    28. for(int i = 0; i < n; i++){
    29. for(int j = 0; j < m; j++){
    30. cout << world[i][j];
    31. }
    32. cout << "\n";
    33. }
    34. }
    35. void print_dist(){
    36. for(int i = 0; i < n; i++){
    37. for(int j = 0; j < m; j++){
    38. cout << dist[i][j][0] << " ";
    39. }
    40. cout << "\n";
    41. }
    42. for(int i = 0; i < n; i++){
    43. for(int j = 0; j < m; j++){
    44. cout << dist[i][j][1] << " ";
    45. }
    46. cout << "\n";
    47. }
    48. }
    49. void input(){
    50. for(int i = 0; i < n; i++){
    51. string s;
    52. cin >> s;
    53. for(int j = 0; j < s.size(); j++){
    54. int tmp;
    55. if(s[j] == '#'){
    56. tmp = 1;
    57. }
    58. else if(s[j] == 'C'){
    59. tmp = 2;
    60. spos = make_pair(i, j);
    61. }
    62. else if(s[j] == 'D'){
    63. tmp = 3;
    64. epos = make_pair(i, j);
    65. }
    66. else{
    67. tmp = 0;
    68. }
    69. world[i][j] = tmp;
    70. }
    71. }
    72. }
    73. // gravity: 0 = inc, 1 = dec
    74. // (-1, -1) = dead;
    75. void init_gravity(){
    76. for(int i = 0; i < n; i++){
    77. for(int j = 0; j < m; j++){
    78. fall[i][j][0] = make_pair(-2, -2);
    79. fall[i][j][1] = make_pair(-2, -2);
    80. }
    81. }
    82. }
    83. pii check_gravity(int x, int y, int gravity){
    84. if(fall[x][y][gravity] != make_pair(-2, -2)){
    85. return fall[x][y][gravity];
    86. }
    87. if(gravity == 0){
    88. if(x == n - 1){
    89. return fall[x][y][gravity] = make_pair(-1, -1);
    90. }
    91. else if(world[x + 1][y] != 1){
    92. return fall[x][y][gravity] = check_gravity(x + 1, y, gravity);
    93. }
    94. else{
    95. return fall[x][y][gravity] = make_pair(x, y);
    96. }
    97. }
    98. else if(gravity == 1){
    99. if(x == 0){
    100. return fall[x][y][gravity] = make_pair(-1, -1);
    101. }
    102. else if(world[x - 1][y] != 1){
    103. return fall[x][y][gravity] = check_gravity(x - 1, y, gravity);
    104. }
    105. else{
    106. return fall[x][y][gravity] = make_pair(x, y);
    107. }
    108. }
    109. }
    110. void init_fall(){
    111. for(int i = 0; i < n; i++){
    112. for(int j = 0; j < m; j++){
    113. if(world[i][j] != 1){
    114. check_gravity(i, j, 0);
    115. check_gravity(i, j, 1);
    116. }
    117. }
    118. }
    119. }
    120. void print_fall(){
    121. for(int i = 0; i < n; i++){
    122. for(int j = 0; j < m; j++){
    123. cout << "Gravity of: " << i << " " << j << "\n";
    124. cout << "Down: " << fall[i][j][0].first << " " << fall[i][j][0].second << "\n";
    125. cout << "Up: " << fall[i][j][1].first << " " << fall[i][j][1].second << "\n";
    126. }
    127. }
    128. }
    129. void init_dist(){
    130. for(int i = 0; i < n; i++){
    131. for(int j = 0; j < m; j++){
    132. dist[i][j][0] = -1;
    133. dist[i][j][1] = -1;
    134. }
    135. }
    136. }
    137. void BFS(){
    138. deque<pair<pii, pii>> q; // coords, gravity
    139. q.push_front(make_pair(spos, make_pair(0, 0)));
    140. while(!q.empty()){
    141. int x = q.front().first.first;
    142. int y = q.front().first.second;
    143. int g = q.front().second.first;
    144. int flips = q.front().second.second;
    145. q.pop_front();
    146. // cout << "BFS: " << x << " " << y << " " << g << " " << flips << "\n";
    147. if(dist[x][y][g] != -1){
    148. // cout << "Visited\n";
    149. continue;
    150. }
    151. dist[x][y][g] = flips;
    152. pii coords = fall[x][y][g];
    153. if(coords == make_pair(-1, -1)){
    154. for(int i = x; i < n and i >= 0; i -= g * 2 - 1){
    155. if(dist[i][y][g] == -1){
    156. dist[i][y][g] = flips;
    157. }
    158. else{
    159. dist[i][y][g] = min(dist[i][y][g], flips);
    160. }
    161. }
    162. // cout << "Fell out\n";
    163. continue;
    164. }
    165. if(coords == make_pair(x, y)){
    166. // cout << "No change from gravity\n";
    167. }
    168. else{
    169. if(x < coords.first){
    170. for(int i = x; i <= coords.first; i++){
    171. if(dist[i][y][g] == -1){
    172. dist[i][y][g] = flips;
    173. }
    174. else{
    175. dist[i][y][g] = min(dist[i][y][g], flips);
    176. }
    177. }
    178. }
    179. else{
    180. for(int i = coords.first; i <= x; i++){
    181. if(dist[i][y][g] == -1){
    182. dist[i][y][g] = flips;
    183. }
    184. else{
    185. dist[i][y][g] = min(dist[i][y][g], flips);
    186. }
    187. }
    188. }
    189. x = coords.first;
    190. y = coords.second;
    191. }
    192. for(int i = 0; i < 2; i++){
    193. int ny = y + dl[i];
    194. // cout << x << " " << ny << ": ";
    195. if(ny < 0 or ny >= m){
    196. // cout << "out of bounds\n";
    197. continue;
    198. }
    199. else if(world[x][ny] == 1){
    200. // cout << "blocked\n";
    201. continue;
    202. }
    203. else{
    204. // cout << "pushed\n";
    205. q.push_front(make_pair(make_pair(x, ny), make_pair(g, flips)));
    206. }
    207. }
    208. // cout << "gravity flipped\n";
    209. q.push_back(make_pair(make_pair(x, y), make_pair(1 - g, flips + 1)));
    210. }
    211. }
    212. void ans(){
    213. int ex = epos.first;
    214. int ey = epos.second;
    215. if(dist[ex][ey][0] == -1 and dist[ex][ey][1] == -1){
    216. cout << "-1";
    217. }
    218. else if(dist[ex][ey][0] == -1){
    219. cout << dist[ex][ey][1];
    220. }
    221. else if(dist[ex][ey][1] == -1){
    222. cout << dist[ex][ey][0];
    223. }
    224. else{
    225. cout << min(dist[ex][ey][1], dist[ex][ey][0]);
    226. }
    227. }
    228. int main() {
    229. FAST;
    230. freopen("gravity.in", "r", stdin);
    231. freopen("gravity.out", "w", stdout);
    232. cin >> n >> m;
    233. input();
    234. init_gravity();
    235. init_fall();
    236. // print_fall();
    237. // print_world();
    238. init_dist();
    239. BFS();
    240. // print_dist();
    241. ans();
    242. }
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /