Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <queue>
  6. using namespace std;
  7.  
  8. char loch[1002][1002];
  9. int dist[1002][1002][2];
  10. #define NORM 0
  11. #define ASTR 1
  12.  
  13. struct pole
  14. {
  15.   int x, y;
  16.   bool lev;
  17. };
  18.  
  19.  
  20. bool free(int x, int y)
  21. {
  22.   return loch[x][y]=='.' || loch[x][y]=='%';
  23. }
  24. bool free(pole asd)
  25. {
  26.   return free(asd.x, asd.y);
  27. }
  28.  
  29.  
  30. bool passable(int x, int y, bool stan)
  31. {
  32.   return free(x, y) ||
  33.          (loch[x][y]=='+' && stan==NORM) ||
  34.          (loch[x][y]=='o' && stan==ASTR);
  35. }
  36. bool passable(pole asd, bool stan)
  37. {
  38.   return passable(asd.x, asd.y, stan);
  39. }
  40.  
  41.  
  42. void process(int x, int y, bool lev, pole now, queue<pole> &tocheck)
  43. {
  44.   if(passable(x, y, lev) && dist[x][y][lev]==-1)
  45.   {
  46.     dist[x][y][lev]=dist[now.x][now.y][now.lev]+1;
  47.    
  48.     pole kurde;
  49.     kurde.x=x; kurde.y=y; kurde.lev=lev;
  50.     tocheck.push(kurde);
  51.   }
  52. }
  53.  
  54. int main()
  55. {
  56.     int ile;
  57.     scanf("%d", &ile);
  58.     while(ile--)
  59.     {
  60.         int w,h;
  61.     scanf("%d %d", &w, &h);
  62.    
  63.     for(int x=0; x<=w+1; x++) for(int y=0; y<=h+1; y++) dist[x][y][NORM]=dist[x][y][ASTR]=-1;
  64.    
  65.    
  66.     pole start, meta;
  67.    
  68.     // otaczamy scianami
  69.     for(int i=0; i<=w+1; i++) loch[i][0  ] = loch[i][h+1] = '#';
  70.                              
  71.     for(int i=1; i<=h  ; i++) loch[0  ][i] = loch[w+1][i] = '#';
  72.    
  73.     // czytamy
  74.     char derp;
  75.     for(int x=1; x<=w; x++) for(int y=1; y<=h; y++)
  76.         {
  77.             do{scanf("%c", &derp);} while(derp=='\n' || derp=='\r');
  78.      
  79.       if(derp=='>')
  80.       {
  81.         meta.x=x; meta.y=y; meta.lev=NORM;
  82.         derp='.';
  83.       }
  84.       else if(derp=='@')
  85.       {
  86.         start.x=x; start.y=y; start.lev=NORM;
  87.         derp='.';
  88.       }
  89.      
  90.       loch[x][y] = derp;
  91.         }
  92.    
  93.     // magia
  94.     pole now;
  95.     queue<pole> tocheck;
  96.     tocheck.push(start);
  97.     dist[start.x][start.y][start.lev]=0;
  98.    
  99.     while(!tocheck.empty())
  100.     {
  101.       now=tocheck.front();
  102.       tocheck.pop();
  103.      
  104.       process(now.x-1, now.y, now.lev, now, tocheck);
  105.       process(now.x+1, now.y, now.lev, now, tocheck);
  106.       process(now.x, now.y-1, now.lev, now, tocheck);
  107.       process(now.x, now.y+1, now.lev, now, tocheck);
  108.      
  109.      
  110.       if(loch[now.x][now.y]=='%')
  111.       {
  112.         process(now.x-1, now.y, !now.lev, now, tocheck);
  113.         process(now.x+1, now.y, !now.lev, now, tocheck);
  114.         process(now.x, now.y-1, !now.lev, now, tocheck);
  115.         process(now.x, now.y+1, !now.lev, now, tocheck);
  116.       }
  117.     }
  118.        
  119.    
  120.     if(dist[meta.x][meta.y][meta.lev]>=0) printf("%d\n", dist[meta.x][meta.y][meta.lev]);
  121.     else printf("NIE\n");
  122.     }
  123.    
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement