洛谷: P1479 宿舍里的故事之五子棋

题目链接: https://www.luogu.com.cn/problem/P1479

思路:

这道题目可以打表或者搜索。每个位置有选择/不选择两种情况。搜索的时候我们一行一行的搜索,直到使用的棋子达到n为止。b[i]为五子连线的数量,b[i] = 1表示五子连线的数量可以取i,在最后我们将可以取达到的数量累加在一起就是最终的结果。

代码:

#include <bits/stdc++.h>
using namespace std;
int a[26][26], n, b[20];
int ck() {//是否能够连成一行
	int ans = 0;
	for (int i = 0; i < 5; i++) {
		if (a[i][0] && a[i][1] && a[i][2] && a[i][3] && a[i][4]) ans++;
		if (a[0][i] && a[1][i] && a[2][i] && a[3][i] && a[4][i]) ans++;
	}
	if (a[0][0] && a[1][1] && a[2][2] && a[3][3] && a[4][4]) ans++;
	if (a[0][4] && a[1][3] && a[2][2] && a[3][1] && a[4][0]) ans++;
	return ans;
}
void dfs(int x, int y, int u) {//选择或者不选两种
	if (u == n) {//已经是用完了
		int k = ck();
		b[k] = 1;
		return;
	}
	if (x == 5) return;
	if (y == 5) {
		dfs(x + 1, 0, u);
		return;
	}
	a[x][y] = 1;
	dfs(x, y + 1, u + 1);
	a[x][y] = 0;
	dfs(x, y + 1, u);
}
		
int main() {
	cin >> n;
	dfs(0, 0, 0);
	int sum = 0;
	for (int i = 1; i < 20; i++) {//b[i] i为五子连线的数量
		if (b[i]) sum += i;
	}
	cout << sum << endl;
	return 0;
}

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值