95pts求助
查看原帖
95pts求助
1034295
QuaternaryTree楼主2024/5/22 12:55
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;

namespace FastIO {
	template <typename T> inline T read() { T x = 0, w = 0; char ch = getchar(); while (ch < '0' || ch > '9') w |= (ch == '-'), ch = getchar(); while ('0' <= ch && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar(); return w ? -x : x; }
	template <typename T> inline void write(T x) { if (!x) return; write<T>(x / 10), putchar(x % 10 ^ '0'); }
	template <typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; else if (x == 0) putchar('0'); write<T>(x); }
	template <typename T> inline void print(T x, char en) { if (x < 0) putchar('-'), x = -x; else if (x == 0) putchar('0'); write<T>(x), putchar(en); }
}; using namespace FastIO;

const int MAXN = 12;
const int score[10][10] =
{{0,0,0,0,0,0,0,0,0,0},
{0,6,6,6,6,6,6,6,6,6},
{0,6,7,7,7,7,7,7,7,6},
{0,6,7,8,8,8,8,8,7,6},
{0,6,7,8,9,9,9,8,7,6},
{0,6,7,8,9,10,9,8,7,6},
{0,6,7,8,9,9,9,8,7,6},
{0,6,7,8,8,8,8,8,7,6},
{0,6,7,7,7,7,7,7,7,6},
{0,6,6,6,6,6,6,6,6,6}};
int row[MAXN][MAXN],col[MAXN][MAXN],area[MAXN][MAXN],sdk[MAXN][MAXN];
int row_cnt[MAXN],col_cnt[MAXN],cnt,ans = -1;

inline int id(int i,int j){
	return (i - 1) / 3 * 3 + 1 + (j - 1) / 3;
}

inline int calc(){
	int tmp = 0;
	for(int i = 1;i <= 9;++i){
		for(int j = 1;j <= 9;++j){
			tmp += score[i][j] * sdk[i][j];
		}
	}
	return tmp;
}

void dfs(int r,int c,int cpl){
	if(cpl == 81){
		ans = max(ans,calc());
		return ;
	}
	for(int k = 1;k <= 9;++k){
        if(row[r][k] || col[c][k] || area[id(r,c)][k]) continue;
        row[r][k] = true;
        col[c][k] = true;
        area[id(r,c)][k] = true;
        row_cnt[r]++,col_cnt[c]++;
        sdk[r][c] = k;
        int tmpr = -1,nxt_r = 0,tmpc = -1,nxt_c = 0;
        for(int i = 1;i <= 9;++i){
        	if(row_cnt[i] > tmpr && row_cnt[i] < 9){
        		tmpr = row_cnt[i],nxt_r = i;
			}
		}
        for(int j = 1;j <= 9;++j){
        	if(col_cnt[j] > tmpc && (!sdk[nxt_r][j])){
        		tmpc = col_cnt[j],nxt_c = j;
			}
		}
        dfs(nxt_r,nxt_c,cpl+1);
        row[r][k] = false;
        col[c][k] = false;
        area[id(r,c)][k] = false;
        row_cnt[r]--,col_cnt[c]--;
        sdk[r][c] = 0;
    }
}

signed main(){
	for(int i = 1;i <= 9;++i){
		for(int j = 1;j <= 9;++j){
			sdk[i][j] = read<int>();
			if(sdk[i][j]!=0){
                row[i][sdk[i][j]] = true;
                col[j][sdk[i][j]] = true;
                area[id(i,j)][sdk[i][j]] = true;
                row_cnt[i]++,col_cnt[j]++;
                cnt++;
            }
		}
	}
	int tmpr = -1,r,tmpc = -1,c;
    for(int i = 1;i <= 9;++i){
    	if(row_cnt[i] > tmpr && row_cnt[i] < 9){
    		tmpr = row_cnt[i],r = i;
		}
	}
    for(int j = 1;j <= 9;++j){
    	if(col_cnt[j] > tmpc && (!sdk[r][j])){
    		tmpc = col_cnt[j],c = j;
		}
	}  
    dfs(r,c,cnt);
    write(ans);
	return 0;
}

测评记录

2024/5/22 12:55
加载中...