Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <cmath>
- using namespace std;
- ifstream f("date.in");
- ofstream g("date.out");
- int n,a[500][500]={0},aux[500],dl[]={-2,-1,1,2,2,1,-1,-2},dc[]={1,2,2,1,-1,-2,-2,-1};
- struct elem
- {int l,c;};
- elem sol[500];
- void citire()
- {
- cin >> n >> sol[1].l >> sol[1].c;
- }
- void init(int k)
- {
- aux[k]=-1;
- }
- int exista(int k)
- {
- if(aux[k]>7)
- return 0;
- return 1;
- }
- int cont(int k)
- {
- int i;
- for(i=1;i<=k-1;i++)
- if(sol[i].l==sol[k].l&&sol[i].c==sol[k].c)
- return 0;
- if(sol[k].l<1||sol[k].l>n||sol[k].c<1||sol[k].c>n)
- return 0;
- if(abs(sol[k].l-sol[k-1].l)+abs(sol[k].c-sol[k-1].c)!=3)
- return 0;
- return 1;
- }
- int solutie(int k)
- {
- if(k==n*n)
- return 1;
- return 0;
- }
- int tipar(int k)
- {
- int a[10][10]={0},i,j;
- for(i=1;i<=k;i++)
- a[sol[i].l][sol[i].c] = i;
- for(i=1;i<=n;i++)
- {for(j=1;j<=n;j++)
- g << a[i][j] << ' ';
- g << '\n';
- }
- g << '\n';
- }
- void valpos(int k)
- {
- sol[k].l=sol[k-1].l+dl[aux[k]];
- sol[k].c=sol[k-1].c+dc[aux[k]];
- }
- void bktg()
- { int k;
- k=2;
- init(k);
- while(k>1)
- if(exista(k))
- {
- aux[k]=aux[k]+1;
- valpos(k);
- if(cont(k)) if(solutie(k)) tipar(k);
- else {k++;init(k);}
- }
- else k--;
- }
- int main()
- {int i,j;
- citire();
- bktg();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement