求助,输出特别奇怪
  • 板块P1784 数独
  • 楼主mediocre_
  • 当前回复1
  • 已保存回复1
  • 发布时间2022/9/3 08:50
  • 上次更新2023/10/27 12:45:41
查看原帖
求助,输出特别奇怪
565707
mediocre_楼主2022/9/3 08:50
#include<bits/stdc++.h>
using namespace std;
const int N = 15;
int a[N][N];
void dfs(int x, int y) {
	printf("--------------------\n");
	for (int i = 1;i <= 9;++i){
	    for (int j = 1;j <= 9;++j)
	        printf("%d ",a[i][j]);
	    printf("\n");
	}
	if (a[x][y] != 0) {
		if (x == 9 && y == 9)
			return;
		else if (y == 9)
			dfs(x + 1, 1);
		else
			dfs(x, y + 1);
	} else {
		int h[10], l[10], f[10];
		int hh = 0;
		for (int j = 1; j <= 9; ++j) {
			if (j != y) {
				++hh;
				h[hh] = a[x][j];
			}
		}
		int ll = 0;
		for (int j = 1; j <= 9; ++j) {
			if (j != x) {
				++ll;
				l[ll] = a[j][y];
			}
		}
		int flag = 0;
		for (int j = (x - 1) / 3 + 1; j <= (x - 1) / 3 + 3; ++j) {
			for (int k = (y - 1) / 3 + 1; k <= (y - 1) / 3 + 3; ++k) {
				if (j != x || k != y) {
					flag++;
					f[flag] = a[j][k];
				}
			}
		}
		for (int i = 1; i <= 9; ++i) {
			bool nope = false;
			for (int j = 1; j <= 8; ++j)
				if (i == h[j] || i == l[j] || i == f[j])
					nope = true;
			if (nope == false) {
				a[x][y] = i;
				if (y == 9)
					dfs(x + 1, 1);
				else
					dfs(x, y + 1);
				a[x][y] = 0;
			}
		}
	}
}
int main() {
	for (int i = 1; i <= 9; ++i)
		for (int j = 1; j <= 9; ++j)
			scanf("%d", &a[i][j]);
	dfs(1, 1);
	for (int i = 1; i <= 9; ++i) {
		for (int j = 1; j <= 9; ++j)
			printf("%d ", a[i][j]);
		printf("\n");
	}
	return 0;
}
2022/9/3 08:50
加载中...