Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<fstream>
- #include<queue>
- using namespace std;
- int dx[]= {-1,-1,-2,-2,+1,+1,+2,+2};///Deplasarile dupa cele 8 directii
- int dy[]= {-2,+2,-1,+1,-2,+2,-1,+1};
- struct LOCATIE /// Structura unei locatii
- {
- int x,y;
- };
- LOCATIE castel;///Adresa castel
- LOCATIE cavaleri[1001];///Adrese cavaleri
- queue<LOCATIE> Coada; /// Coada de asteptare
- int A[1001][1001];///Harta
- int p,m,n,k;
- int raspuns1,raspuns2;///Raspunsurile la cele doua intrebari
- void citire()
- {
- ifstream f("camelot.in");
- f>>p>>m>>n>>k;
- f>>castel.x>>castel.y;
- for(int i=1; i<=k; i++)
- {
- f>>cavaleri[i].x>>cavaleri[i].y;
- }
- f.close();
- }
- void rezolvare()
- {
- LOCATIE p,adresa;
- int i,x,y;
- Coada.push(castel);
- A[castel.x][castel.y]=1;
- while(!Coada.empty())
- {
- p=Coada.front();
- Coada.pop();
- for(i=0;i<8;++i)
- {
- x=p.x+dx[i];
- y=p.y+dy[i];
- if(x>=1&&x<=m&&y>=1&&y<=n)
- if(A[x][y]==0)
- {
- adresa.x=x;
- adresa.y=y;
- Coada.push(adresa);
- A[x][y]=1+A[p.x][p.y];
- }
- }
- }
- p=cavaleri[1];
- raspuns1=raspuns2=A[p.x][p.y];
- for(i=2;i<=k;++i)
- {
- if(raspuns1>A[cavaleri[i].x][cavaleri[i].y])
- raspuns1=A[cavaleri[i].x][cavaleri[i].y];
- if(raspuns2<A[cavaleri[i].x][cavaleri[i].y])
- raspuns2=A[cavaleri[i].x][cavaleri[i].y];
- }
- --raspuns1;
- --raspuns2;
- }
- void afisare()
- {
- ofstream g("camelot.out");
- if(p==1)
- g<<raspuns1;
- else
- g<<raspuns2;
- g.close();
- }
- int main()
- {
- citire();
- rezolvare();
- afisare();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement