Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #include <queue>
- #include <cstring>
- #include <iomanip>
- #include <climits>
- using namespace std;
- ifstream fin("ai.in");
- ofstream fout("ai.out");
- int a[1005][1005], pas[1005][1005];
- int CMMDC(int a, int b)
- {
- int r;
- while(b)
- {
- r = a%b;
- a = b;
- b = r;
- }
- return a;
- }
- struct el
- {
- int x, y;
- el(int a, int b): x(a), y(b) {};
- el();
- };
- queue<el>q;
- int di[4] = {1, -1, 0, 0}, dj[4] = {0, 0, -1, 1};
- int n;
- bool valid(int i, int j)
- {
- return(i >= 1 && i <= n && j >= 1 && j <= n);
- }
- int DistRTo1, DistRTo2;
- void Lee(int i, int j)
- {
- q = queue<el>();
- DistRTo1 = INT_MAX;
- DistRTo2 = INT_MAX;
- memset(pas, 0, sizeof(pas));
- bool reached1=false, reached2=false;
- int nexti, nextj;
- q.push(el(i, j));
- pas[i][j] = 1;
- while(!q.empty())
- {
- i = q.front().x;
- j = q.front().y;
- q.pop();
- // cout<<i<<' '<<j<<endl;
- for(int k=0; k<4; k++)
- {
- nexti = i+di[k];
- nextj = j+dj[k];
- if(a[nexti][nextj] == 0 && valid(i, j) && !pas[nexti][nextj])
- {
- pas[nexti][nextj] = pas[i][j] + 1;
- q.push(el(nexti, nextj));
- }
- else if(a[nexti][nextj] == 1 && !pas[nexti][nextj])
- {
- if(!reached1)
- {
- DistRTo1 = pas[i][j];
- reached1 = true;
- }
- pas[nexti][nextj] = pas[i][j] + 1;
- q.push(el(nexti, nextj));
- }
- else if(a[nexti][nextj] == 2 && !pas[nexti][nextj])
- {
- if(!reached2)
- {
- reached2 = true;
- DistRTo2 = pas[i][j];
- }
- pas[nexti][nextj] = pas[i][j] + 1;
- q.push(el(nexti, nextj));
- }
- if(reached1 && reached2)
- {
- return;
- }
- }
- }
- }
- int main()
- {
- int i, j, Tx, Ty, S1x, S1y, S2x, S2y, R1x, R1y, R2x, R2y, k, x, y, dx1, dy1, dx2, dy2;
- fin>>n>>Tx>>Ty>>S1x>>S1y>>S2x>>S2y>>R1x>>R1y>>R2x>>R2y;
- fin>>k;
- for(i=1; i<=k; i++)
- {
- fin>>x>>y;
- a[x][y] = -1;
- }
- int lcrt=0, lmax=0;
- for(i=1; i<=n; i++)
- {
- lcrt = 0;
- for(j=1; j<=n; j++)
- {
- if(a[i][j] == -1)
- {
- lcrt++;
- }
- else
- {
- if(lcrt > lmax)
- {
- lmax = lcrt;
- }
- lcrt = 0;
- }
- }
- if(lcrt > lmax)
- {
- lmax = lcrt;
- }
- }
- for(i=1; i<=n; i++)
- {
- lcrt = 0;
- for(j=1; j<=n; j++)
- {
- if(a[j][i] == -1)
- {
- lcrt++;
- }
- else
- {
- if(lcrt > lmax)
- {
- lmax = lcrt;
- }
- lcrt = 0;
- }
- }
- if(lcrt > lmax)
- {
- lmax = lcrt;
- }
- }
- fout<<lmax<<'\n';
- a[Tx][Ty] = -1;
- int cmmdc1, cmmdc2;
- dx1 = Tx - S1x;
- dy1 = Ty - S1y;
- cmmdc1 = CMMDC(abs(dx1), abs(dy1));
- dx1/=cmmdc1;
- dy1/=cmmdc1;
- dx2 = Tx - S2x;
- dy2 = Ty - S2y;
- cmmdc2 = CMMDC(abs(dx2), abs(dy2));
- dx2/=cmmdc2;
- dy2/=cmmdc2;
- for(i=S1x, j=S1y; i != Tx || j != Ty; i += dx1, j += dy1)
- {
- if(a[i][j] != -1)
- a[i][j] = 1;
- }
- for(i=S2x, j=S2y; i != Tx || j != Ty; i += dx2, j += dy2)
- {
- if(a[i][j] != -1)
- a[i][j] = 2;
- }
- int dr1to1, dr1to2, dr2to1, dr2to2;
- Lee(R1x, R1y);
- dr1to1 = DistRTo1;
- dr1to2 = DistRTo2;
- Lee(R2x, R2y);
- dr2to1 = DistRTo1;
- dr2to2 = DistRTo2;
- fout<<min(max(dr1to1, dr2to2), max(dr1to2, dr2to1));
- fin.close();
- fout.close();
- return 0;
- }
Add Comment
Please, Sign In to add comment