Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ID: teh_min1
- PROG: checker
- LANG: C
- Checker Challenge
- Done by Teh Ming Han
- */
- /*
- Algorithm: Recursion
- Notes:
- If n is even, we can compute half a board and then flip along the x axis to generate the other half of the board.
- this means we only need to generate half the board if n is even.
- If n is odd, we compute half a board and then (half + 1 rows) of the board.
- Then we sum then up to get the answer.
- */
- #include <stdio.h>
- #include <string.h>
- //#include <iostream.h>
- int ans[3][14] = {0}, ans_c = 0;
- int board[14] = {0}, n, size;
- int recur(int r){
- if (r==size){ //jackpot
- if (ans_c<3) memcpy(ans[ans_c],board,sizeof(int)*size);
- ans_c++;
- return 0;
- }
- int a;
- char cans[28] = {'#'};
- for (a=1;a<r;a++){
- cans[board[a]] = '@';
- cans[board[a]-(r-a)] = '@';
- cans[board[a]+(r-a)] = '@';
- }
- for (a=1;a<=n;a++) //start checking if position is suitable
- if (cans[a]!='@'){
- board[r] = a;
- recur(r+1);
- }
- return 0;
- }
- int main(){
- int i,a;
- FILE * in = fopen("checker.in","r");
- fscanf(in,"%d",&n);
- fclose(in);
- size = n + 1;
- int ans_temp,limit;
- if (n==6) limit = n;
- else limit = (n+1)/2;
- for (i=1;i<=limit;i++){
- ans_temp = ans_c;
- board[1] = i;
- recur(2);
- }
- if (n%2==0 && n>6) ans_c *= 2;
- else if (n%2==1 && n>6) ans_c = ans_c + ans_temp;
- FILE *out = fopen ("checker.out", "w");
- for (i=0;i<3;i++){
- for (a=1;a<n;a++)
- fprintf(out,"%d ",ans[i][a]);
- fprintf(out,"%d\n",ans[i][n]); //solve the spacing prob
- }
- fprintf(out,"%d\n",ans_c);
- printf("%d\n",ans_c);
- fclose(out);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement