Advertisement
bartekltg

skrzyzowania

Dec 10th, 2021
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <string>
  7.  
  8.  
  9. using namespace std;
  10.  
  11. constexpr int lcm = 840;
  12.  
  13. constexpr int levels = 15;
  14.  
  15. class tablica3d{
  16.     vector<int16_t> data;
  17. public:
  18.     size_t mp1;
  19.     size_t m_lc;
  20.     tablica3d( size_t Mp1, size_t LCM, size_t levs ){
  21.         mp1 = Mp1;
  22.         m_lc = Mp1 * LCM;
  23.         data.resize(mp1*LCM*levs);
  24.     }
  25.     int16_t& at(size_t x, size_t time, size_t level){
  26.         return data[ x + mp1*time + m_lc * level  ];
  27.     }
  28. };
  29.  
  30.  
  31.  
  32. int marsz_w_przod(  tablica3d & ruch_w_przod, int x_start, int x_stop, int time ){
  33.  
  34.     while (ruch_w_przod.at(x_start,time%lcm,0) < x_stop ){
  35.         int level = levels-1;
  36.         while( (ruch_w_przod.at(x_start,time%lcm,level)>=x_stop) /*&& (level!=0)*/  ){
  37.             level--;
  38.         }
  39.         x_start = ruch_w_przod.at(x_start,time%lcm,level);
  40.         time += (1<<level);
  41.     }
  42.     return time;
  43. }
  44. int marsz_w_tyl(  tablica3d &ruch_w_tyl, int x_start, int x_stop, int time ){
  45.  
  46.     while (ruch_w_tyl.at(x_start,time%lcm,0) > x_stop ){
  47.         int level = levels-1;
  48.         while( (ruch_w_tyl.at(x_start,time%lcm,level)<=x_stop) /*&& (level!=0)*/  ){
  49.             level--;
  50.         }
  51.         x_start = ruch_w_tyl.at(x_start,time%lcm,level);
  52.         time += (1<<level);
  53.     }
  54.     return time;
  55. }
  56.  
  57. void skroty( tablica3d &ruch){
  58.     size_t m = ruch.mp1-1;
  59.  
  60.     for (int level=1; level<levels; level++){
  61.         for (int t=0; t<lcm; t++){
  62.             for (size_t x = 0; x<=m; x++){
  63.                 ruch.at(x,t,level) = ruch.at( ruch.at(x,t,level-1) ,(t + (1<<(level-1)))%lcm ,level-1);
  64.             }
  65.         }
  66.     }
  67.  
  68. }
  69.  
  70. int main(){
  71.     ios_base::sync_with_stdio(0);
  72.     cin.tie(0);
  73.  
  74.     int n,m,q;
  75.     cin>>n>>m>>q;
  76.  
  77.     vector<vector<string>> skrz (n,vector<string> (m,"")) ;
  78.  
  79.     for (int y=0; y<n; y++){
  80.         for (int x=0; x<m; x++){
  81.             cin>>skrz[y][x];   ///0 oznacza blok pionowy
  82.         }
  83.     }
  84.  
  85.     vector<vector<bool>> blok_pion (m, vector<bool> (lcm,true));
  86.  
  87.     vector<vector<bool>> blok_poziom (n, vector<bool> (lcm, true));
  88.  
  89.     for (int y=0; y<n; y++){   //da sie zoptymalizowac
  90.         for (int x=0; x<m; x++){
  91.             for (int t=0; t<lcm; t++){
  92.                 char znak = skrz[y][x][ t%(skrz[y][x].length()) ];
  93.                 if (znak=='0'){//przestaje blokować poziomo
  94.                     blok_poziom[y][t]=false;
  95.                 }else{
  96.                     blok_pion[x][t]=false;
  97.                 }
  98.             }
  99.         }
  100.     }
  101.  
  102. //    vector < vector< vector<int16_t> > > ruch_w_prawo( m+1, vector< vector<int16_t> > (lcm,
  103. //         vector<int16_t>(levels,0))  );
  104.     tablica3d ruch_w_prawo(m+1,lcm,levels);
  105.     tablica3d ruch_w_lewo(m+1,lcm,levels);
  106.     tablica3d ruch_w_dol(n+1,lcm,levels);
  107.     tablica3d ruch_w_gore(n+1,lcm,levels) ;
  108.  
  109.  
  110.     for( int t=0; t<lcm; t++ ){//jak powszechnie wiadomo....
  111.         int dojde  = m;
  112.         ruch_w_prawo.at(m,t,0) = dojde;
  113.         for (int x = m-1; x>=0; x--){
  114.             if (blok_pion[x][t])
  115.                 dojde = x;
  116.             ruch_w_prawo.at(x,t,0) = dojde;
  117.         }
  118.     }
  119.  
  120.     for( int t=0; t<lcm; t++ ){//...pisanie 10 razy tego samego kodu...
  121.         int dojde  = 0;
  122.         ruch_w_lewo.at(0,t,0) = dojde;
  123.         for (int x = 1; x<=m; x++){
  124.             if (blok_pion[x-1][t])
  125.                 dojde = x;
  126.             ruch_w_lewo.at(x,t,0) = dojde;
  127.         }
  128.     }
  129.  
  130.     for( int t=0; t<lcm; t++ ){//..znaczaca zmniejsza prawdopodobienstwo..
  131.         int dojde  = n;
  132.         ruch_w_dol.at(n,t,0) = dojde;
  133.         for (int y = n-1; y>=0; y--){
  134.             if (blok_poziom[y][t])
  135.                 dojde = y;
  136.             ruch_w_dol.at(y,t,0) = dojde;
  137.         }
  138.     }
  139.  
  140.     for( int t=0; t<lcm; t++ ){//..walniecia glupiego buga
  141.         int dojde  = 0;
  142.         ruch_w_gore.at(0,t,0) = dojde;
  143.         for (int y = 1; y<=n; y++){
  144.             if (blok_poziom[y-1][t])
  145.                 dojde = y;
  146.             ruch_w_gore.at(y,t,0) = dojde;
  147.         }
  148.     }
  149.  
  150.     skroty(ruch_w_prawo);
  151.     skroty(ruch_w_lewo);
  152.     skroty(ruch_w_dol);
  153.     skroty(ruch_w_gore);
  154.  
  155.  
  156.     for (int test =0; test < q; test++){//brute
  157.         int time, y_start, x_start, y_stop, x_stop;
  158.  
  159.         cin >> time>>y_start >> x_start >> y_stop >> x_stop ;
  160.  
  161.         int t_pion=time;
  162.         int t_poziom=time;
  163.         if (y_stop > y_start){
  164.             t_pion=marsz_w_przod( ruch_w_dol, y_start, y_stop, time );
  165.         }else if (y_stop < y_start){
  166.             t_pion = marsz_w_tyl( ruch_w_gore, y_start, y_stop, time );
  167.         }
  168.  
  169.         if (x_stop > x_start){
  170.             t_poziom=marsz_w_przod( ruch_w_prawo, x_start, x_stop, time );
  171.         }else if (x_stop < x_start){
  172.             t_poziom = marsz_w_tyl( ruch_w_lewo, x_start, x_stop, time );
  173.         }
  174.  
  175.         cout << max(t_pion, t_poziom)<<'\n';
  176.  
  177.     }
  178.  
  179.     return 0;
  180. }
  181.  
  182.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement