Advertisement
YEZAELP

PROG-1161: Sewer

Jun 6th, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int INF=1e9;
  4. using pii=pair<int,int>;
  5. int n,m;
  6. vector <int> g[10001];
  7. vector <int> dis(100001,INF);
  8. bool pst(int i,int j) {
  9.     return i>=1 and i<=n and j>=1 and j<=m;
  10. }
  11. int drt(int i,int j){
  12.     return m*(i-1)+j;
  13. }
  14. int I(int v){
  15.     if(v%m!=0) return v/m+1;
  16.     else return v/m;
  17. }
  18. int J(int v){
  19.     v=v%m;
  20.     if(v==0) return m;
  21.     return v;
  22. }
  23. int main(){
  24.     scanf("%d%d",&n,&m);
  25.  
  26.     for(int i=1;i<=n;i++){
  27.         for(int j=1;j<=m;j++){
  28.             char a;
  29.             int u,v;
  30.             scanf(" %c",&a);
  31.             u=drt(i,j);
  32.             if(( a=='R' or a=='B' ) and pst(i,j+1)) {
  33.                 v=drt(i,j+1);
  34.                 g[u].push_back(v);
  35.                 g[v].push_back(u);
  36.             }
  37.             if(( a=='D' or a=='B') and pst(i+1,j)) {
  38.                 v=drt(i+1,j);
  39.                 g[u].push_back(v);
  40.                 g[v].push_back(u);
  41.             }
  42.         }
  43.     }
  44.  
  45.     priority_queue <pii,vector<pii>,greater<pii>> pq;
  46.     dis[1]=1;
  47.     pq.push({1,1});
  48.     while(pq.size()>0){
  49.         int u,d;
  50.         d=pq.top().first;
  51.         u=pq.top().second;
  52.         pq.pop();
  53.  
  54.         for(auto v:g[u]){
  55.             if(d+1<dis[v]){
  56.                 dis[v]=d+1;
  57.                 pq.push({d+1,v});
  58.             }
  59.             else if(dis[v]==d+1){
  60.                 printf("%d\n%d %d",d+1,I(v),J(v));
  61.                 return 0;
  62.             }
  63.         }
  64.  
  65.     }
  66.  
  67.     return 0;
  68. }
  69. /*
  70. 3 3
  71. B R D
  72. D B N
  73. R N N
  74. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement