fork download
  1. #include "bits/stdc++.h"
  2. using namespace std;
  3.  
  4. int n,m,bx,by,sx,sy,tx,ty;
  5. vector<vector<char>> a;
  6. map <pair <pair <int,int>,pair <int,int>>,int> dp;
  7.  
  8. int dx[]={+1,-1, 0, 0};
  9. int dy[]={ 0, 0,+1,-1};
  10.  
  11. bool invalid(int x,int y)
  12. {
  13. return min(x,y)<0 or x>=n or y>=m or a[x][y]=='#';
  14. }
  15.  
  16. void add(int bx,int by,int sx,int sy,queue <pair <pair <int,int>,pair <int,int>>> &q,int dist)
  17. {
  18. if(invalid(bx,by) or invalid(sx,sy) or dp.find({{bx,by},{sx,sy}})!=dp.end()) return;
  19. q.push({{bx,by},{sx,sy}});
  20. dp[{{bx,by},{sx,sy}}]=dist;
  21. }
  22.  
  23. class Solution
  24. {
  25. public:
  26. int minPushBox(vector<vector<char>>& grid)
  27. {
  28. a=grid;
  29. dp.clear();
  30. n=a.size(),m=a[0].size();
  31. for(int i=0;i<n;i++)
  32. for(int j=0;j<m;j++)
  33. if(a[i][j]=='B') bx=i,by=j;
  34. else if(a[i][j]=='S') sx=i,sy=j;
  35. else if(a[i][j]=='T') tx=i,ty=j;
  36.  
  37. queue <pair <pair <int,int>,pair <int,int>>> cur;
  38. add(bx,by,sx,sy,cur,0);
  39. while(not cur.empty())
  40. {
  41. queue <pair <pair <int,int>,pair <int,int>>> nxt;
  42. while(not cur.empty())
  43. {
  44. auto pp=cur.front();
  45. cur.pop();
  46.  
  47. int dist=dp[pp];
  48. auto box=pp.first;
  49. auto shopkeeper=pp.second;
  50. if(box.first==tx and box.second==ty) return dist;
  51.  
  52. for(int k=0;k<4;k++)
  53. {
  54. int nsx=shopkeeper.first+dx[k],nsy=shopkeeper.second+dy[k];
  55. if(invalid(nsx,nsy)) continue;
  56. if(nsx==box.first and nsy==box.second)
  57. {
  58. if(invalid(box.first+dx[k],box.second+dy[k])) continue;
  59. add(box.first+dx[k],box.second+dy[k],nsx,nsy,nxt,dist+1);
  60. }
  61. else add(box.first,box.second,nsx,nsy,cur,dist);
  62. }
  63. }
  64. cur=nxt;
  65. }
  66.  
  67. return -1;
  68. }
  69. };
  70.  
  71. #ifdef LOCAL
  72. int main()
  73. {
  74. vector <int> v={1,2};
  75. auto ans=Solution().maxSumDivThree(v);
  76. }
  77. #endif
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty