Solving the Data independence algorithm-C ++ implementation

Source: Internet
Author: User

# Include <iostream> using namespace std; // optional number int candidate [] = {1, 2, 4, 5, 6, 7, 8, 9 }; // mark whether the space is the original data int g_a [9] [9] = {0}; // print function void print (int (* a) [9]) {for (int I = 0; I <9; I ++) {for (int j = 0; j <9; j ++) {cout <a [I] [j] <";}cout <endl ;} // determine the number void ConfirmCandidate (int (* a) [9], int I, int j) {for (int I _candidate = 0; I _candidate <9; I _candidate ++) candidate [I _can Didate] = I _candidate + 1; for (int colm = 0; colm <9; colm ++) {if (a [I] [colm]! = 0) candidate [a [I] [colm]-1] = 0 ;}for (int line = 0; line <9; line ++) {if (a [line] [j]! = 0) candidate [a [line] [j]-1] = 0;} for (int line = I/3*3; line <I/3*3 + 3; line ++) {for (int colm = j/3*3; colm <j/3*3 + 3; colm ++) if (a [line] [colm]! = 0) candidate [a [line] [colm]-1] = 0 ;}// mark each space position void TotalNumbers (int (* a) [9], int I, int j) {for (int line = 0; line <I; line ++) {for (int colm = 0; colm <j; colm ++) if (a [line] [colm] = 0) {g_a [line] [colm] = 1 ;}}} // determine whether the entered number is correct bool JudgeValue (int (* a) [9], int I, int j) {// whether there are repeated numbers in the same row for (int colm = 0; colm <9; colm ++) {if (a [I] [colm] = a [I] [j] & j! = Colm) return false;} // whether the same column has repeated numbers for (int line = 0; line <9; line ++) {if (a [line] [j] = a [I] [j] & I! = Line) return false;} // There are no repeated numbers in a 3*3 square for (int line = I/3*3; line <I/3*3 + 3; line ++) {for (int colm = j/3*3; colm <j/3*3 + 3; colm ++) if (a [line] [colm] = a [I] [j] & I! = Line & j! = Colm) return false;} return true;} // determines whether bool success (int (* a) [9], int I, int j) is successful) {if (I <0 | j <0) return false; int line = I; int colm = j; for (; line <9; line ++, colm = 0) {for (; colm <9; colm ++) {// cout <"line =" <line <"colm =" <colm <endl; // if (colm = 8 & line = 8) return true; if (a [line] [colm]! = 0 & g_a [line] [colm] = 0) continue; ConfirmCandidate (a, line, colm); for (int c = 0; c <9; c ++) {if (candidate [c]> a [line] [colm]) {a [line] [colm] = candidate [c]; /** TEST * TEST available numbers * // * for (int I = 0; I <9; I ++) cout <candidate [I] <""; cout <endl; * // print (a); // determines whether the put value is correct. bool bRet = JudgeValue (a, line, colm); if (! BRet) {// cout <"bRet is false" <endl ;}else {// cout <"bRet is true" <endl; break ;}} else if (c = 8 & candidate [c] <= a [line] [colm]) {// cout <"line =" <line <"colm =" <colm <endl; int set_colm = 8; a [line] [colm] = 0; if (colm = 0) {while (g_a [line-1] [set_colm] = 0) {if (set_colm = 0) {line --; set_colm = 8;} else set_colm --;} return success (a, line-1, set_colm );} else {while (g_a [line] [colm-1] = 0) {if (set_colm = 0) {line --; set_colm = 8;} else colm --;} return success (a, line, colm-1) ;}}} return true;} http://www.shengshiyouxi.com int main () {// initializationint a [9] [9] = {8, 0, 0, 0, 0, 0, 0}, {0, 3, 6, 0, 0, 0, 0}, {0, 7, 0, 0, 9, 0, 0}, {0, 5, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8}, {, 0, 0}, {, 0, 0 }}; // test/* int a [9] [9] = {8, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0 }, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 8}, {0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }}; */TotalNumbers (a, 9, 9); success (a, 0, 0); print ();}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.