题解【洛谷P1074】[NOIP2009]靶形数独

题面

题解

一开始写了一个朴素的数独,无任何剪枝优化,得到了\(55\)分的好成绩

就是这道题加一个计算分数。

代码如下(\(\mathrm{55\ pts}\)):

/********************************
	Author: csxsl
	Date: 2019/10/28
	Language: C++
	Problem: P1074
********************************/
#include <bits/stdc++.h>
#define itn int
#define gI gi

using namespace std;

inline int gi()
{
	int f = 1, x = 0; char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return f * x;
}

inline long long gl()
{
	long long f = 1, x = 0; char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return f * x;
}

int n, m, ans, a[10][10];
bool h[10][10], l[10][10], fz[10][10];
const int fenshu[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}
};

inline int getfz(int x, int y) {return (x - 1) / 3 * 3 + (y - 1) / 3 + 1;}

inline void getans()
{
	int sum = 0;
	for (int i = 1; i <= 9; i+=1)
	{
		for (int j = 1; j <= 9; j+=1)
		{
			sum = sum + a[i][j] * fenshu[i][j];
		}
	}
	if (sum > ans) ans = sum;
}

void dfs(int x, int y)
{
	if (a[x][y])
	{
		if (x == 9 && y == 9) {getans(); return;}
		else if (y == 9) dfs(x + 1, 1);
		else dfs(x, y + 1);
	}
	else 
	{
		for (int i = 1; i <= 9; i+=1)
		{
			if (!a[x][y] && !h[x][i] && !l[y][i] && !fz[getfz(x, y)][i])
			{
				a[x][y] = i;
				h[x][i] = l[y][i] = fz[getfz(x, y)][i] = 1;
				if (x == 9 && y == 9) {getans(); return;}
				else if (y == 9) dfs(x + 1, 1);
				else dfs(x, y + 1);
				a[x][y] = 0;
				h[x][i] = l[y][i] = fz[getfz(x, y)][i] = 0;
			}
		}
	}
}

int main()
{
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	for (int i = 1; i <= 9; i+=1)
	{
		for (int j = 1; j <= 9; j+=1)
		{
			a[i][j] = gi();
			if (a[i][j]) h[i][a[i][j]] = l[j][a[i][j]] = fz[getfz(i, j)][a[i][j]] = 1;//标记数字
		}
	}
	dfs(1, 1);//搜索
	printf("%d\n", (ans == 0) ? (-1) : (ans));//输出
	return 0;
}

常见的搜索优化方式有:

  • 调换搜索顺序,让方案数少的先搜。

  • 剪枝,又分为可行性剪枝和最优性剪枝。

这里可以思考如何调换搜索顺序:

不难发现,只要一个位置上填了数字,我们就直接递归下一个数字即可,这一行枚举的数的个数就与这一行\(0\)的个数有关。

因此,我们可以预处理处每一行\(0\)的个数,从小到大排序后再进行搜索。

用不同的顺序进行搜索,效率也会大不一样!

注意此处需要开一个三维数组\(\mathrm{vis[0/1/2][i][j]}\)

\(\mathrm{vis[0/1/2][i][j]}\)分别表示第\(i\)行、第\(i\)列、第\(i\)个方阵有没有\(j\)这个数。

这样设的原因留给读者作为练习。

代码中的\(\mathrm{b[i]}\)表示搜索到了第几个数,顺序与每行\(0\)的个数有关。

调换搜索顺序后发现就可以\(\mathrm{AC}\)了。

代码

/********************************
	Author: csxsl
	Date: 2019/10/28
	Language: C++
	Problem: P1074
********************************/
#include <bits/stdc++.h>
#define itn int
#define gI gi

using namespace std;

inline int gi()
{
	int f = 1, x = 0; char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return f * x;
}

inline long long gl()
{
	long long f = 1, x = 0; char c = getchar();
	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return f * x;
}

int n, m, ans, a[10][10], b[85];
bool vis[3][10][10];
const int fenshu[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}
};//每个点对应的分值
struct Node
{
	int Zero_geshu/*每行0的个数*/, h/*是哪一行*/;
} zz[10];

inline int getfz(int x, int y) {return (x - 1) / 3 * 3 + (y - 1) / 3 + 1;}//(i,j)对应的方阵

inline void getans()//计算分值
{
	int sum = 0;
	for (int i = 1; i <= 9; i+=1)
	{
		for (int j = 1; j <= 9; j+=1)
		{
			sum = sum + a[i][j] * fenshu[i][j];
		}
	}
	if (sum > ans) ans = sum;//更新答案
}

void dfs(int bh)//搜索
{
	if (bh == 82) {getans(); return;}//搜完了
	int x = b[bh] / 9 + 1, y = b[bh] % 9;//求出当前的行和列
	if (!y) y = 9, x = b[bh] / 9;//特判y=0
	int g = getfz(x, y);//求出当前所在的方阵编号
	if (a[x][y]) dfs(bh + 1);//当前位置已经有数
	else 
	{
		for (int i = 1; i <= 9; i+=1)//枚举
		{
			if (!vis[0][x][i] && !vis[1][y][i] && !vis[2][g][i]) 
			{
				vis[0][x][i] = vis[1][y][i] = vis[2][g][i] = 1;
				a[x][y] = i;
                //填数
				dfs(bh + 1);
                //递归
				a[x][y] = 0;
				vis[0][x][i] = vis[1][y][i] = vis[2][g][i] = 0;
                //回溯
			}
		}
	}
}

inline bool cmp(Node x, Node y) {return x.Zero_geshu < y.Zero_geshu;}//按每一行0的个数排序

int main()
{
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	for (int i = 1; i <= 9; i+=1)
	{
		zz[i].h = i;
		int cnt = 0;
		for (int j = 1; j <= 9; j+=1)
		{
			a[i][j] = gi();
			int g = getfz(i, j);
			if (a[i][j])
			{
				vis[0][i][a[i][j]] = 1;
				vis[1][j][a[i][j]] = 1;
				vis[2][g][a[i][j]] = 1;//标记有数
			}
			else ++cnt;//增加这一行0的个数
		}
		zz[i].Zero_geshu = cnt;
	}
	sort(zz + 1, zz + 1 + 9, cmp);//排序
	int num = 0;
	for (int i = 1; i <= 9; i+=1)
	{
		for (int j = 1; j <= 9; j+=1)
		{
			b[++num] = (zz[i].h - 1) * 9 + j;//编号
		}
	}
	dfs(1);
	printf("%d\n", (ans == 0) ? (-1) : (ans));//输出,注意判断-1
	return 0;//结束
}
posted @ 2019-10-28 15:16  csxsi  阅读(267)  评论(0编辑  收藏  举报