标签:not 报错 logs ons sudo rac else 规范 lin
1.概要部分
1)规格部分
输出规格:
用一个文本文件(假设名字叫 sudoku.txt)的形式保存起来,每次生成的txt文件需要覆盖上次生成的txt文件,文件内的格式如下,数与数之间由空格分开,终局与终局之间空一行,行末无空格
-s 实际输出:
4 7 8 9 1 6 2 5 3 1 5 9 8 2 3 7 6 4 2 6 3 5 4 7 8 9 1 7 4 6 1 3 5 9 8 2 8 9 1 4 6 2 5 3 7 5 3 2 7 8 9 1 4 6 3 8 7 6 5 1 4 2 9 6 1 5 2 9 4 3 7 8 9 2 4 3 7 8 6 1 5 4 7 8 9 1 6 2 5 3 1 5 9 8 2 3 7 6 4 2 6 3 5 4 7 8 9 1 7 4 6 1 3 5 9 8 2 8 9 1 4 6 2 5 3 7 5 3 2 7 8 9 1 4 6 3 8 7 6 5 1 4 2 9 6 1 5 2 9 4 3 7 8 9 2 4 3 7 8 6 1 5 4 7 8 9 1 6 2 5 3 1 5 9 8 2 3 7 6 4 2 6 3 5 4 7 8 9 1 7 4 6 1 3 5 9 8 2 8 9 1 4 6 2 5 3 7 5 3 2 7 8 9 1 4 6 3 8 7 6 5 1 4 2 9 6 1 5 2 9 4 3 7 8 9 2 4 3 7 8 6 1 5 ...
-c 实际输出:
3 1 2 7 8 9 4 5 6 4 5 6 3 1 2 7 8 9 7 8 9 4 5 6 3 1 2 2 3 1 9 7 8 6 4 5 6 4 5 2 3 1 9 7 8 9 7 8 6 4 5 2 3 1 1 2 3 8 9 7 5 6 4 5 6 4 1 2 3 8 9 7 8 9 7 5 6 4 1 2 3 3 1 2 7 9 8 4 5 6 4 5 6 3 1 2 7 9 8 7 9 8 4 5 6 3 1 2 2 3 1 8 7 9 6 4 5 6 4 5 2 3 1 8 7 9 8 7 9 6 4 5 2 3 1 1 2 3 9 8 7 5 6 4 5 6 4 1 2 3 9 8 7 9 8 7 5 6 4 1 2 3 3 1 2 8 7 9 4 5 6 4 5 6 3 1 2 8 7 9 8 7 9 4 5 6 3 1 2 2 3 1 9 8 7 6 4 5 6 4 5 2 3 1 9 8 7 9 8 7 6 4 5 2 3 1 1 2 3 7 9 8 5 6 4 5 6 4 1 2 3 7 9 8 7 9 8 5 6 4 1 2 3 ...
2) 输入规格:
对于命令:
a. -c 100,结果如上
b. -c 0,
number should be positive
c. -c abc
invalid number
d. -c
invalid parameter number
e. -s XXXX\liuchang_review\Sudoku\subject.txt
输入内容:
4 7 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 1 0 0 6 0 0 0 0 0 0 8 0 1 0 0 2 5 0 0 0 0 0 7 0 0 0 4 0 0 0 0 0 5 1 0 0 0 6 0 0 0 0 0 0 7 0 0 0 0 0 0 8 0 0 0 4 7 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 0 1 0 0 6 0 0 0 0 0 0 8 0 1 0 0 2 5 0 0 0 0 0 7 0 0 0 4 0 0 0 0 0 5 1 0 0 0 6 0 0 0 0 0 0 7 0 0 0 0 0 0 8 0 0 0 ...
输出如上提到。
f. -s 0
number should be positive
e. -s abc
invalid number
f. -s
invalid parameter number
g. 无参数
invalid parameter number
h. -abc
unknown function
总体评价:
作为一个程序,能在不崩溃的前提下,处理正确的输入并输出符合规范的结果,处理异常的输入并返回报错信息,规格实现非常完善。
2) 需求部分:
1. 生成数独:
- 生成不重复的数独终局至文件
- 读取文件内的数独问题,求解并将结果输出到文件
- 在生成数独矩阵时,左上角的第一个数为:(学号后两位相加)% 9 + 1。例如学生A学号后2位是80,则该数字为(8+0)% 9 + 1 = 9,那么生成的数独棋盘应如下(x表示满足数独规则的任意数字)
1. 格式和不重复性:
运行 -c 100000,使用字典树存储和查重:
COUNT = 100000 class TrieNode:
"""A Trie tree specialized to store strings of sudoku solutions.""" desc = {} char = "" def __init__(self): self.desc = {} self.char = "" def insert(self, string):
"""
Each time we come to or create a lower node, directed by the first character of ‘string‘. If len(string) is 1, we assert the node does not exist.
""" if len(string) == 0: return elif len(string) == 1: if len(self.desc) > 0 and string[0] in self.desc: print("Same sudokus.") input() if string[0] not in self.desc: new_node = TrieNode() #print(new_node) self.desc[string[0]] = new_node new_node.char = string[0] self.desc[string[0]].insert(string[1:]) def display(self): for key, node in self.desc.items(): node.display() class Reader: pos = 0 f = None line = 1 def __init__(self, f): self.f = f def match(self, expect="\n"): c = self.f.read(1) if c == "\n": self.line += 1 if c == expect: return c, True else: return c, False if __name__ == "__main__": f_in = open(r"XXXX\liuchang_review\Sudoku\Sudoku\sudoku.txt", "r") reader = Reader(f_in) root = TrieNode() for sudoku_count in range(COUNT): if sudoku_count%10000 == 0: print(sudoku_count) sudoku_str = "" for j in range(9): #line for k in range(9): char, v = reader.match() sudoku_str += char if k != 8: char, v = reader.match(" ") else: char, v = reader.match("\n") if not v: print("PE in line "+str(reader.line)) if sudoku_count != COUNT-1: char, v = reader.match("\n") if not v: print("PE in line " + str(reader.line)) root.insert(sudoku_str)
结果:未报重复错误和格式错误。
2. 正确性:
调用check_validity()函数在输出前检查每一行每一列每一宫是否填满1-9。
bool check_validity(int s[9][9]) const{ int record = 0; const int mask = 0x1ff; // 9 ones for (int group_x = 0; group_x < 3; group_x++) { for (int group_y = 0; group_y < 3; group_y++) { record = 0; for (int x = 3 * group_x; x < 3 * group_x + 3; x++) { for (int y = 3 * group_y; y < 3 * group_y + 3; y++) { int v = s[x][y]; record |= 1 << (v-1); } } if (record != mask) { return false; } } } for (int line = 1; line <= 9; line++) { record = 0; for (int row = 1; row <= 9; row++) { int v = s[line][row]; record |= 1 << (v - 1); } if (record != mask) { return false; } } for (int row = 1; row <= 9; row++) { record = 0; for (int line = 1; line <= 9; line++) { int v = s[line][row]; record |= 1 << (v - 1); } if (record != mask) { return false; } } return true; }
结果:
全部正确。
标签:not 报错 logs ons sudo rac else 规范 lin
原文地址:http://www.cnblogs.com/wangchenyu1996/p/7617000.html