Ad

Write a function that will solve a 9x9 Sudoku puzzle. The function will take one argument consisting of the 2D puzzle array, with the value 0 representing an unknown square.

The Sudokus tested against your function will be "easy" (i.e. determinable; there will be no need to assume and test possibilities on unknowns) and can be solved with a brute-force approach.

For Sudoku rules, see the Wikipedia article.


const int sudoku[9][9] = {
  {0,0,0,0,0,0,6,0,0},
  {0,0,0,0,0,7,0,0,1},
  {2,0,8,0,0,0,0,4,0},
  {6,0,0,0,0,0,0,0,0},
  {0,5,1,4,0,9,0,0,0},
  {0,0,0,5,0,0,0,2,0},
  {0,0,4,0,0,0,0,5,0},
  {0,0,7,1,9,6,0,0,0},
  {0,8,0,0,0,0,3,0,0},
};

#include <stddef.h>
typedef int (*T)[9];
T solve (const T sudoku) {
   /* Write your code here */
  return NULL;
}

Converts the string to Brainfuck.

#include <stdio.h>
#include <string.h>
// #define DEBUG

char s[4096] = "";
int s2b(int argc, char *argv[]) {
	register int i = 0,j;
	register int k,m;
	register int l,last = 0;
	if (argc != 2) {
		printf("Usage: %s [string]",*argv);
		return 1;
	}
	while (argv[1][++i]!=0)
		continue;
	for (j=0; j<i; j++) {
		register int t;
		t = argv[1][j];
		l = k = t-last;
		last = t;
#ifdef DEBUG
		printf("\n%d, %d, %d, %d\n",last,k,j,i);
#endif

		if (k >= 0) {
			k >>= 3; // k /= 8
			l &= 7; //在更多的机器工作,求余
			if (k == 1)
				strcat(s,">++++++++");
			else {
				if (k != 0) {
					strcat(s,"++++++++[>");
					for (m=0; m<k; m++)
						strcat(s,"+");
					strcat(s,"<-]>");
				} else {
					strcat(s,">");
				}
			}
			for (m=0; m<l; m++)
				strcat(s,"+");
			strcat(s,".");
		} else {
			k = -k >> 3; // k /= 8
			l = -l & 7; //在更多的机器工作,求余
			if (k == 1)
				strcat(s,">--------");
			else {
				strcat(s,"++++++++[>");
				for (m=0; m<k; m++)
					strcat(s,"-");
				strcat(s,"<-]>");
			}
			for (m=0; m<l; m++)
				strcat(s,"-");
			strcat(s,".");
		}
#ifdef DEBUG
		printf("\n%d, %d, %d\n",m,k,l);
#endif

		strcat(s,"<");
	}
  return 0;
}