fork download
  1. // ROOT : DRAGON3012009
  2. #include <bits/stdc++.h>
  3. #define ll int
  4. #define ld long double
  5. #define el "\n"
  6. #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  7. #define __ROOT__ int main()
  8. #pragma GCC optimize("O2")
  9. //#pragma GCC optimize("unroll-loops")
  10. //#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
  11. #define FOR(i,l,r) for(int i = l ; i <= r ; i ++)
  12. #define FORD(i,r,l) for(int i = r ; i >= l ; i --)
  13. #define REP(i, a ) for(int i = 0 ; i < a ; i ++ )
  14. #define fi first
  15. #define se second
  16. #define M 1000000007
  17. #define MAXN 5001
  18. #define INF (1ll<<30)
  19. #define BLOCK_SIZE 425
  20. #define MAX_NODE 1001001
  21. #define LOG 19
  22. #define ALPHA_SIZE 26
  23. #define BASE 256
  24. #define NAME "file"
  25. #define compare(v) sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end());
  26. using namespace std;
  27. using namespace chrono ;
  28. const ll MOD[] = {(ll)1e9 + 2277, (ll)1e9 + 5277, (ll)1e9 + 8277, (ll)1e9 + 9277, (ll) 1e9 + 7 };
  29. const ll NMOD = 1;
  30. const int dx[] = {0,0,1,1,1,-1,-1,-1};
  31. const int dy[] = {1,-1,0,1,-1,0,-1,1};
  32. //**Variable**//
  33. ll n, m , ans = INF ;
  34. char arr[MAXN][MAXN] ;
  35. ll d[MAXN][MAXN] ;
  36. //**Struct**//
  37. struct Data {
  38. ll u, v, w ;
  39. bool operator < (const Data & other ) const {
  40. return w > other.w ;
  41. }
  42. };
  43. //**Function**//
  44. template<class X, class Y >
  45. bool minimize(X & x, const Y &y ) {
  46. return x > y ? x = y, 1:0 ;
  47. }
  48. template<class X, class Y >
  49. bool maximize(X &x, const Y &y ) {
  50. return x < y ? x = y, 1:0 ;
  51. }
  52.  
  53. void init() {
  54. cin>>n >> m ;
  55. FOR(i,1, n) FOR(j, 1, m) {
  56. cin >> arr[i][j] ;
  57. if(arr[i][j] == '#') arr[i][j] = '0';
  58. }
  59. }
  60. void Dijkstra(ll st1, ll st2 ) {
  61. FOR(i, 0, n ) FOR(j, 0, m ) d[i][j] = INF ;
  62. priority_queue<Data > pq ;
  63. d[st1][st2] = arr[st1][st2] - '0' ;
  64. pq.push({st1, st2, d[st1][st2]}) ;
  65. while(!pq.empty()) {
  66. Data u =pq.top() ;
  67. pq.pop() ;
  68. FOR(k, 0, 7) {
  69. ll ni = u.u + dx[k] ;
  70. ll nj = u.v + dy[k] ;
  71. if(ni >= 1 && nj >= 1 && ni <= n && nj <= m && arr[ni][nj] != '.') {
  72. ll w = u.w + arr[ni][nj] -'0' ;
  73. if(d[ni][nj] > w ) pq.push({ni, nj, d[ni][nj] = w }) ;
  74. }
  75. }
  76. }
  77. }
  78. void solve() {
  79. FOR(i, 2, n ) {
  80. if(arr[i][1] == '.' ) continue ;
  81. Dijkstra(i, 1) ;
  82. FOR(k, 1, m ) minimize(ans, d[1][k]) ;
  83. FOR(k,1, n ) minimize(ans, d[k][m]) ;
  84. }
  85. FOR(i, 1, m - 1 ) {
  86. if(arr[n][i] == '.') continue ;
  87. Dijkstra(n, i ) ;
  88. FOR(k, 1, m ) minimize(ans, d[1][k]) ;
  89. FOR(k,1, n ) minimize(ans, d[k][m]) ;
  90. }
  91. cout << (ans == INF ? -1 : ans) << el ;
  92. }
  93.  
  94.  
  95. __ROOT__ {
  96. // freopen(NAME".inp" , "r" , stdin);
  97. // freopen(NAME".out" , "w", stdout) ;
  98. fast;
  99. srand(time(nullptr)) ;
  100. int t = 1; // cin >> t ;
  101. while(t--) {
  102. init();
  103. solve();
  104. }
  105. return (0&0);
  106. }
  107.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
-1