2015CCPC南阳场 H - Sudoku

本文介绍了一种简化版的Sudoku游戏,并提供了两种解决该问题的方法。游戏由四个2x2的矩阵组成,每行、每列及每个小矩阵内包含1到4的数字。文章详细介绍了使用深度优先搜索和迭代填充策略的解决方案。

比赛的时候读错了题意我去。。一直TLE+WA..

赛后看了题解才知道读错了题意。。愧疚啊我去。。这次比赛我读的两道题都读错了题意,坑死队友。。


H - Sudoku
Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u

Description

Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller. 

Actually, Yi Sima was playing it different. First of all, he tried to generate a   board with every row contains 1 to 4, every column contains 1 to 4. Also he made sure that if we cut the board into four   pieces, every piece contains 1 to 4. 

Then, he removed several numbers from the board and gave it to another guy to recover it. As other counselors are not as smart as Yi Sima, Yi Sima always made sure that the board only has one way to recover. 

Actually, you are seeing this because you've passed through to the Three-Kingdom Age. You can recover the board to make Yi Sima happy and be promoted. Go and do it!!!
 

Input

The first line of the input gives the number of test cases,   test cases follow. Each test case starts with an empty line followed by 4 lines. Each line consist of 4 characters. Each character represents the number in the corresponding cell (one of '1', '2', '3', '4'). '*' represents that number was removed by Yi Sima. 

It's guaranteed that there will be exactly one way to recover the board.
 

Output

For each test case, output one line containing  Case #x:, where   is the test case number (starting from 1). Then output 4 lines with 4 characters each. indicate the recovered board.
 

Sample Input

      
3 **** 2341 4123 3214 *243 *312 *421 *134 *41* **3* 2*41 4*2*
 

Sample Output

      
Case #1: 1432 2341 4123 3214 Case #2: 1243 4312 3421 2134 Case #3: 3412 1234 2341 4123

这里提供两种方法:

方法一:

思路:用res数组记录一下要填数字的点  用dfs枚举当前点可以填的所有数字。暴力搜索就可以了。

代码+注释如下:

#include<iostream> 
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=7;
char mp[maxn][maxn];
bool vis[maxn][maxn],flag;
int res[maxn*maxn][2];
int resnum;
void input()//输入 
{
	resnum=0;
	flag=false;
	memset(vis,false,sizeof(vis));
	for(int i=1;i<=4;i++)scanf("%s",(&mp[i][0])+1);
	for(int i=1;i<=4;i++)
		for(int j=1;j<=4;j++)
			//记录一下要填数字的点 
			if(mp[i][j]=='*'){res[resnum][0]=i,res[resnum][1]=j,resnum++;} 
			else vis[i][j]=true;
}
void dfs(int step)
{
	if(flag||step==resnum){flag=true;return;}
	int tx=res[step][0],ty=res[step][1];
	int book[maxn]={false};
	//枚举4个2*2的小矩阵 
	if(tx<=2&&ty<=2)for(int i=1;i<=2;i++)for(int j=1;j<=2;j++)if(mp[i][j]!='*')book[mp[i][j]-'0']=true;
	if(tx<=2&&ty>2)for(int i=1;i<=2;i++)for(int j=3;j<=4;j++)if(mp[i][j]!='*')book[mp[i][j]-'0']=true;
	if(tx>2&&ty<=2)for(int i=3;i<=4;i++)for(int j=1;j<=2;j++)if(mp[i][j]!='*')book[mp[i][j]-'0']=true;
	if(tx>2&&ty>2)for(int i=3;i<=4;i++)for(int j=3;j<=4;j++)if(mp[i][j]!='*')book[mp[i][j]-'0']=true;
	//枚举列和行 
	for(int i=1;i<=4;i++)
	{
		if(mp[tx][i]!='*')book[mp[tx][i]-'0']=true;
		if(mp[i][ty]!='*')book[mp[i][ty]-'0']=true;
	}
	for(int i=1;i<=4;i++)
		if(!book[i])
		{
			mp[tx][ty]=i+'0';
			dfs(step+1);
			if(flag)return;
			mp[tx][ty]='*';
		}
}
int main()
{
	int T;
	scanf("%d",&T);
	for(int tcase=1;tcase<=T;tcase++)
	{
		input();
		dfs(0);
		printf("Case #%d:\n",tcase);
		for(int i=1;i<=4;i++)printf("%s\n",(&mp[i][0])+1);
	}
}


方法二:

思路:每次都填一次那个可以确定的点,不能填的点放在队列中,队列为空则OK!

用xvis[i][num]数组表示第i行num这个数出现过了没---------------yvis数组同理

再用jz[i][num]数组表示矩阵i中num这个数出现过了没。

然后每次都填那个可以确定的点 然后继续更新这三个标记数组,然后该点出列。直到队列为空。

代码如下:

#include<iostream> 
#include<algorithm>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=10;
struct node
{
	int x,y;
};
bool xvis[maxn][5] ,yvis[maxn][5],jz[maxn][maxn];
char mp[maxn][maxn];
node temp,head;
queue<node>Q;
int findjz(int tx,int ty)//判断是在哪个矩阵
{
	if(tx<=2&&ty<=2)return 1;  
    if(tx<=2&&ty>2)return 2;
    if(tx>2&&ty<=2)return 3;
    return 4;
} 
void init()//初始化 
{
	memset(xvis,false,sizeof(xvis));
	memset(yvis,false,sizeof(yvis));
	memset(jz,false,sizeof(jz));
}
void input()
{
	for(int i=1;i<=4;i++)scanf("%s",mp[i]+1);
	for(int i=1;i<=4;i++)
		for(int j=1;j<=4;j++)
			if(mp[i][j]=='*'){temp.x=i,temp.y=j,Q.push(temp);}
			else
			{
				int num=mp[i][j]-'0';
				jz[findjz(i,j)][num]=xvis[i][num]=yvis[j][num]=true;//该数所处的行-列-矩阵都标记一下 }
			}
}
bool solve(node& head)
{
	int tx=head.x,ty=head.y;
	int sum=0,last;
	int pos=findjz(tx,ty);
	for(int i=1;i<=4;i++)
	{
		if(xvis[tx][i]||yvis[ty][i]||jz[pos][i])sum++;
		else last=i;
	}
	if(sum==3)//所以该要填的数字就可以确定了
	{
		xvis[tx][last]=yvis[ty][last]=jz[pos][last]=true;//更新标记 
		mp[tx][ty]=last+'0';
		return true;
	}
	return false;
}
int main()
{
	int T;
	scanf("%d",&T);
	for(int tcase=1;tcase<=T;tcase++)
	{
		init();
		input();
		while(!Q.empty()){head=Q.front(),Q.pop();if(!solve(head))Q.push(head);}
		printf("Case #%d:\n",tcase);
		for(int i=1;i<=4;i++)printf("%s\n",(&mp[i][0])+1);
	}
}




 



### CCPC 2023 H题 解析 关于CCPC 2023 H题的具体题目描述尚未公开,但从以往的比惯例以及类似的题目解析可以推测其可能涉及的内容和技术要点。以下是基于已有参考资料和专业知识对该类问题的解答框架。 #### 1. **问题背景** CCPC(Chinese Collegiate Programming Contest)作为国内重要的编程竞之一,通常会设计具有挑战性的算法问题来测试参者的逻辑思维能力和编码技巧。H题通常是比中的难点之一,往往涉及到复杂的算法模型或数据结构的应用[^2]。 #### 2. **潜在的技术方向** 根据过往的经验,H题可能会覆盖以下几个方面: - 动态规划 (Dynamic Programming)[^1] - 构造性问题 (Construction Problems)[^4] - 数学优化 (Mathematical Optimization) 假设该题属于动态规划类别,则需关注状态转移方程的设计;如果是构造性问题,则重点在于如何通过有限操作达到目标条件。 #### 3. **通用解题策略** 无论具体主题为何种类型,在面对高难度题时可遵循如下方法论: ##### (1)深入理解题目需求 仔细阅读并反复确认输入输出的要求及其约束条件,确保不会遗漏任何细节信息[^3]。 ##### (2)选取合适的算法工具箱 依据实际景挑选最匹配的方法论,比如当存在重叠子问题且具备最优子结构性质时优先选用DP技术[^1]。 ##### (3)编写清晰易懂的代码实现 采用模块化的方式分步完成整个功能开发流程,并辅以充分注释说明每一部分的作用机制。 ```cpp // 示例伪代码片段展示基本框架布局 #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; // 输入规模参数 vector<long long> dp(n+1, INF); // 初始化dp数组,默认极大值表示未访问过 dp[0]=0; for(int i=1;i<=n;i++){ for(auto &coin : coins){ if(i >= coin && dp[i - coin]+costs[coin]<dp[i]){ dp[i]=dp[i - coin]+costs[coin]; } } } cout << (dp[n]==INF ? -1 : dp[n])<< "\n"; } ``` 上述例子仅作示意用途,真实情况下应严格依照官方给定的数据范围调整变量类型及边界处理方式。 #### 4. **复杂度考量** 对于大规模实例而言,效率至关重要。因此除了正确率之外还需兼顾运行时间和内存消耗指标。一般建议尽可能降低渐近时间开销至O(NlogN)甚至更低级别。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值