(藍橋杯)填數方案數

填數方案數

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 33   Accepted Submission(s) : 19

Font: Times New Roman | Verdana | Georgia

Font Size:  

Problem Description

如下的10個格子
(圖形見QQ羣)
填入0~9的數字。要求:連續的兩個數字不能相鄰。
(左右、上下、對角都算相鄰)
一共有多少種可能的填數方案?
請填寫表示方案數目的整數。
注意:你提交的應該是一個整數,不要填寫任何多餘的內容或說明性文字。

Input

Output

輸出一個整數

Author

njtechliubin

不知道爲什麼我的答案是8400,錯誤代碼我列一哈。

#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
int map[6][6];
int vis[10];
int ans = 0;
void dfs(int x0,int y0) {
	if (x0==3&&y0==3) {
		ans++;
		return;
	}
	for (int i = 0; i < 10; i++) {
		int a1 = map[x0 - 1][y0];
		int a2 = map[x0][y0 - 1];
		int a3 = map[x0 - 1][y0 - 1];
		int a4 = map[x0 + 1][y0 - 1];
		int a5 = map[x0 + 1][y0 + 1];
		int a6 = map[x0 - 1][y0 + 1];
		int a7 = map[x0 + 1][y0];
		int a8 = map[x0][y0+1];
		if (abs(a1 - i) <= 1)
			continue;
		if (abs(a2 - i) <= 1 || abs(a8 - i) <= 1 || abs(a7 - i) < 2)
			continue;
		if (abs(a3 - i) <= 1||abs(a4-i)<2||abs(a5 - i) <= 1 || abs(a6 - i)<2)
			continue;
		if (!vis[i]) {
			vis[i] = 1;
			map[x0][y0] = i;
			if (x0 == 4)
				dfs(1, y0 + 1);
			else dfs(x0+1 , y0);
			map[x0][y0] = -2;
			vis[i] = 0;
		}
	}
}
int main()
{
	memset(map, -2, sizeof(map));
	dfs(2, 1);
	cout << ans;
	//cout << 321723;
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章