zoukankan      html  css  js  c++  java
  • 数独

    一、 Github项目地址

      https://github.com/Zhaoziqingg/sudoku1968

    二、各模块开发上的预估时间

    PSP2.1  

    Personal Software Process Stages    

    预估耗时

    (分钟)

    实际耗时

    (分钟)

    Planning

    计划

    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

    三、 解题思路

    根据题目要求,首先不懂github是一个什么样的东西。然后至于代码部分。

    代码分为生成数独和解数独。

    (1)命令行识别:

                  命令行使用 -c和-s,识别并根据命令生成或读取相应的文件。

        用ofstream。对于-c和-s以及一些错误的输入,进行处理,分别进行数独的生成或者求解。利用main函数里的argc和argv两个参数解决。

    (2)生成数独。

        一:数独左上角的第一个数应该为 (6 + 8)% 9 + 1 ,先定义一行随机初始行。

                   二:将第一行右移,得到后面的行数。

                   这样的话第一行的数字就可确定整个数独终局。由于第一行第一个数字已经确定,则第一行剩余8个数字可产生的不同排列为8!=40320种矩阵。

    (3)合并。

    (4)上传github

    四、设计实现过程

      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!
    ";
    
                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;
    }
  • 相关阅读:
    有多少程序员超过四十岁呢?
    乔布斯的演讲技巧:教你站在讲台上怎样化腐朽为神奇
    想要好看的WORDPRESS模板
    多些时间能少写些代码
    java播放au音频程序
    明星软件工程师的10种特质
    中国互联网创业的四个机遇
    Android环境搭建
    想写代码?离开你的电脑吧!
    深讨Java SE 6 在 HTTP 方面的新特性(NTLM)
  • 原文地址:https://www.cnblogs.com/zhaoziqing/p/8910501.html
Copyright © 2011-2022 走看看