Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int s[16][16],n,v[16];
- void afisare()
- {
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=n;j++)
- cout<<v[s[i][j]]<<' ';
- cout<<'\n';}
- cout<<'\n'<<'\n';
- }
- bool conditie(int i,int j)
- {
- if(i==1 && j==1) return 1;
- if(i==1 && j>1) {if(v[s[i][j]]%2==v[s[i][j-1]]%2) return 1; else return 0;}
- if(i>2 && j==1) {if(v[s[i-1][j]]%2==v[s[i][j]]%2) return 1; else return 0;}
- if(i>2 && j>2) {if(v[s[i][j]]%2==v[s[i-1][j]]%2) return 1; else if(v[s[i][j]]%2==v[s[i][j-1]]%2) return 1;}
- return 0;
- }
- void backtr(int i,int j)
- {
- int x;
- if(i>0 && i<=n && j>0 && j<=n)
- {
- for(x=1;x<=n*n;x++)
- {
- s[i][j]=x;
- if(conditie(i,j)) if(i==n && j==n) afisare();
- else if(j<n) backtr(i,j+1);
- else backtr(i+1,1);
- }
- }
- }
- int main()
- {
- cin>>n;
- for(int i=1;i<=n*n;i++)
- cin>>v[i];
- backtr(1,1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement