Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N=8;
- int chess[N][N];
- int dx[]{2, 1, -1, -2, -2, -1, 1, 2};
- int dy[]{1, 2, 2, 1, -1, -2, -2, -1};
- void printsol(){
- for(int i=0;i<N;i++){
- printf("\n");
- for(int j=0;j<N;j++)
- printf("%d ", chess[i][j]);
- }
- }
- bool safe(int x, int y){
- if(x<0 || x>N || y<0 || y>N)return false;
- if(chess[x][y]!=-1)return false;
- return true;
- }
- bool go(int x, int y, int movei){
- if(movei==N*N)return true;
- for(int i=0;i<8;i++){
- int xx=x+dx[i];
- int yy=y+dy[i];
- if(safe(xx, yy)){
- chess[xx][yy]=movei;
- if(go(xx, yy, movei+1))return true;
- chess[xx][yy]=-1;
- }
- }
- return false;
- }
- void solve(){
- memset(chess, -1, sizeof chess);
- chess[0][0]=0;
- if(go(0,0,0))printsol();
- else printf("otario\n");
- }
- int main(){
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement