Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define N 105
- using namespace std;
- ifstream fin("saritura_calului1.in");
- ofstream fout("saritura_calului1.out");
- int n,a[N][N],m,x,y,i,j;
- int di[]={-2,-2,-1,-1,1,1,2,2};
- int dj[]={-1,1,-2,2,-2,2,-1,1};
- int Inside(int i, int j)
- {
- return i>=1 && i<=n && j>=1&& j<=m;
- }
- int Access(int i,int j)
- {
- int ct=0;
- for(int k=0;k<8;k++)
- if(Inside(i+di[k],j+dj[k])&& a[i+di[k]][j+dj[k]]==0)ct++;
- return ct;
- }
- void Greedy(int x, int y)
- {
- int poz=1, gasit=1;
- a[x][y]=poz;
- int in, jn, Min,ic=0,jc=0,fii;
- while(poz<=n*m && gasit)
- {
- gasit=0;
- Min=n*m;
- for(int k=0; k<8; k++)
- {
- in=x+di[k];
- jn=y+dj[k];
- if(Inside(in,jn)&&a[in][jn]==0)
- {
- gasit=1;
- fii=Access(in,jn);
- if(fii<Min)
- {
- Min=fii;
- ic=in;
- jc=jn;
- }
- }
- }
- if(gasit==1)
- {
- x=ic;
- y=jc;
- a[x][y]=++poz;
- }
- }
- }
- void Afisare()
- {
- for(i=1;i<=n;i++)
- {
- for(j=1;j<=m;j++)
- fout<<a[i][j]<<' ';
- fout<<'\n';
- }
- }
- int main()
- {
- fin>>n>>m>>x>>y;
- Greedy(x,y);
- Afisare();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement