數獨

1、 Github項目地址git

  https://github.com/Zhaoziqingg/sudoku1968github

2、各模塊開發上的預估時間函數

PSP2.1  性能

Personal Software Process Stages    單元測試

預估耗時學習

(分鐘)測試

實際耗時ui

(分鐘)編碼

Planningspa

計劃

90

60

·Estimate

·估計這個任務須要多少時間

10

10

Development

開發

1800

1900

·Analysis

·需求分析(包括學習新技術)

120

200

·Design Spec

·生成設計文檔

120

300

·Design Review

·設計複審

50

60

·Coding Standard

·代碼規範

25

25

·Design

·具體設計

250

280

·Coding

·具體編碼

1000

1200

·Code Review

·代碼複審

60

45

·Test

·測試

40

60

Reporting

報告

90

120

·Test Report

·測試報告

30

15

·Size Measurement

·計算工做量

15

15

·Postmortem&Process Improvement Plan

·過後總結並提出過程改進計劃

60

80

 

 

合計

3730

3690

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

3、 解題思路

根據題目要求,首先不懂github是一個什麼樣的東西。而後至於代碼部分。

代碼分爲生成數獨和解數獨。

(1)命令行識別:

              命令行使用 -c和-s,識別並根據命令生成或讀取相應的文件。

    用ofstream。對於-c和-s以及一些錯誤的輸入,進行處理,分別進行數獨的生成或者求解。利用main函數裏的argc和argv兩個參數解決。

(2)生成數獨。

    一:數獨左上角的第一個數應該爲 (6 + 8)% 9 + 1 ,先定義一行隨機初始行。

               二:將第一行右移,獲得後面的行數。

               這樣的話第一行的數字就可肯定整個數獨終局。因爲第一行第一個數字已經肯定,則第一行剩餘8個數字可產生的不一樣排列爲8!=40320種矩陣。

(3)合併。

(4)上傳github

4、設計實現過程

  1) 函數功能

1 int main(int argc, char* argv[])  //主函數,獲取命令參數,這裏主要負責輸入和判別功能
2 
3 bool isSuitable(int count);
4 void solve(int count);//數獨求解,全部小格循環進行,採用回溯法
5 void RightShift(int *a, int n, int K);//右移函數
6 void creat(int count);//數獨生成

     2)單元測試設計

    測試命令行的斷定:-s  -c  -a  ab

    運行狀況斷定:-c 6  -c 1000000  -c 0  -s input

五,性能分析

六,主要部分代碼

1.移位

void RightShift(int *a, int n, int K)
{//對數據進行移位
    int pos = K % n;
    if (pos == 0);
    else 
    {
        while (pos--)
        {
            int temp = a[n - 1];
            for (int i = n - 1; i > 0; i--) a[i] = a[i - 1];
            a[0] = temp;
        }
    }
}

2.生成

void creat(int count)
{
    ofstream output;
    ofstream input;
    int shudu[9][9] = { 0 };
    int rand[] = { 1,2,3,4,5,6,7,8 };
    int num[9] = { 0,3,6,1,4,7,2,5,8 };
    int temp[9];
    int n;
    int i, j, k;

    shudu[0][0] = (6+8)%9+1;//數獨第一個數字爲(6+8)%9+1
for (i = 0; i < count; i++) { next_permutation(rand, rand + 8);//對1~8進行全排列,共有8!種可能 for (j = 0; j < 8; j++) //賦值給第一行 { shudu[0][j + 1] = rand[j]; } memcpy(&temp, &shudu[0], sizeof(shudu[0])); for (j = 0; j < 9; j++) { n = 9; RightShift(shudu[0], n, num[j]); for (k = 0; k < 8; k++) { output << shudu[0][k]; output << " "; } write << shudu[0][k] << endl; memcpy(&shudu[0], &temp, sizeof(temp)); } output << endl; } } }

3.main

int main(int argc, const char * argv[]) 
{
    char c[5];
    int n;
    int path[200];
    if (arc>2) 
    {
        //判斷是不是生成數獨
        strcpy_s(c, argv[1]);
        if (c[1] == 'c')
        {
            ofstream output;
            output.open("./BIN/sudoku.txt");  //打開或者建立TXT。
            n = atoi(argv[2]);
            if (n)
                creat(n);
            else
                cout << "Error!" << endl;
        }
        if (output.is_open())
            output.close();

    }


    else if (c[1]=='s')
    {
        ofstream output;
        FILE *fp = fopen(argv[2], "r");
        int temp;
        int i = 0, j = 0;
        if (!fp) {

            cout << "File open error!\n";

            return -1;

        }
        output.open("./BIN/sudoku.txt");
        while ((temp = getc(fp)) != EOF)
        {
            if (temp < '0' || temp > '9')
                continue;
            map[i][j] = temp - '0';
            j++;
            if (j == 9) 
            {//數字讀入
                i++;
                j = 0;
            }
            if (i == 9) 
            {
                solve(0);
                i = 0;
                j = 0;
            }
        }
        output.close();
        input.close();
    }
    else cout <<" Error!"<< endl;
}
相關文章
相關標籤/搜索