Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. const int N=8;
  6. int chess[N][N];
  7. int dx[]{2, 1, -1, -2, -2, -1, 1, 2};
  8. int dy[]{1, 2, 2, 1, -1, -2, -2, -1};
  9.  
  10. void printsol(){
  11.   for(int i=0;i<N;i++){
  12.     printf("\n");
  13.     for(int j=0;j<N;j++)
  14.       printf("%d  ", chess[i][j]);
  15.   }
  16. }
  17.  
  18. bool safe(int x, int y){
  19.   if(x<0 || x>N || y<0 || y>N)return false;
  20.   if(chess[x][y]!=-1)return false;
  21.  
  22.   return true;
  23. }
  24. bool go(int x, int y, int movei){
  25.  
  26.   if(movei==N*N)return true;
  27.  
  28.   for(int i=0;i<8;i++){
  29.     int xx=x+dx[i];
  30.     int yy=y+dy[i];
  31.  
  32.     if(safe(xx, yy)){
  33.       chess[xx][yy]=movei;
  34.       if(go(xx, yy, movei+1))return true;
  35.       chess[xx][yy]=-1;
  36.     }
  37.   }
  38.   return false;
  39. }
  40. void solve(){
  41.   memset(chess, -1, sizeof chess);
  42.   chess[0][0]=0;
  43.   if(go(0,0,0))printsol();
  44.   else printf("otario\n");
  45. }
  46. int main(){
  47.   solve();
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement