fork download
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. using namespace std;
  5.  
  6. int n,cols,empty,board[10][10],sum=0,hi;
  7.  
  8. void set(int i,int j,int count){
  9. if(i<0 || i>=10 || j<0 || j>=10 || board[i][j] == 0)
  10. return;
  11. int ans = 0;
  12. board[i][j] = 0;//unsets the (i,j) cell
  13. hi = max(hi,count+1);//hi stores the maximum of value of visited squares
  14. //try all 8 possible moves for knight
  15. set(i-1,j-2,count+1);
  16. set(i-2,j-1,count+1);
  17. set(i+1,j-2,count+1);
  18. set(i+2,j-1,count+1);
  19. set(i-1,j+2,count+1);
  20. set(i-2,j+1,count+1);
  21. set(i+1,j+2,count+1);
  22. set(i+2,j+1,count+1);
  23. board[i][j] = 1;//sets (i,j) cell again(backtracking)
  24. }
  25. int main(){
  26. // freopen("input.txt","r",stdin);
  27.  
  28. cin>>n;
  29. sum = 0;
  30.  
  31. for(int i=0;i<n;i++){
  32. for(int j=0;j<n;j++){
  33. cin>>board[i][j];
  34. if(board[i][j])
  35. sum++; // sum stroes total number of valid cells on chessboard
  36. }
  37. }
  38. hi=0;
  39. set(0,0,0);
  40. cout<<sum-hi<<"\n";
  41. return 0;
  42. }
Success #stdin #stdout 0s 4496KB
stdin
Standard input is empty
stdout
0