a53

Suprasolicitare

a53
May 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. using namespace std;
  4. ifstream fin("suprasolicitare.in");
  5. ofstream fout("suprasolicitare.out");
  6. const int N=105;
  7. bool a[N][N],r[N][N];
  8. int d[N][N],t[N][N],n,x,y;
  9. const int dx[]={-1,0,1,0},
  10. dy[]={0,1,0,-1};
  11. queue <pair <int, int> > Q;
  12.  
  13. void get_sol(int x,int y)
  14. {
  15. int tx=t[x][y]/n,ty=t[x][y]%n;
  16. if (t[x][y]!=-1)
  17. get_sol(tx,ty);
  18. else
  19. return;
  20. if (ty==y)
  21. {
  22. if(a[(x + 1)%n][y])
  23. fout<<"Jos\n";
  24. if(a[(x+ n-1)%n][y])
  25. fout<<"Sus\n";
  26. }
  27. else
  28. {
  29. if(a[x][(y+1)%n])
  30. fout<<"Dreapta\n";
  31. else
  32. if(a[x][(y+n-1)% n])
  33. fout<<"Stanga\n";
  34. }
  35. }
  36.  
  37. int main()
  38. {
  39. fin>>n;
  40. for(int i=0;i<n;++i)
  41. for(int j=0;j<n;++j)
  42. fin>>a[i][j];
  43. fin>>x>>y;
  44. d[--x][--y]=1;
  45. t[x][y]=-1;
  46. Q.push(make_pair(x,y));
  47. for(int i=0;i<n;++i)
  48. {
  49. bool ok=1;
  50. for(int j=0;ok&&j<n;++j)
  51. ok&=!a[i][j];
  52. if (ok)
  53. for(int j=0;j<n;++j)
  54. r[i][j]=1;
  55. }
  56. for (int j=0;j<n;++j)
  57. {
  58. bool ok=1;
  59. for(int i=0;i<n;++i)
  60. ok&=!a[i][j];
  61. if (ok)
  62. for(int i=0;i<n;++i)
  63. r[i][j]=1;
  64. }
  65. while(Q.size()&&!r[Q.front().first][Q.front().second])
  66. {
  67. int x =Q.front().first,y=Q.front().second;
  68. Q.pop();
  69. for(int k=0;k<4;++k)
  70. {
  71. int xx=x,yy=y;
  72. while(!a[(xx+n+dx[k])%n][(yy+n+dy[k])%n])
  73. {
  74. xx=(xx+n+dx[k])%n,yy=(yy+n+dy[k])%n;
  75. }
  76. if(!d[xx][yy])
  77. {
  78. d[xx][yy]=d[x][y]+1;
  79. t[xx][yy]=x*n+y;
  80. Q.push(make_pair(xx,yy));
  81. }
  82. }
  83. }
  84. if(!Q.size())
  85. {
  86. fout<<-1;
  87. return 0;
  88. }
  89. int sol_x=Q.front().first,sol_y=Q.front().second;
  90. fout<<d[sol_x][sol_y]<<'\n';
  91. get_sol(sol_x,sol_y);
  92. if(d[sol_x][sol_y]==1)
  93. {
  94. fout<<"Dreapta";
  95. return 0;
  96. }
  97. if(a[(sol_x+1)%n][sol_y]||a[(sol_x+n-1)%n][sol_y])
  98. fout<<"Dreapta";
  99. else
  100. if(a[sol_x][(sol_y+1)%n]||a[sol_x][(sol_y+n-1)%n])
  101. fout<<"Jos";
  102. }
Add Comment
Please, Sign In to add comment