L1-104 九宫格

分数 20

全屏浏览

切换布局

作者 陈越

单位 浙江大学

htls.jpg

九宫格是一款数字游戏,传说起源于河图洛书,现代数学中称之为三阶幻方。游戏规则是:将一个 9×9 的正方形区域划分为 9 个 3×3 的正方形宫位,要求 1 到 9 这九个数字中的每个数字在每一行、每一列、每个宫位中都只能出现一次。
本题并不要求你写程序解决这个问题,只是对每个填好数字的九宫格,判断其是否满足游戏规则的要求。

输入格式:

输入首先在第一行给出一个正整数 n(≤10),随后给出 n 个填好数字的九宫格。每个九宫格分 9 行给出,每行给出 9 个数字,其间以空格分隔。

输出格式:

对每个给定的九宫格,判断其中的数字是否满足游戏规则的要求。满足则在一行中输出 1,否则输出 0。

输入样例:

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

代码长度限制

16 KB

Java (javac)

时间限制

500 ms

内存限制

256 MB

其他编译器

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

本来我的是路是每个九宫格的行和列和每个小九宫格都等于45,这个九宫格就是正确的,但只过了15分,所以就想了模拟一遍就可以了。

代码如下

#include<bits/stdc++.h>  
 
using namespace std;
  
int mp[10][10], a[10], b[10];

int main() {
    int n;
    cin >> n; // 输入九宫格的数量  
 
    for (int z = 1; z <= n; z++) { 
        int isLegal = 0;
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= 9; j++) {
                cin >> mp[i][j];
            }
        }
 
        
        for (int i = 1; i <= 9; i++) {
            isLegal = 0;   
            memset(a, 0, sizeof(a));   
            memset(b, 0, sizeof(b)); 
  
            for (int j = 1; j <= 9; j++) {
                a[mp[i][j]]++;
                b[mp[j][i]]++;
            }
 
       
            for (int k = 1; k <= 9; k++) {
                if (a[k] == 0 || b[k] == 0 || a[k] > 1 || b[k] > 1) 
                {
                    isLegal = 1;
                    break;
                }
            }
 
            if (isLegal == 1) break;  
        }
 
        int isLegalBox = 0; 
        
        for (int x = 1; x <= 7; x += 3) { 
            for (int y = 1; y <= 7; y += 3) {
                memset(a, 0, sizeof(a));   
 
                for (int i = x; i < x + 3; i++)
                {
                    for (int j = y; j < y + 3; j++) 
                    {
                        a[mp[i][j]]++;
                    }
                } 
                for (int k = 1; k <= 9; k++)
                {
                    if (a[k] == 0 || a[k] > 1) 
                    {
                        isLegalBox = 1; 
                        break;
                    }
                }
 
                if (isLegalBox == 1) break; 
            }
        }
 
     
        if (isLegal == 1 || isLegalBox == 1) cout << "0" << '\n';
        else cout << "1" << '\n';
    }
 
    return 0; /  
}

### 关于九宫格问题的C++实现 #### 1. 九宫格按键输入问题概述 九宫格键盘是一种常见的手机键盘布局方式,在这种布局下,字母被分配到不同的数字键上。当用户按下某个数字键时,可能会对应多个字符选项。因此,程序需要能够解析用户的按键序列并返回可能的文字组合。 对于这个问题的一个典型解法是采用回溯算法来遍历所有可能性,并构建最终的结果列表[^1]。 #### 2. C++代码示例 下面是一个简单的C++版本的九宫格按键输入问题解决方案: ```cpp #include <iostream> #include <vector> #include <unordered_map> using namespace std; class Solution { public: vector<string> letterCombinations(string digits) { unordered_map<char, string> phoneMap{ {'2', "abc"}, {'3', "def"}, {'4', "ghi"}, {'5', "jkl"}, {'6', "mno"}, {'7', "pqrs"}, {'8', "tuv"}, {'9', "wxyz"} }; vector<string> combinations; if (digits.empty()) return combinations; backtrack(combinations, "", 0, digits, phoneMap); return combinations; } private: void backtrack(vector<string>& combinations, const string& combination, int index, const string& digits, const unordered_map<char, string>& phoneMap) { if (index == digits.length()) { combinations.push_back(combination); } else { char digit = digits[index]; const string& letters = phoneMap.at(digit); for (const char& letter : letters) { backtrack(combinations, combination + letter, index + 1, digits, phoneMap); } } } }; int main() { Solution sol; string input = "23"; auto res = sol.letterCombinations(input); cout << "Possible combinations are:" << endl; for(auto &str : res){ cout << str << ' '; } return 0; } ``` 这段代码定义了一个`Solution`类,其中包含了两个主要成员函数:一个是公共接口`letterCombinations()`用于接收用户输入;另一个私有辅助方法`backtrack()`负责递归地生成所有的字符串组合。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值