Submission #61396418


Source Code Expand

Copy
/* -------------------------------------- HEADER FILES ---------------------------------------- */
/**
* username: aditya_0670
* created: 2025-01-04 01:53:22
**/
#include <bits/stdc++.h>
using namespace std;
/* ------------------------------------ MACROS & CONSTANTS ------------------------------------ */
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define debug(...) ;
#endif
#define int long long
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
/* -------------------------------------- HEADER FILES ---------------------------------------- */

/**
 *    username:  aditya_0670
 *    created: 2025-01-04 01:53:22
 **/

  #include <bits/stdc++.h>
  using namespace std;

/* ------------------------------------ MACROS & CONSTANTS ------------------------------------ */

  #ifndef ONLINE_JUDGE
      #include "debug.h"
  #else
      #define debug(...) ;
  #endif

  #define int             long long
  #define all(v)          (v).begin(), (v).end()
  #define rall(v)         (v).rbegin(), (v).rend()
  #define fastio          ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)

  constexpr int MOD = 1E9 + 7;
  constexpr int INF = 1E18;

  vector<array<int,2>> horz_dir = {{0, 1}, {0, -1}};
  vector<array<int,2>> vert_dir = {{1, 0}, {-1, 0}};


/* ------------------------------------ OTHER FUNCTIONS ------------------------------------ */


/* ------------------------------------ LAMBDA TEMPLATE ------------------------------------- */

  auto solve = [] -> void
  {
      int n , m; cin >> n >> m;

      vector<string> g(n);
      for (auto& i : g) cin >> i;

      int s_i = -1, s_j = -1, e_i = -1, e_j = -1;

      for (int i = 0; i < n; i++)
          for (int j = 0; j < m; j++)
          {
             if (g[i][j] == 'S') s_i = i , s_j = j;
             if (g[i][j] == 'G') e_i = i , e_j = j;
          }

      auto bfs =[&](int s_i, int s_j, int e_i, int e_j) -> int
      {
           vector<vector<vector<int>>> d(n, vector<vector<int>>(m, vector<int>(2, INF)));

           queue<array<int,3>> q;

           d[s_i][s_j][0] = 0;
           d[s_i][s_j][1] = 0;

           q.push({s_i, s_j, 0});
           q.push({s_i, s_j, 1});

           while (!q.empty())
           {
               auto [c_i, c_j, ok] = q.front();
               q.pop();

               if (c_i == e_i && c_j == e_j) return d[c_i][c_j][ok];

               if (!ok)
               {
                   for (auto& [di, dj] : horz_dir)
                   {
                       int ni = c_i + di, nj = c_j + dj;

                       if (ni >= 0 && ni < n && nj >= 0 && nj < m && g[ni][nj]!= '#' && d[ni][nj][1] == INF)
                       {
                           d[ni][nj][1] = d[c_i][c_j][0] + 1;
                           q.push({ni, nj, 1});
                       }
                   }
               }
               else 
               {
                  for (auto& [di, dj] : vert_dir)
                  {
                      int ni = c_i + di, nj = c_j + dj;

                      if (ni >= 0 && ni < n && nj >= 0 && nj < m && g[ni][nj]!= '#' && d[ni][nj][0] == INF)
                      {
                          d[ni][nj][0] = d[c_i][c_j][1] + 1;
                          q.push({ni, nj, 0});
                      }
                  }
               }
           }

           return -1;
      };

      cout << bfs(s_i, s_j, e_i, e_j) << '\n';
  };

/* ------------------------------------ MAIN FUNCTION --------------------------------------- */

  signed main() 
  {
      fastio;

      int t = 1;
      while (t--) solve();

      return 0;
  }

Submission Info

Submission Time
Task D - Snaky Walk
User aditya_0670
Language C++ 23 (gcc 12.2)
Score 400
Code Size 3264 Byte
Status AC
Exec Time 118 ms
Memory 59364 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 57
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 02_random2_06.txt, 02_random2_07.txt, 02_random2_08.txt, 02_random2_09.txt, 02_random2_10.txt, 02_random2_11.txt, 02_random2_12.txt, 02_random2_13.txt, 02_random2_14.txt, 02_random2_15.txt, 02_random2_16.txt, 02_random2_17.txt, 02_random2_18.txt, 02_random2_19.txt, 03_random3_00.txt, 03_random3_01.txt, 03_random3_02.txt, 03_random3_03.txt, 04_random4_00.txt, 04_random4_01.txt, 04_random4_02.txt, 04_random4_03.txt, 05_handmade_00.txt, 05_handmade_01.txt, 05_handmade_02.txt, 05_handmade_03.txt, 05_handmade_04.txt, 05_handmade_05.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3528 KB
00_sample_01.txt AC 1 ms 3476 KB
00_sample_02.txt AC 1 ms 3456 KB
01_random_00.txt AC 16 ms 20216 KB
01_random_01.txt AC 2 ms 4660 KB
01_random_02.txt AC 35 ms 47188 KB
01_random_03.txt AC 10 ms 15208 KB
01_random_04.txt AC 31 ms 34496 KB
01_random_05.txt AC 2 ms 4536 KB
01_random_06.txt AC 1 ms 3332 KB
01_random_07.txt AC 1 ms 3360 KB
01_random_08.txt AC 48 ms 59020 KB
01_random_09.txt AC 98 ms 59084 KB
01_random_10.txt AC 95 ms 58952 KB
01_random_11.txt AC 64 ms 59044 KB
01_random_12.txt AC 66 ms 58952 KB
01_random_13.txt AC 100 ms 59120 KB
01_random_14.txt AC 64 ms 59116 KB
01_random_15.txt AC 49 ms 59020 KB
01_random_16.txt AC 49 ms 58956 KB
01_random_17.txt AC 49 ms 58960 KB
01_random_18.txt AC 106 ms 59120 KB
01_random_19.txt AC 64 ms 59056 KB
02_random2_00.txt AC 50 ms 59100 KB
02_random2_01.txt AC 49 ms 59056 KB
02_random2_02.txt AC 50 ms 58964 KB
02_random2_03.txt AC 50 ms 59008 KB
02_random2_04.txt AC 50 ms 59016 KB
02_random2_05.txt AC 49 ms 59116 KB
02_random2_06.txt AC 51 ms 59084 KB
02_random2_07.txt AC 52 ms 59032 KB
02_random2_08.txt AC 49 ms 59084 KB
02_random2_09.txt AC 50 ms 59044 KB
02_random2_10.txt AC 48 ms 59116 KB
02_random2_11.txt AC 49 ms 58884 KB
02_random2_12.txt AC 49 ms 59012 KB
02_random2_13.txt AC 49 ms 59044 KB
02_random2_14.txt AC 50 ms 59048 KB
02_random2_15.txt AC 49 ms 59008 KB
02_random2_16.txt AC 50 ms 58884 KB
02_random2_17.txt AC 50 ms 58884 KB
02_random2_18.txt AC 53 ms 59056 KB
02_random2_19.txt AC 52 ms 59044 KB
03_random3_00.txt AC 115 ms 59032 KB
03_random3_01.txt AC 73 ms 59020 KB
03_random3_02.txt AC 118 ms 58952 KB
03_random3_03.txt AC 114 ms 59048 KB
04_random4_00.txt AC 110 ms 59020 KB
04_random4_01.txt AC 108 ms 59080 KB
04_random4_02.txt AC 105 ms 59112 KB
04_random4_03.txt AC 112 ms 58996 KB
05_handmade_00.txt AC 67 ms 59032 KB
05_handmade_01.txt AC 97 ms 59364 KB
05_handmade_02.txt AC 1 ms 3540 KB
05_handmade_03.txt AC 1 ms 3468 KB
05_handmade_04.txt AC 1 ms 3572 KB
05_handmade_05.txt AC 105 ms 58688 KB


2025-03-05 (Wed)
20:49:38 +00:00