5pts求调
查看原帖
5pts求调
1285198
jianghaochen117楼主2024/11/2 15:58
#include <bits/stdc++.h>
using namespace std;
int shudu[15][15] = {{6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6} , 
				   	 {6 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 6} , 
					 {6 , 7 , 8 , 8 , 8 , 8 , 8 , 7 , 6} , 
					 {6 , 7 , 8 , 9 , 9 , 9 , 8 , 7 , 6} , 
					 {6 , 7 , 8 , 9 , 10 , 9 , 8 , 7 , 6} , 
					 {6 , 7 , 8 , 9 , 9 , 9 , 8 , 7 , 6} , 
					 {6 , 7 , 8 , 8 , 8 , 8 , 8 , 7 , 6} , 
					 {6 , 7 , 7 , 7 , 7 , 7 , 7 , 7 , 6} , 
					 {6 , 6 , 6 , 6 , 6 , 6 , 6 , 6 , 6}};
int maxn = -2e9;
map<int , int> p;
int mp[20][20];
int row[20] , col[20] , cell[10][10];
int ones[1 << 10];
int lowbit(int x)
{
	return x & (-x);
}
void init()
{
	for(int i = 0;i < 9;i++)
	{
		row[i] = col[i] = cell[i / 3][i % 3] = (1 << 9) - 1;
	}
	for(int i = 0;i < (1 << 9);i++)
	{
		int t = i;
		while(t > 0)
		{
			t &= t - 1;
			ones[i]++;
		}
	}
	for(int i = 0;i < 9;i++)
	{
		p[1 << i] = i + 1;
	}
}
void dfs(int cnt , int ans)
{
	if(cnt == 0)
	{
		maxn = max(ans , maxn);
		return;
	}
	int minn = 10 , x , y;
	for(int i = 0;i < 9;i++)
	{
		for(int j = 0;j < 9;j++)
		{
			if(mp[i][j] == 0)
			{
				int t = row[i] & col[j] & cell[i / 3][j / 3];
				if(ones[t] < minn)
				{
					minn = ones[t] , x = i , y = j;
				}
			}
		}
	}
	int t = row[x] & col[y] & cell[x / 3][y / 3];
	while(t)
	{
		int lt = lowbit(t);
		t -= lt;
		row[x] -= lt;
		col[y] -= lt;
		cell[x / 3][y / 3] -= lt;
		mp[x][y] = p[lt];
		ans += p[lt] * shudu[x][y];
		dfs(cnt - 1 , ans);
		row[x] += lt;
		col[y] += lt;
		cell[x / 3][y / 3] += lt;
		mp[x][y] = 0;
	}
}
int main()
{
	init();
	int cnt = 0;
	for(int i = 0;i < 9;i++)
	{
		for(int j = 0;j < 9;j++)
		{
			cin >> mp[i][j];
			if(mp[i][j] != 0)
			{
				row[i] -= 1 << mp[i][j] - 1;
				col[j] -= 1 << mp[i][j] - 1;
				cell[i / 3][j / 3] -= 1 << mp[i][j] - 1; 
			}
			else
			{
				cnt++;
			}
		}
	}
	dfs(cnt , 0);
	if(maxn == -2e9)
	{
		cout << -1 << endl;
	}
	else
	{
		cout << maxn << endl;
	}
	return 0;
}

我的代码值的了5分,请各位大佬帮忙查错

2024/11/2 15:58
加载中...