2025年天梯赛补题记录——九宫格

九宫格
image
输入样例:

3
5 1 9 2 8 3 4 6 7
7 2 8 9 6 4 3 5 1
3 4 6 5 7 1 9 2 8
8 9 2 1 4 5 7 3 6
4 7 3 6 2 8 1 9 5
6 5 1 7 3 9 2 8 4
9 3 4 8 1 6 5 7 2
1 6 7 3 5 2 8 4 9
2 8 5 4 9 7 6 1 3
8 2 5 4 9 7 1 3 6
7 9 6 5 1 3 8 2 4
3 4 1 6 8 2 7 9 5
6 8 4 2 7 1 3 5 9
9 1 2 8 3 5 6 4 7
5 3 7 9 6 4 2 1 8
2 7 9 1 5 8 4 6 3
4 5 8 3 2 6 9 7 1
1 6 3 7 4 9 5 8 3
81 2 5 4 9 7 1 3 6
7 9 6 5 1 3 8 2 4
3 4 1 6 8 2 7 9 5
6 8 4 2 7 1 3 5 9
9 1 2 8 3 5 6 4 7
5 3 7 9 6 4 2 1 8
2 7 9 1 5 8 4 6 3
4 5 8 3 2 6 9 7 1
1 6 3 7 4 9 5 8 2

输出样例:

1
0
0

AC代码:

点击查看代码
#include<iostream>
#include<vector>
#include<unordered_set>
using namespace std;

int main() {
    int n;
    cin >> n;
    while (n--) {
        int a[9][9],flag=0;
        for (int i=0;i<9;++i){
            for (int j=0;j<9;++j) {
                cin >> a[i][j];
            }
        }
        unordered_set<int> s;
        for (auto& ele : a) {
            for (auto& e : ele) {
                if(e>0&&e<10)s.insert(e);
            }
            if(s.size()!=9) {cout << "0\n";flag=1;break;}
            s.clear();
        }
        if(flag)  continue;

        for (int i=0;i<9;++i) {
            for (auto& ele : a) {
                if(ele[i]>0&&ele[i]<10)s.insert(ele[i]);
            }
            if(s.size()!=9) {cout << "0\n";flag=1;break;}
            s.clear();
        }
        if(flag)  continue;

        for (int x = 0; x < 9; x += 3) {
            for (int y = 0; y < 9; y += 3) {
                s.clear();
                for (int i = x; i < x+3; ++i) {
                    for (int j = y; j < y+3; ++j) {
                        if(a[i][j]>0&&a[i][j]<10) s.insert(a[i][j]);
                    }
                }
                if (s.size() != 9) {
                    cout << "0\n";
                    flag = 1;
                    break;
                }
            }
            if (flag) break;
        }
        if(!flag) cout << "1\n";
    }
    return 0;

}


思路:统计0-9每个数出现的数量,或者直接利用set集合的元素相异性和其大小进行判定 难点:安装要求遍历矩形

很简单,困困困晚上不困打比赛困,无力

posted @ 2025-03-31 18:58  xialingn  阅读(128)  评论(0)    收藏  举报