计蒜客 蓝桥模拟赛 连连看 (dfs)

传送门

答案:89

思路:

    这个直接暴搜就好了,图这么小》

#include <bits/stdc++.h>

using namespace std;
int a[6][6] = {
	{0,0,0,0,0,0},
	{0,1,4,2,5,0},
	{0,2,1,2,1,0},
	{0,3,1,3,2,0},
	{0,2,5,3,4,0},
	{0,0,0,0,0,0}
};
int ans = -1;
int go[4][2] = {0,1,1,0,0,-1,-1,0};
bool check(int x,int y)
{
	for(int i = 0;i < 4;++i)
	{
		int tx = x + go[i][0];
		int ty = y +go[i][1];
		if(a[tx][ty] == 0) return 1;
	}
	return 0;
}
void dfs(int step,int sum)
{
	ans = max(sum,ans);
	for(int i = 1;i <= 4;++i)
	{
		for(int j = 1;j <= 4;++j)
		{
			if(a[i][j] == 0) 
				continue;
			if(!check(i,j))
				continue;
			for(int ii = 1;ii <= 4;++ii)
			{
				for(int jj = 1;jj <= 4;++jj)
				{
					if(i == ii && j == jj) continue;
					if(!check(ii,jj)) continue;
					if(a[i][j] == a[ii][jj])
					{
						int w = a[i][j];
						a[i][j] = a[ii][jj] = 0;
						dfs(step + 1,sum + step * w);
						a[i][j] = a[ii][jj] = w;
					}
				}
			}
		}
	}
	return ;
}
int main()
{
	ans = -1;
	dfs(1,0);
	cout << ans << endl;
}

猜你喜欢

转载自blog.csdn.net/howardemily/article/details/79763409
今日推荐