新数独 HYSBZ - 3109 dfs

原题
判断> < 后dfs 即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-10
const int N = 1505;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

#define dfsnxt(x,y) y==9?dfs(x+1,1):dfs(x,y+1)


int ans[30][30];
int usel[30][30];
int useh[30][30];
int useA[30][30];
int G[30][30];
int cmpl[30][30], cmpr[30][30];
bool fg;
int cmp(int x, int y) {
	return x > y ? 1 : -1;
}

bool check(int x, int y, int k) {
	if (useh[x][k] || usel[y][k])return 0;
	if (useA[((x - 1) / 3) * 3 + (y - 1) / 3 + 1][k])return 0;
	if (cmp(k, ans[x - 1][y]) != cmpr[x][y] && cmpr[x][y] != 0)return 0;
	if (cmp(k, ans[x][y - 1]) != cmpl[x][y] && cmpl[x][y] != 0)return 0;
	useh[x][k] = usel[y][k] = useA[((x - 1) / 3) * 3 + (y - 1) / 3 + 1][k] = 1;
	return 1;
}

void dfs(int x, int y) {
	if (x == 10) {
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= 9; j++) {
				cout << ans[i][j];
				if (j != 9)cout << " ";
			}
			cout << endl;
		}
		fg = 1; return;
	}
	if (fg)return;
	for (int i = 1; i <= 9; i++) {
		if (check(x, y, i)) {
			ans[x][y] = i;
			dfsnxt(x, y);
			ans[x][y] = 0;
			useA[((x - 1) / 3) * 3 + (y - 1) / 3 + 1][i] = usel[y][i] = useh[x][i] = 0;
		}
	}
}

int main()
{
	//ios::sync_with_stdio(false);
	char ch[2];
	for (int i = 1; i <= 9; i++) {
		for (int j = 1; j <= 9; j++) {
			if (j % 3 != 0) {
				scanf("%s", ch);
				if (ch[0] == '>')cmpl[i][j+1] = -1;
				else cmpl[i][j+1] = 1;
			}
		}
		if (i % 3 != 0) {
			for (int j = 1; j <= 9; j++) {
				scanf("%s", ch);
				if (ch[0] == 'v')cmpr[i + 1][j] = -1;
				else cmpr[i+1][j] = 1;
			}
		}
	}
	dfs(1, 1);

}

猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/82933718
今日推荐