P1074 靶形数独 (搜索决策)
作者:互联网
题目链接:https://www.luogu.com.cn/problem/P1074
详细讲解:https://www.luogu.com.cn/blog/cpp/solution-p1074
当暴力过不了的时候也可以考虑改变搜索的起点从而减少搜索树的大小
#include <algorithm> #include <string> #include <string.h> #include <vector> #include <map> #include <stack> #include <set> #include <queue> #include <math.h> #include <cstdio> #include <iomanip> #include <time.h> #include <bitset> #include <cmath> #include <sstream> #include <iostream> #include <cstring> #define LL long long #define ls nod<<1 #define rs (nod<<1)+1 #define pii pair<int,int> #define mp make_pair #define pb push_back const double eps = 1e-10; const int maxn = 10 + 10; const LL mod = 1e9 + 7; const LL INF = 1e18; int sgn(double a){return a < -eps ? -1 : a < eps ? 0 : 1;} using namespace std; struct Point { int r,sum; bool operator < (const Point &t) const { return sum < t.sum; } }p[maxn]; int row[maxn][10],col[maxn][10],G[maxn][10]; int cnt,ans; int a[maxn][maxn]; int mapp[maxn][4]; inline int get_which(int i,int j) { if (i <= 3) { if (j <= 3) return 1; else if (j <= 6) return 2; else return 3; } else if (i <= 6) { if (j <= 3) return 4; else if (j <= 6) return 5; else return 6; } else { if (j <= 3) return 7; else if (j <= 6) return 8; else return 9; } } inline int get_point(int i,int j) { if(i==1||j==1||i==9||j==9) return 6; if(i==2||j==2||i==8||j==8) return 7; if(i==3||j==3||i==7||j==7) return 8; if(i==4||j==4||i==6||j==6) return 9; return 10; } inline void dfs(int x,int sc) { if (x == cnt+1) { if (sc > ans) ans = sc; return ; } for (int i = 1;i <= 9;i++) { if (!row[mapp[x][0]][i] && !col[mapp[x][1]][i] && !G[mapp[x][3]][i]) { row[mapp[x][0]][i] = col[mapp[x][1]][i] = G[mapp[x][3]][i] = 1; dfs(x+1,sc+mapp[x][2]*i); row[mapp[x][0]][i] = col[mapp[x][1]][i] = G[mapp[x][3]][i] = 0; } } } int main() { int now = 0; for (int i = 1;i <= 9;i++) { for (int j = 1;j <= 9;j++) { p[i].r = i; cin >> a[i][j]; if (a[i][j] > 0) { row[i][a[i][j]] = col[j][a[i][j]] = G[get_which(i, j)][a[i][j]] = 1; now += a[i][j] * get_point(i,j); } else p[i].sum++; } } sort(p+1,p+1+9); cnt = 0; for (int i = 1;i <= 9;i++) { for (int j = 1;j <= 9;j++) { if (a[p[i].r][j] == 0) { ++cnt; mapp[cnt][0] = p[i].r; mapp[cnt][1] = j; mapp[cnt][2] = get_point(p[i].r,j); mapp[cnt][3] = get_which(p[i].r,j); } } } ans = -1; dfs(1,now); cout << ans << endl; return 0; }
标签:10,include,形数,int,define,搜索,maxn,P1074,const 来源: https://www.cnblogs.com/-Ackerman/p/12431500.html