练习codeforces1335D. Anti-Sudoku

题目如下
D. Anti-Sudoku
time limit per test2 seconds
memory limit per test256 megabytes
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 9 elements of this field (i.e. choose some 1≤𝑖,𝑗≤9 and change the number at the position (𝑖,𝑗) to any other number in range [1;9]) to make it anti-sudoku. The anti-sudoku is the 9×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 𝑡 independent test cases.

Input
The first line of the input contains one integer 𝑡 (1≤𝑡≤104) — the number of test cases. Then 𝑡 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.

题目大意
题目给出原本合法的数独九宫格,现要求在九次改动内使得每行每列至少有两个相同的数来破坏这个数独九宫格

题目分析
只要使原来的九宫格内数字改变来获得每行每列至少两个相同的数字,这是一个多解的问题,我首先想到的是对这个九行九列的阵列的对角线进行改变正好进行九次改动,又因为数独的要求,每个数字都要求< 9,所以对小于9的+1,剩下9-1;
另外也可以替换一个数,遍历整个九宫格,凡1换2,那么很容易实现了每行每列至少有两个相同的数字

如下

点击查看代码
#include <stdio.h>

int main(){
    int t;
    scanf("%d", &t);
    while (t--) {
        char s[10];
        for(int i = 0; i < 9; i++){
            scanf("%s", s);
            for(int j = 0; j < 9; j++){
                if (s[j] == '1')
                    s[j] = '2';
            }
            printf("%s\n", s);
        }
    }
    return 0;
}
posted @ 2025-07-06 20:45  sirro1uta  阅读(8)  评论(0)    收藏  举报