0分求助
查看原帖
0分求助
972183
MESSI_楼主2024/3/31 19:05
#include<bits/stdc++.h>
using namespace std;
const int r = 3, c = 4;
int mapp[10][10];
int numv[15];
int cou;
int dir[4][2] = { 0, -1, -1, -1, -1, 0, -1, 1 };
bool check(int x, int y, int n) {
	for (int i = 0; i < 4; i++) {
		int nx = x + dir[i][0];
		int ny = y + dir[i][1];
		if (nx >= 0 && nx < r && ny >= 0 && ny < c) {
			if (mapp[nx][ny] == n - 1 || mapp[nx][ny] == n + 1) {
				return false;
			}
		}
	}
	return true;
}
void dfs(int dep, int pos) {
	if (dep == 2 && pos == 3) {
		cou++;
#if 0
		for (int i = 0; i < r; i++) {
			for (int j = 0; j < c; j++) {
				printf("%4d", mapp[i][j]);
			}
			puts("");
		}
		puts("");
		system("pause");
#endif
		return;
	}

	if (pos >= c) {
		dfs(dep + 1, 0);
	}
	else {
		for (int i = 0; i <= 9; i++) {
			if (!numv[i] && check(dep, pos, i)) {
				numv[i] = true;
				mapp[dep][pos] = i;
				dfs(dep, pos + 1);
				mapp[dep][pos] = -10;
				numv[i] = false;
			}
		}
	}
}

int main()
{
	for (int i = 0; i <= 5; i++) {
		for (int j = 0; j <= 5; j++) {
			mapp[i][j] = -10;
		}
	}
	memset(numv, false, sizeof(numv));
	cou = 0;
	dfs(0, 1);
	cout << cou << endl; 
	system("pause");
    return 0;
}
2024/3/31 19:05
加载中...