Nita_Cristian

ai

Feb 25th, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4. #include <queue>
  5. #include <cstring>
  6. #include <iomanip>
  7. #include <climits>
  8. using namespace std;
  9.  
  10. ifstream fin("ai.in");
  11. ofstream fout("ai.out");
  12.  
  13. int a[1005][1005], pas[1005][1005];
  14.  
  15. int CMMDC(int a, int b)
  16. {
  17.     int r;
  18.     while(b)
  19.     {
  20.         r = a%b;
  21.         a = b;
  22.         b = r;
  23.     }
  24.     return a;
  25. }
  26.  
  27. struct el
  28. {
  29.     int x, y;
  30.     el(int a, int b): x(a), y(b) {};
  31.     el();
  32. };
  33.  
  34. queue<el>q;
  35.  
  36. int di[4] = {1, -1, 0, 0}, dj[4] = {0, 0, -1, 1};
  37.  
  38. int n;
  39.  
  40. bool valid(int i, int j)
  41. {
  42.     return(i >= 1 && i <= n && j >= 1 && j <= n);
  43. }
  44. int DistRTo1, DistRTo2;
  45. void Lee(int i, int j)
  46. {
  47.     q = queue<el>();
  48.     DistRTo1 = INT_MAX;
  49.     DistRTo2 = INT_MAX;
  50.     memset(pas, 0, sizeof(pas));
  51.     bool reached1=false, reached2=false;
  52.     int nexti, nextj;
  53.     q.push(el(i, j));
  54.     pas[i][j] = 1;
  55.  
  56.     while(!q.empty())
  57.     {
  58.         i = q.front().x;
  59.         j = q.front().y;
  60.         q.pop();
  61.         //  cout<<i<<' '<<j<<endl;
  62.  
  63.         for(int k=0; k<4; k++)
  64.         {
  65.             nexti = i+di[k];
  66.             nextj = j+dj[k];
  67.             if(a[nexti][nextj] == 0 && valid(i, j) && !pas[nexti][nextj])
  68.             {
  69.                 pas[nexti][nextj] = pas[i][j] + 1;
  70.                 q.push(el(nexti, nextj));
  71.             }
  72.             else if(a[nexti][nextj] == 1 && !pas[nexti][nextj])
  73.             {
  74.                 if(!reached1)
  75.                 {
  76.                     DistRTo1 = pas[i][j];
  77.                     reached1 = true;
  78.                 }
  79.                 pas[nexti][nextj] = pas[i][j] + 1;
  80.                 q.push(el(nexti, nextj));
  81.             }
  82.             else if(a[nexti][nextj] == 2 && !pas[nexti][nextj])
  83.             {
  84.                 if(!reached2)
  85.                 {
  86.                     reached2 = true;
  87.                     DistRTo2 = pas[i][j];
  88.                 }
  89.                 pas[nexti][nextj] = pas[i][j] + 1;
  90.                 q.push(el(nexti, nextj));
  91.             }
  92.             if(reached1 && reached2)
  93.             {
  94.                 return;
  95.             }
  96.  
  97.         }
  98.     }
  99.  
  100. }
  101.  
  102. int main()
  103. {
  104.     int i, j, Tx, Ty, S1x, S1y, S2x, S2y, R1x, R1y, R2x, R2y, k, x, y, dx1, dy1, dx2, dy2;
  105.  
  106.     fin>>n>>Tx>>Ty>>S1x>>S1y>>S2x>>S2y>>R1x>>R1y>>R2x>>R2y;
  107.     fin>>k;
  108.     for(i=1; i<=k; i++)
  109.     {
  110.         fin>>x>>y;
  111.         a[x][y] = -1;
  112.     }
  113.  
  114.     int lcrt=0, lmax=0;
  115.     for(i=1; i<=n; i++)
  116.     {
  117.         lcrt = 0;
  118.         for(j=1; j<=n; j++)
  119.         {
  120.             if(a[i][j] == -1)
  121.             {
  122.                 lcrt++;
  123.             }
  124.             else
  125.             {
  126.                 if(lcrt > lmax)
  127.                 {
  128.                     lmax = lcrt;
  129.                 }
  130.                 lcrt = 0;
  131.             }
  132.  
  133.         }
  134.         if(lcrt > lmax)
  135.         {
  136.             lmax = lcrt;
  137.         }
  138.     }
  139.     for(i=1; i<=n; i++)
  140.     {
  141.         lcrt = 0;
  142.         for(j=1; j<=n; j++)
  143.         {
  144.             if(a[j][i] == -1)
  145.             {
  146.                 lcrt++;
  147.             }
  148.             else
  149.             {
  150.                 if(lcrt > lmax)
  151.                 {
  152.                     lmax = lcrt;
  153.                 }
  154.                 lcrt = 0;
  155.             }
  156.  
  157.         }
  158.         if(lcrt > lmax)
  159.         {
  160.             lmax = lcrt;
  161.         }
  162.     }
  163.     fout<<lmax<<'\n';
  164.  
  165.     a[Tx][Ty] = -1;
  166.  
  167.     int cmmdc1, cmmdc2;
  168.     dx1 = Tx - S1x;
  169.     dy1 = Ty - S1y;
  170.     cmmdc1 = CMMDC(abs(dx1), abs(dy1));
  171.     dx1/=cmmdc1;
  172.     dy1/=cmmdc1;
  173.  
  174.     dx2 = Tx - S2x;
  175.     dy2 = Ty - S2y;
  176.     cmmdc2 = CMMDC(abs(dx2), abs(dy2));
  177.     dx2/=cmmdc2;
  178.     dy2/=cmmdc2;
  179.  
  180.     for(i=S1x, j=S1y; i != Tx || j != Ty; i += dx1, j += dy1)
  181.     {
  182.         if(a[i][j] != -1)
  183.             a[i][j] = 1;
  184.     }
  185.     for(i=S2x, j=S2y; i != Tx || j != Ty; i += dx2, j += dy2)
  186.     {
  187.         if(a[i][j] != -1)
  188.             a[i][j] = 2;
  189.     }
  190.  
  191.     int dr1to1, dr1to2, dr2to1, dr2to2;
  192.     Lee(R1x, R1y);
  193.  
  194.     dr1to1 = DistRTo1;
  195.     dr1to2 = DistRTo2;
  196.  
  197.     Lee(R2x, R2y);
  198.  
  199.     dr2to1 = DistRTo1;
  200.     dr2to2 = DistRTo2;
  201.  
  202.  
  203.     fout<<min(max(dr1to1, dr2to2), max(dr1to2, dr2to1));
  204.  
  205.  
  206.  
  207.     fin.close();
  208.     fout.close();
  209.     return 0;
  210.  
  211. }
Add Comment
Please, Sign In to add comment