Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int di[4]={0,1,-1,0};
  5. int dj[4]={1,0,0,-1};
  6. int a[101][101];
  7. int n,s,inc,i,j,banif;
  8. ifstream ci("taxe2.in");
  9. ofstream cou("taxe2.out");
  10. struct milcorupidemilinvinetesti
  11. {
  12. int x,y;
  13. };
  14. milcorupidemilinvinetesti q[50000];
  15. int ok(int i,int j)
  16. {
  17. if(i<1 or j<1 or i>n or j>n )
  18. return 0;
  19. return 1;
  20.  
  21. }
  22. void millee()
  23. {
  24. int ii,jj,sf=1;
  25. inc=0;
  26. while(inc<=sf)
  27. {
  28. i=q[++inc].x;
  29. j=q[inc].y;
  30. for(int k=0;k<4;k++)
  31. {
  32. ii=i+di[k];
  33. jj=j+dj[k];
  34.  
  35. if(ok(ii,jj))
  36. {
  37. if(a[i][j]-a[ii][jj]>a[ii][jj])
  38. {
  39. a[ii][jj]=a[i][j]-a[ii][jj];
  40. q[++sf].x=ii;
  41. q[sf].y=jj;
  42. }
  43. }
  44.  
  45. }
  46.  
  47. }
  48.  
  49. }
  50. int main()
  51. {
  52. ci>>s>>n;
  53. for(i=1;i<=n;i++)
  54. for(j=1;j<=n;j++)
  55. ci>>a[i][j];
  56. a[1][1]=s;
  57. q[1].x=1;
  58. q[1].y=1;
  59. banif=a[n][n];
  60. millee();
  61. if(a[n][n]==banif)
  62. cou<<-1;
  63. else
  64. cou<<a[n][n];
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement