D.Anti-Sudoku 思维题(Codeforces Round #634 (Div. 3))
D.Anti-Sudoku 思维题(Codeforces Round #634 (Div. 3))
原题链接:http://codeforces.com/contest/1335/problem/D
You are given a correct solution of the sudoku puzzle. If you don’t know what is the sudoku, you can read about it here.
The picture showing the correct sudoku solution:
Blocks are bordered with bold black color.
Your task is to change at most 99 elements of this field (i.e. choose some 1≤i,j≤91≤i,j≤9 and change the number at the position (i,j)(i,j) to any other number in range [1;9]) to make it anti-sudoku. The anti-sudoku is the 9×99×9 field, in which:
- Any number in this field is in range [1;9];
- each row contains at least two equal elements;
- each column contains at least two equal elements;
- each 3×3 block (you can read what is the block in the link above) contains at least two equal elements.
It is guaranteed that the answer exists.
You have to answer tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤104) — the number of test cases. Then tt test cases follow.
Each test case consists of 9 lines, each line consists of 9 characters from 1 to 9 without any whitespaces — the correct solution of the sudoku puzzle.
Output
For each test case, print the answer — the initial field with at most 9 changed elements so that the obtained field is anti-sudoku. If there are several solutions, you can print any. It is guaranteed that the answer exists.
Example
input
1
154873296
386592714
729641835
863725149
975314628
412968357
631457982
598236471
247189563
output
154873396
336592714
729645835
863725145
979314628
412958357
631457992
998236471
247789563
题意:给定我们一个数独,我么要将这个数独修改为anti-sudoku,即每行每列每块都至少有两个相同的元素,**我们最多有9次更改的机会。**一定有解,请输出任意一个有解的答案。
解题思路:一开始给定的是满足数独要求的,那么我们只要随机确定一个[1:9]的数字,再将每一行的确定数字更换为[1:9]除确定数字的一个,注意,每一行更换的都要相同,更换完之后得到的必然是答案,读者可以自行思考一下。因为原来满足每行每列每块数方阵没有相同数字,而每行每列每块数方阵都有9个数,说明1~9分别出现1次,只要其中一个数字变成另外一个数字就满足每行每列每9数方阵有两个相同数字。
弊端:用完了9次机会,不能实现最优解,
AC代码:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<cstring>
#include<map>
#include<iterator>
#include<list>
#include<set>
#include<functional>
#include<memory.h>
using namespace std;
int t; //t组测试用例
char antishudu[10][10];//用字符型数组存储,因为输入数据不含空格
int main(){
while(cin>>t){
while(t--){
for(int i=0;i<9;i++){
cin>>antishudu[i];
for(int j=0;j<9;j++){
//我们只要确定1~9的随机一个数,再将每行中的这个数换成其他的1~9的数即可。
if(antishudu[i][j]=='1'){
antishudu[i][j]='2';
}
}
}
for(int i=0;i<9;i++)
cout<<antishudu[i]<<endl;
}
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)