Advertisement
a53

camelot

a53
May 2nd, 2017
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. #include<fstream>
  2. #include<queue>
  3. using namespace std;
  4. int dx[]= {-1,-1,-2,-2,+1,+1,+2,+2};///Deplasarile dupa cele 8 directii
  5. int dy[]= {-2,+2,-1,+1,-2,+2,-1,+1};
  6. struct LOCATIE /// Structura unei locatii
  7. {
  8. int x,y;
  9. };
  10. LOCATIE castel;///Adresa castel
  11. LOCATIE cavaleri[1001];///Adrese cavaleri
  12. queue<LOCATIE> Coada; /// Coada de asteptare
  13. int A[1001][1001];///Harta
  14. int p,m,n,k;
  15. int raspuns1,raspuns2;///Raspunsurile la cele doua intrebari
  16.  
  17. void citire()
  18. {
  19. ifstream f("camelot.in");
  20. f>>p>>m>>n>>k;
  21. f>>castel.x>>castel.y;
  22. for(int i=1; i<=k; i++)
  23. {
  24. f>>cavaleri[i].x>>cavaleri[i].y;
  25. }
  26. f.close();
  27. }
  28.  
  29. void rezolvare()
  30. {
  31. LOCATIE p,adresa;
  32. int i,x,y;
  33. Coada.push(castel);
  34. A[castel.x][castel.y]=1;
  35. while(!Coada.empty())
  36. {
  37. p=Coada.front();
  38. Coada.pop();
  39. for(i=0;i<8;++i)
  40. {
  41. x=p.x+dx[i];
  42. y=p.y+dy[i];
  43. if(x>=1&&x<=m&&y>=1&&y<=n)
  44. if(A[x][y]==0)
  45. {
  46. adresa.x=x;
  47. adresa.y=y;
  48. Coada.push(adresa);
  49. A[x][y]=1+A[p.x][p.y];
  50. }
  51. }
  52. }
  53. p=cavaleri[1];
  54. raspuns1=raspuns2=A[p.x][p.y];
  55. for(i=2;i<=k;++i)
  56. {
  57. if(raspuns1>A[cavaleri[i].x][cavaleri[i].y])
  58. raspuns1=A[cavaleri[i].x][cavaleri[i].y];
  59. if(raspuns2<A[cavaleri[i].x][cavaleri[i].y])
  60. raspuns2=A[cavaleri[i].x][cavaleri[i].y];
  61. }
  62. --raspuns1;
  63. --raspuns2;
  64. }
  65.  
  66. void afisare()
  67. {
  68. ofstream g("camelot.out");
  69. if(p==1)
  70. g<<raspuns1;
  71. else
  72. g<<raspuns2;
  73. g.close();
  74. }
  75.  
  76. int main()
  77. {
  78. citire();
  79. rezolvare();
  80. afisare();
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement