---
# System prepended metadata

title: Untitled

---

　#include <stdio.h>

static	int	grid[4][4] = {
	{1, 0, 0, 0},
	{0, 0, 0, 0},
	{0, 0, 0, 0},
	{0,	0, 0, 0}
};

void	answerDisplay(void)     //解答を出力
{
	int i, j;

	j = 0 ;
	while (j < 4)
	{
		i = 0;
		while (i < 4)
		{
			printf("%d ", grid[j][i]);
			i++;
		}
		printf("\n");
		j++;
	}
	printf("\n");
}

int	checkRow(int j, int num)   //行のチェック
{
	int x;

	x = 0;
	while (x < 4)
	{
		if (grid[j][x] == num)
		{
			return (0);
		}
		x++;
	}
	return (1);
}

int checkCol(int i, int num)    //列のチェック
{
	int y;

	y = 0;
	while (y < 4)
	{
		if (grid[y][i] == num)
		{
			return (0);
		}
		y++;
	}
	return (1);
}

int checkView(int i, int j, int num)
{
	if (grid[j][i]
		{
			
		}

	return (1);

int	checkNumber(int i, int j, int num)
{
	if (checkRow(j, num) && checkCol(i, num) && checkView(i, j, num))
	{
		return (1);
	}
	return (0);
}

int	putNumber(int i, int j, int num)       //数字を入力する
{
	int fix_flag = 0;                 //フラグ
	if (grid[j][i] != num) 
	{
		if (grid[j][i] != 0)
		{
			return (0);
		}
		if (!checkNumber(i, j, num))
		{
			return (0);
		}
		grid[j][i] = num;                 //座標に数字を入れる
	}
	else
	{
		fix_flag = 1;                   //フラグを立てる
	}
	if (i == 3 && j == 3)
	{
		answerDisplay();
	}
	else
	{
		int	n;
		int	next_i;
		int next_j;

		if (i + 1 >= 4)
		{
			next_i = 0;
			next_j = j + 1;
		}
		else
		{
			next_i = i + 1;
			next_j = j;
		}
		n = 1;
		while (n <= 4)
		{
			putNumber(next_i, next_j, n);    //再帰呼び出し
			n++;
		}
	}
	if (!fix_flag)
	{
		grid[j][i] = 0;
	}
	return (1);
}

int	main (void)
{
	int	n;

	n = 1;
	while (n <= 4)
	{
		putNumber(0, 0, n);
		n++;
	}
	return (0);
}