Skip to main content
fixed link
Source Link
hargasinski
  • 451
  • 7
  • 14

C++, 672*06720.80 6450.80 = 537.6516 bytes

#include <iostream> int **b,**x,**y,*d,*a,n; #define R(i) for(i=0;i<n;i++){ #define E(e) e=new int[n]; int f(int c,int r) {int i=0,p=c-1;if(r>=n)return 1;if(c == n + 1)return f(1,r+1);if(x[r][p])return f(c+1,r);R(i)int m=r==i,s=r+i==n-1;if(!b[r][i]&&!x[r][p]&&!(m&&d[p])&&!(s&&a[p])&&!y[i][p]){b[r][i]=c;x[r][p]=1;y[i][p]=1;if(m)d[p]=1;if(s)a[p]=1;if(f(c+1,r))return 1;b[r][i]=0;x[r][p]=0;y[i][p]=0;if(m)d[p]=0;if(s)a[p]=0;}}return 0;} int main(){std::cin>>n;int i=0,j=0;b=new int*[n];x=new int*[n];y=new int*[n];E(d);E(a);R(i)E(b[i]);E(x[i]);E(y[i]); d[i]=0;a[i]=0;R(j)b[i][j]=0;x[i][j]=0;y[i][j]=0;}}if(f(1,0)){R(i)R(j)std::cout<<b[i][j];}std::cout<<std::endl;}}} 

Try it online here

#include <iostream> // global variables to save bytes on passing these are function arguments int **b, // this will store the state of the board   **x, // if x[i][j] is true, row i of b contains the number j   **y, // if y[i][j] is true, column i of b contains the number j   *d, // if d[i] the main diagonal of b contains i   *a, // if a[i] the antidiagonal of a contains i   n; // preprocessor to save bytes on repeated statements #define R(i) for(i=0;i<n;i++){ #define E(e) e=new int[n]; // Recursively looks for a solution // c - the current number to insert in row r // r - the current row to fill int f (int c, int r) { int i=0,p=c-1; if (r >= n) return 1;  // we are done if (c == n + 1) return f(1,r+1); // this row is full, move to the next row if (x[r][p]) return f(c+1,r); // this row already contains this number, // move onto the next number R(i)  // go through the positions in this row, // trying to fill them with c int m=r==i, s=r+i==n-1;  // check if this position (r,i) is on ones // of the diagonals // if this spot isn't filled, and the row (r), column (i) and diagonals // (if it's on the diagonal) doesn't contain the number, fill the spot if (!b[r][i] && !x[r][p] && !(m&&d[p]) && !(s&&a[p]) && !y[i][p]) { // fill the spot, and indicate that this row, column and diagonals // contain this number, c b[r][i]=c; x[r][p]=1; y[i][p]=1; if (m) d[p]=1; if (s)a[p]=1; // move onto to the next number, if you find a solution, stop if (f(c+1,r)) return 1; // with this number in this spot, a solution is impossible, clear // its, and clear the checks b[r][i]=0; x[r][p]=0; y[i][p]=0; if (m) d[p]=0; if (s) a[p]=0; } } return 0; // a solution wasn't found } int main() { std::cin >> n;  // get n from STDIN // initialization int i=0,j=0; b=new int*[n]; x=new int*[n]; y=new int*[n]; E(d); E(a); R(i) E(b[i]); E(x[i]); E(y[i]); // initialization the inner arrays of b, x, y d[i]=0; a[i]=0; R(j) b[i][j]=0; x[i][j]=0; y[i][j]=0; // ensure each point is initialized as 0 } } // find a solution starting at the top-left corner and print it if it finds one if (f(1,0)) { R(i) R(j) std::cout<<b[i][j]; } std::cout<<std::endl; } } } 

C++, 672*0.80 = 537.6 bytes

#include <iostream> int **b,**x,**y,*d,*a,n; #define R(i) for(i=0;i<n;i++){ #define E(e) e=new int[n]; int f(int c,int r) {int i=0,p=c-1;if(r>=n)return 1;if(c == n + 1)return f(1,r+1);if(x[r][p])return f(c+1,r);R(i)int m=r==i,s=r+i==n-1;if(!b[r][i]&&!x[r][p]&&!(m&&d[p])&&!(s&&a[p])&&!y[i][p]){b[r][i]=c;x[r][p]=1;y[i][p]=1;if(m)d[p]=1;if(s)a[p]=1;if(f(c+1,r))return 1;b[r][i]=0;x[r][p]=0;y[i][p]=0;if(m)d[p]=0;if(s)a[p]=0;}}return 0;} int main(){std::cin>>n;int i=0,j=0;b=new int*[n];x=new int*[n];y=new int*[n];E(d);E(a);R(i)E(b[i]);E(x[i]);E(y[i]); d[i]=0;a[i]=0;R(j)b[i][j]=0;x[i][j]=0;y[i][j]=0;}}if(f(1,0)){R(i)R(j)std::cout<<b[i][j];}std::cout<<std::endl;}}} 
#include <iostream> // global variables to save bytes on passing these are function arguments int **b, // this will store the state of the board   **x, // if x[i][j] is true, row i of b contains the number j   **y, // if y[i][j] is true, column i of b contains the number j   *d, // if d[i] the main diagonal of b contains i   *a, // if a[i] the antidiagonal of a contains i   n; // preprocessor to save bytes on repeated statements #define R(i) for(i=0;i<n;i++){ #define E(e) e=new int[n]; // Recursively looks for a solution // c - the current number to insert in row r // r - the current row to fill int f (int c, int r) { int i=0,p=c-1; if (r >= n) return 1;  // we are done if (c == n + 1) return f(1,r+1); // this row is full, move to the next row if (x[r][p]) return f(c+1,r); // this row already contains this number, // move onto the next number R(i)  // go through the positions in this row, // trying to fill them with c int m=r==i, s=r+i==n-1;  // check if this position (r,i) is on ones // of the diagonals // if this spot isn't filled, and the row (r), column (i) and diagonals // (if it's on the diagonal) doesn't contain the number, fill the spot if (!b[r][i] && !x[r][p] && !(m&&d[p]) && !(s&&a[p]) && !y[i][p]) { // fill the spot, and indicate that this row, column and diagonals // contain this number, c b[r][i]=c; x[r][p]=1; y[i][p]=1; if (m) d[p]=1; if (s)a[p]=1; // move onto to the next number, if you find a solution, stop if (f(c+1,r)) return 1; // with this number in this spot, a solution is impossible, clear // its, and clear the checks b[r][i]=0; x[r][p]=0; y[i][p]=0; if (m) d[p]=0; if (s) a[p]=0; } } return 0; // a solution wasn't found } int main() { std::cin >> n;  // get n from STDIN // initialization int i=0,j=0; b=new int*[n]; x=new int*[n]; y=new int*[n]; E(d); E(a); R(i) E(b[i]); E(x[i]); E(y[i]); // initialization the inner arrays of b, x, y d[i]=0; a[i]=0; R(j) b[i][j]=0; x[i][j]=0; y[i][j]=0; // ensure each point is initialized as 0 } } // find a solution starting at the top-left corner and print it if it finds one if (f(1,0)) { R(i) R(j) std::cout<<b[i][j]; } std::cout<<std::endl; } } } 

C++, 6720.80 6450.80 = 516 bytes

#include <iostream> int **b,**x,**y,*d,*a,n; #define R(i) for(i=0;i<n;i++){ #define E(e) e=new int[n]; int f(int c,int r) {int i=0,p=c-1;if(r>=n)return 1;if(c == n + 1)return f(1,r+1);R(i)int m=r==i,s=r+i==n-1;if(!b[r][i]&&!x[r][p]&&!(m&&d[p])&&!(s&&a[p])&&!y[i][p]){b[r][i]=c;x[r][p]=1;y[i][p]=1;if(m)d[p]=1;if(s)a[p]=1;if(f(c+1,r))return 1;b[r][i]=0;x[r][p]=0;y[i][p]=0;if(m)d[p]=0;if(s)a[p]=0;}}return 0;} int main(){std::cin>>n;int i=0,j=0;b=new int*[n];x=new int*[n];y=new int*[n];E(d);E(a);R(i)E(b[i]);E(x[i]);E(y[i]); d[i]=0;a[i]=0;R(j)b[i][j]=0;x[i][j]=0;y[i][j]=0;}}if(f(1,0)){R(i)R(j)std::cout<<b[i][j];}std::cout<<std::endl;}}} 

Try it online here

#include <iostream> // global variables to save bytes on passing these are function arguments int **b, // this will store the state of the board **x, // if x[i][j] is true, row i of b contains the number j **y, // if y[i][j] is true, column i of b contains the number j *d, // if d[i] the main diagonal of b contains i *a, // if a[i] the antidiagonal of a contains i n; // preprocessor to save bytes on repeated statements #define R(i) for(i=0;i<n;i++){ #define E(e) e=new int[n]; // Recursively looks for a solution // c - the current number to insert in row r // r - the current row to fill int f (int c, int r) { int i=0,p=c-1; if (r >= n) return 1; // we are done if (c == n + 1) return f(1,r+1); // this row is full, move to the next row R(i) // go through the positions in this row, // trying to fill them with c int m=r==i, s=r+i==n-1; // check if this position (r,i) is on ones // of the diagonals // if this spot isn't filled, and the row (r), column (i) and diagonals // (if it's on the diagonal) doesn't contain the number, fill the spot if (!b[r][i] && !x[r][p] && !(m&&d[p]) && !(s&&a[p]) && !y[i][p]) { // fill the spot, and indicate that this row, column and diagonals // contain this number, c b[r][i]=c; x[r][p]=1; y[i][p]=1; if (m) d[p]=1; if (s)a[p]=1; // move onto to the next number, if you find a solution, stop if (f(c+1,r)) return 1; // with this number in this spot, a solution is impossible, clear // its, and clear the checks b[r][i]=0; x[r][p]=0; y[i][p]=0; if (m) d[p]=0; if (s) a[p]=0; } } return 0; // a solution wasn't found } int main() { std::cin >> n; // get n from STDIN // initialization int i=0,j=0; b=new int*[n]; x=new int*[n]; y=new int*[n]; E(d); E(a); R(i) E(b[i]); E(x[i]); E(y[i]); // initialization the inner arrays of b, x, y d[i]=0; a[i]=0; R(j) b[i][j]=0; x[i][j]=0; y[i][j]=0; // ensure each point is initialized as 0 } } // find a solution starting at the top-left corner and print it if it finds one if (f(1,0)) { R(i) R(j) std::cout<<b[i][j]; } std::cout<<std::endl; } } } 
Source Link
hargasinski
  • 451
  • 7
  • 14

C++, 672*0.80 = 537.6 bytes

#include <iostream> int **b,**x,**y,*d,*a,n; #define R(i) for(i=0;i<n;i++){ #define E(e) e=new int[n]; int f(int c,int r) {int i=0,p=c-1;if(r>=n)return 1;if(c == n + 1)return f(1,r+1);if(x[r][p])return f(c+1,r);R(i)int m=r==i,s=r+i==n-1;if(!b[r][i]&&!x[r][p]&&!(m&&d[p])&&!(s&&a[p])&&!y[i][p]){b[r][i]=c;x[r][p]=1;y[i][p]=1;if(m)d[p]=1;if(s)a[p]=1;if(f(c+1,r))return 1;b[r][i]=0;x[r][p]=0;y[i][p]=0;if(m)d[p]=0;if(s)a[p]=0;}}return 0;} int main(){std::cin>>n;int i=0,j=0;b=new int*[n];x=new int*[n];y=new int*[n];E(d);E(a);R(i)E(b[i]);E(x[i]);E(y[i]); d[i]=0;a[i]=0;R(j)b[i][j]=0;x[i][j]=0;y[i][j]=0;}}if(f(1,0)){R(i)R(j)std::cout<<b[i][j];}std::cout<<std::endl;}}} 

Since a couple answers have already been posted, I thought I would post the golfed version of the code I used to generated the output for the examples. This is my first time answering a , so all feedback is welcomed. :)

Ungolfed + explanations:

Essentially, the code is brute-forcing a solution. It starts in the first row, with 0. It starts in the first spot, if that spots passes all the checks, it moves onto to the next number. If it fills the row, it moves onto the next row. If it's done all of the rows, that means a solution has been found. If the spot doesn't pass all of the checks, it moves onto the next spot. If it's done the row, then it backtracks, as a number in one of the previous rows prevents a solution from being possible.

#include <iostream> // global variables to save bytes on passing these are function arguments int **b, // this will store the state of the board **x, // if x[i][j] is true, row i of b contains the number j **y, // if y[i][j] is true, column i of b contains the number j *d, // if d[i] the main diagonal of b contains i *a, // if a[i] the antidiagonal of a contains i n; // preprocessor to save bytes on repeated statements #define R(i) for(i=0;i<n;i++){ #define E(e) e=new int[n]; // Recursively looks for a solution // c - the current number to insert in row r // r - the current row to fill int f (int c, int r) { int i=0,p=c-1; if (r >= n) return 1; // we are done if (c == n + 1) return f(1,r+1); // this row is full, move to the next row if (x[r][p]) return f(c+1,r); // this row already contains this number, // move onto the next number R(i) // go through the positions in this row, // trying to fill them with c int m=r==i, s=r+i==n-1; // check if this position (r,i) is on ones // of the diagonals // if this spot isn't filled, and the row (r), column (i) and diagonals // (if it's on the diagonal) doesn't contain the number, fill the spot if (!b[r][i] && !x[r][p] && !(m&&d[p]) && !(s&&a[p]) && !y[i][p]) { // fill the spot, and indicate that this row, column and diagonals // contain this number, c b[r][i]=c; x[r][p]=1; y[i][p]=1; if (m) d[p]=1; if (s)a[p]=1; // move onto to the next number, if you find a solution, stop if (f(c+1,r)) return 1; // with this number in this spot, a solution is impossible, clear // its, and clear the checks b[r][i]=0; x[r][p]=0; y[i][p]=0; if (m) d[p]=0; if (s) a[p]=0; } } return 0; // a solution wasn't found } int main() { std::cin >> n; // get n from STDIN // initialization int i=0,j=0; b=new int*[n]; x=new int*[n]; y=new int*[n]; E(d); E(a); R(i) E(b[i]); E(x[i]); E(y[i]); // initialization the inner arrays of b, x, y d[i]=0; a[i]=0; R(j) b[i][j]=0; x[i][j]=0; y[i][j]=0; // ensure each point is initialized as 0 } } // find a solution starting at the top-left corner and print it if it finds one if (f(1,0)) { R(i) R(j) std::cout<<b[i][j]; } std::cout<<std::endl; } } }