藍橋杯 剪郵票

傳送門

#include <iostream>
#include <algorithm>
using namespace std;
int a[12] = { 0,0,0,0,0,0,0,1,1,1,1,1 };//枚舉剪的五張郵票
int ans = 0;
void dfs(int g[3][4], int i, int j) {
	g[i][j] = 0;
	if (i - 1 >= 0 && g[i - 1][j] == 1)dfs(g, i - 1, j);
	if (i + 1 <= 2 && g[i + 1][j] == 1)dfs(g, i + 1, j);
	if (j - 1 >= 0 && g[i][j - 1] == 1)dfs(g, i, j - 1);
	if (j + 1 <= 3 && g[i][j + 1] == 1)dfs(g, i, j + 1);
}//dfs連通性檢查
bool check() {
	int g[3][4] = { 0 };
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 4; j++)
			if (a[i * 4 + j] == 1)//二維數組轉一維數組
				g[i][j] = 1;
			else
				g[i][j] = 0;
	int cnt = 0;//連通塊的個數
	for (int i = 0; i < 3; i++)
		for (int j = 0; j < 4; j++)
			if (g[i][j] == 1) {
				dfs(g, i, j);
				cnt++;
			}
	return cnt == 1;
}
int main() {
	do {
		if (check())
			ans++;
	} while (next_permutation(a, a + 12));//全排列
	cout << ans;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章