Advertisement
nicuvlad76

384

Oct 25th, 2020
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define N 105
  3. using namespace std;
  4. ifstream fin("saritura_calului1.in");
  5. ofstream fout("saritura_calului1.out");
  6. int n,a[N][N],m,x,y,i,j;
  7. int di[]={-2,-2,-1,-1,1,1,2,2};
  8. int dj[]={-1,1,-2,2,-2,2,-1,1};
  9. int Inside(int i, int j)
  10. {
  11.     return i>=1 && i<=n && j>=1&& j<=m;
  12. }
  13.  
  14. int  Access(int i,int j)
  15. {
  16.     int ct=0;
  17.     for(int k=0;k<8;k++)
  18.          if(Inside(i+di[k],j+dj[k])&& a[i+di[k]][j+dj[k]]==0)ct++;
  19.     return ct;
  20. }
  21. void Greedy(int x, int y)
  22. {
  23.     int poz=1, gasit=1;
  24.     a[x][y]=poz;
  25.     int in, jn, Min,ic=0,jc=0,fii;
  26.     while(poz<=n*m && gasit)
  27.     {
  28.         gasit=0;
  29.         Min=n*m;
  30.         for(int k=0; k<8; k++)
  31.         {
  32.             in=x+di[k];
  33.             jn=y+dj[k];
  34.             if(Inside(in,jn)&&a[in][jn]==0)
  35.             {
  36.                 gasit=1;
  37.                 fii=Access(in,jn);
  38.                 if(fii<Min)
  39.                 {
  40.                     Min=fii;
  41.                     ic=in;
  42.                     jc=jn;
  43.                 }
  44.             }
  45.         }
  46.         if(gasit==1)
  47.         {
  48.             x=ic;
  49.             y=jc;
  50.             a[x][y]=++poz;
  51.         }
  52.     }
  53.  
  54.  
  55. }
  56. void Afisare()
  57. {
  58.     for(i=1;i<=n;i++)
  59.     {
  60.         for(j=1;j<=m;j++)
  61.             fout<<a[i][j]<<' ';
  62.         fout<<'\n';
  63.     }
  64. }
  65. int main()
  66. {
  67.     fin>>n>>m>>x>>y;
  68.     Greedy(x,y);
  69.     Afisare();
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement