[ create a new paste ] login | about

Link: http://codepad.org/RJ3J8hjW    [ raw code | output | fork ]

C, pasted on Sep 30:
#include<stdio.h>

short int masu[100]={},fin;

void next(int y,int x);

int main(){
	int x, y, i;
	
	for(y=0;y<9;y++) //読み込み
		for(x=0;x<9;x++)
			scanf(" %d",&masu[y*10+x]);

	x=0,y=0;
	next(y,x);

	for(y=0;y<9;y++){
		printf("\n");
		for(x=0;x<9;x++)
			printf("%d ",masu[y*10+x]);
	}

	return 0;
}

void next(int y,int x){
	int num=y*10+x, i, m, n, check;

	if(num==90)
		fin = 1;
	
	else if(masu[num]){
		if(x==8)
			next(y+1,0);
		else if(x!=8)next(y,x+1);
	}

	else if(!masu[num])
		for(i=1;i<10;i++){
			check=0;
			if(fin)
				break;

			for(m=0;m<9;m++)
				if(masu[y*10+m]==i)
					check=1;
				else if(masu[m*10+x]==i)
					check=1;

			for(m=0;m<3;m++){
				for(n=0;n<3;n++)
					if(num==m*10+n)
						for(m=0;m<3;m++)
							for(n=0;n<3;n++)
								if(masu[m*10+n]==i)
									check=1;
			}
			for(m=0;m<3;m++){
				for(n=3;n<6;n++)
					if(num==m*10+n)
						for(m=0;m<3;m++)
							for(n=3;n<6;n++)
								if(masu[m*10+n]==i)
									check=1;
			}
			for(m=0;m<3;m++){
				for(n=6;n<9;n++)
					if(num==m*10+n)
						for(m=0;m<3;m++)
							for(n=6;n<9;n++)
								if(masu[m*10+n]==i)
									check=1;
			}
			for(m=3;m<6;m++){
				for(n=0;n<3;n++)
					if(num==m*10+n)
						for(m=3;m<6;m++)
							for(n=0;n<3;n++)
								if(masu[m*10+n]==i)
									check=1;
			}
			for(m=3;m<6;m++){
				for(n=3;n<6;n++)
					if(num==m*10+n)
						for(m=3;m<6;m++)
							for(n=3;n<6;n++)
								if(masu[m*10+n]==i)
									check=1;
			}
			for(m=3;m<6;m++){
				for(n=6;n<9;n++)
					if(num==m*10+n)
						for(m=3;m<6;m++)
							for(n=6;n<9;n++)
								if(masu[m*10+n]==i)
									check=1;
			}
			for(m=6;m<9;m++){
				for(n=0;n<3;n++)
					if(num==m*10+n)
						for(m=6;m<9;m++)
							for(n=0;n<3;n++)
								if(masu[m*10+n]==i)
									check=1;
			}
			for(m=6;m<9;m++){
				for(n=3;n<6;n++)
					if(num==m*10+n)
						for(m=6;m<9;m++)
							for(n=3;n<6;n++)
								if(masu[m*10+n]==i)
									check=1;
			}
			for(m=6;m<9;m++){
				for(n=6;n<9;n++)
					if(num==m*10+n)
						for(m=6;m<9;m++)
							for(n=6;n<9;n++)
								if(masu[m*10+n]==i)
									check=1;
			}

			if(!check){
				masu[num]=i;
				if(x==8)
					next(y+1,0);
				else if(x!=8)next(y,x+1);
			}
		}
}


Output:
1
2
3
4
5
6
7
8
9
10

1 2 3 4 5 6 7 8 9 
4 5 6 7 8 9 3 2 1 
7 8 9 1 2 3 4 5 6 
2 1 4 3 6 5 8 9 7 
3 6 7 2 9 8 1 4 5 
5 9 8 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 


Create a new paste based on this one


Comments: