C. 结果填空:连连看(11分)

连连看是一款非常有意思的游戏。

在这里插入图片描述

我们可以把任意两个在图的在边界上的相同的方格一起消掉,比如把两个 44 消掉以后,

在这里插入图片描述
在这里插入图片描述

每次消掉两个方格的时候,都有会获得一个分数,第 ii次消的分数为 i × 方格的值。比如上面的消法,是第一次消,获得的分数为 1×4=4。

请你帮忙最优操作情况下,获得的分数最多为多少。

在这里插入图片描述

按照图中匹配方式 2 -> 1 > 1 > 2 > 3 > 4 > 5

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;

int map1[4][4]={1,4,2,5,2,1,2,1,3,1,3,2,2,5,3,4};
int dir[4][2]={1,0,0,1,-1,0,0,-1};
bool vis[5][5];
int ans;

bool fun(int x,int y){
	return x<0||y<0||x>=4||y>=4;
}

bool check(int x,int y){
	if(vis[x][y])return false;
	for(int i=0;i<4;i++){
		int dx=x+dir[i][0];
		int dy=y+dir[i][1];
		if(fun(dx,dy))return true;
		if(vis[dx][dy])return true;
	}
	return false;
}

void dfs(int step,int sum){
	ans=max(ans,sum);
	for(int i=0;i<4;i++){
		for(int j=0;j<4;j++){
			if(!check(i,j))continue;
			for(int k=0;k<4;k++){
				for(int l=0;l<4;l++){
					if(k==i&&l==j)continue;
					if(map1[i][j]==map1[k][l]&&check(k,l)){
						vis[i][j]=vis[k][l]=1;
						dfs(step+1,sum+step*map1[i][j]);
						vis[i][j]=vis[k][l]=0;
					}
				}
			}
		}
	}
}

int main(){
	ans=0;
	memset(vis,0,sizeof(vis));
	dfs(1,0);
	printf("%d\n",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/abc1235454/article/details/88743970
今日推荐