快活林资源网 Design By www.csstdc.com
我们在 Bitcoin SV 上实现了一个数独游戏智能合约,利用之前介绍过的一种合约范式可以将游戏中寻找解题方案的过程外包上链。因为求解数独问题的计算工作量会随着其行列数快速增长,实际上它也是一个 NP-完全 问题。不过我们可以借由比特币智能合约巧妙地寻求答案,只需要验证答案提供者所给出的解答是否满足要求即可,这样即可以将复杂的求解计算过程实现链下外包。
sCrypt 合约代码如下:
import "util.scrypt"; import "array.scrypt"; contract Sudoku { bytes board; static const int N = 9; static bytes EMPTY = b'00'; constructor(bytes board) { this.board = board; } function merge(bytes solution) : bytes { bytes newBoard = this.board; int i = 0; loop (N) { int j = 0; loop (N) { int value = this.readValue(newBoard, i, j); int inputValue = this.readValue(solution, i, j); if (value == 0) { require(inputValue <= 9); newBoard = this.setValue(newBoard, i, j, inputValue); } else { require(value == inputValue); } j++; } i++; } return newBoard; } public function solve(bytes solution) { require(len(solution) == Sudoku.N * Sudoku.N); bytes newBord = this.merge(solution); Array rowArray = new Array(); Array colArray = new Array(); Array squareArray = new Array(); int i = 0; loop (N) { int j = 0; loop (N) { // check for duplicate // in a row int rowElem = this.readValue(newBord, i, j); require(rowArray.indexOf(rowElem) == -1); rowArray.push(rowElem); // in a column int colElem = this.readValue(newBord, j, i); require(colArray.indexOf(colElem) == -1); colArray.push(colElem); // in a subgrid int squareElem = this.readSquareValue(newBord, i, j); require(squareArray.indexOf(squareElem) == -1); squareArray.push(squareElem); j++; } rowArray.clear(); colArray.clear(); squareArray.clear(); i++; } require(true); } static function readValue(bytes board, int i, int j): int { return Util.fromLEUnsigned(Util.getElemAt(board, Sudoku.index(i, j))); } static function setValue(bytes board, int i, int j, int value): bytes { return Util.setElemAt(board, this.index(i, j), Util.toLEUnsigned(value, 1)); } static function readSquareValue(bytes board, int i, int j): int { return Util.fromLEUnsigned(Util.getElemAt(board, Sudoku.indexSquare(i, j))); } static function index(int row, int col) : int { return row * Sudoku.N + col; } static function indexSquare(int i, int j) : int { int row = i / 3 * 3 + j / 3; int col = i % 3 * 3 + j % 3; return Sudoku.index(row, col); } }
快活林资源网 Design By www.csstdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
快活林资源网 Design By www.csstdc.com
暂无评论...
更新日志
2024年04月29日
2024年04月29日
- 4.6版本隐藏锚点位置一览,隐藏锚点在哪
- 群星.2024-追风者电视剧影视原声带【SONY】【FLAC分轨】
- 曾庆瑜.1992-18首中英文经典全集【派森】【WAV+CUE】
- 群星.1991-华纳浪漫心曲精丫华纳】【WAV+CUE】
- 幕府将军 Shogun
- 纳克鲁斯 Knuckles
- 特污兔《填空题》[FLAC/分轨][239.68MB]
- 陈奕迅《黑白灰》台湾版[WAV+CUE][400M]
- 群星《三大发烧女声》3CD[WAV+CUE][2G]
- 英雄传说闪之轨迹北方战役国际版 5月29日全球同步上市
- 仙剑世界风启测试定档5月31日 感受属于东方的浪漫幻想世界
- 元气骑士前传星界法师怎么玩 操控黑暗禁忌
- 物华弥新迷踪盘第二关怎么过 迷踪盘第二关通关方法
- 物华弥新迷踪盘第三关怎么过 迷踪盘第三关通关方法
- 物华弥新迷踪盘第四关怎么过 迷踪盘第四关通关方法