? 枚举 编程题#2:拨钟问题(Coursera 程序设计与算法 专项课程4;函数memcpy的用法,switch case break 语句!)

来源:互联网 发布:ubuntu版的qq 编辑:程序博客网 时间:2024/05/29 16:02

编程题#2:拨钟问题

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述
有9个时钟,排成一个3*3的矩阵。
这里写图片描述
           (图 1)
现在需要用最少的移动,将9个时钟的指针都拨到12点的位置。共允许有9种不同的移动。如下表所示,每个移动会将若干个时钟的指针沿顺时针方向拨动90度。

移动 影响的时钟
1 ABDE
2 ABC
3 BCEF
4 ADG
5 BDEFH
6 CFI
7 DEGH
8 GHI
9 EFHI
(图 2)

输入
从标准输入设备读入9个整数,表示各时钟指针的起始位置。0=12点、1=3点、2=6点、3=9点。

输出
输出一个最短的移动序列,使得9个时钟的指针都指向12点。按照移动的序号大小,输出结果。

样例输入

3 3 02 2 22 1 2

样例输出

4 5 8 9 



程序解答(此程序有缺陷,每个移动不能重复使用):

#include <iostream>#include <set>#include <vector>#include <iterator> using namespace std;bool guessOperation(const int clocks[], const int rotate[][9], const int signs[]){    int clocksTemp[9];    memcpy(clocksTemp, clocks, sizeof(clocksTemp));  //要拷贝一份,不能直接修改 clocks 数组!    // memcpy(dest, source, sizeof dest); http://zh.cppreference.com/w/cpp/string/byte/memcpy    int n = 0;  //表示第 n 个钟,从0到8    while (n < 9){        for (int i = 0; i < 9; i++)            clocksTemp[n] += rotate[i][n] * signs[i];        if (clocksTemp[n] % 4 != 0)            return false;        n++;    }    return true;}//函数对象,比较移动序列的长短class MyCompare{public:    bool operator()(const vector<int>& operation1, const vector<int>& operation2){        return (operation1.size() < operation1.size());    }};int main(){    int clocks[9];    int signs[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };  //标记要使用的操作,0代表不使用,1代表使用    set<vector<int>, MyCompare> operations;    set<vector<int>, MyCompare>::iterator ioperation;    vector<int> operation;    //vector<int>::iterator ii;    //根据读入数据初始化时钟旋转的次数,时钟旋转的次数应为4的整数倍!    int n;    for (int i = 0; i < 9; i++){        cin >> n;        switch (n){        case 0: clocks[i] = 0; break; //犯了一个超低级的错误!这里要加 break; !!!        case 1: clocks[i] = 1; break; //犯了一个超低级的错误!这里要加 break; !!!        case 2: clocks[i] = 2; break; //犯了一个超低级的错误!这里要加 break; !!!        case 3: clocks[i] = 3; break; //犯了一个超低级的错误!这里要加 break; !!!        }    }    const int rotate[9][9] =    {    //钟 A  B  C  D  E  F  G  H  I        {1, 1, 0, 1, 1, 0, 0, 0, 0 },  //op1: ABDE        {1, 1, 1, 0, 0, 0, 0, 0, 0 },  //op2: ABC        {0, 1, 1, 0, 1, 1, 0, 0, 0 },  //op3: BCEF        {1, 0, 0, 1, 0, 0, 1, 0, 0 },  //op4: ADG        {0, 1, 0, 1, 1, 1, 0, 1, 0 },  //op5: BDEFH        {0, 0, 1, 0, 0, 1, 0, 0, 1 },  //op6: CFI        {0, 0, 0, 1, 1, 0, 1, 1, 0 },  //op7: DEGH        {0, 0, 0, 0, 0, 0, 1, 1, 1 },  //op8: GHI        {0, 0, 0, 0, 1, 1, 0, 1, 1 }   //op9: EFHI    };    /*以下书写太麻烦!    int rotate[9][9];     //拨钟矩阵初始化    for (int i = 0; i < 3; i++){        for (int j = 0; j < 3; j++)            rotate[i][j] = 0;    }    //操作1——9初始化    rotate[0][0]++; rotate[0][1]++; rotate[0][3]++; rotate[0][4]++;      rotate[1][0]++; rotate[1][1]++; rotate[1][2]++;    rotate[2][1]++; rotate[2][2]++; rotate[2][4]++; rotate[2][5]++;    ...    */    int c, cTemp = 0;    while (1){        if (guessOperation(clocks, rotate, signs) == true){  //判断拨钟是否成功            for (int i = 0; i < 9; i++){    //记录此时的操作序列                if (signs[i] == 1)                    operation.push_back(i + 1);            }            operations.insert(operation);   //用 set 容器储存所有的成功操作序列,并且自动按移动序列的长短排序            operation.clear();    //清空当前的成功操作序列        }        //按rotate的列枚举所有情况         c = 0;        signs[0]++;        while (signs[c] > 1) {            signs[c] = 0;            c++;            if (c > 8)                break;            signs[c]++;        }        if (c > 8)            break;    }    if (operations.size() > 0){        ioperation = operations.begin();        ostream_iterator<int> o(cout, " ");  //放到输出流的时候,每放一个整数,就末尾添加一个" "中的内容 http://blog.csdn.net/u012482828/article/details/72549003        copy((*ioperation).begin(), (*ioperation).end(), o);  //V中的数据通过流迭代器o放到o输出流中        //参考:        // http://blog.csdn.net/happyygdx/article/details/78535968        // http://blog.csdn.net/happyygdx/article/details/78534884        cout << endl;    }    else{        cout << "无有效的移动序列!" << endl;    }    return 0;}



其他正确解法参考:
http://www.cnblogs.com/CCBB/archive/2011/09/13/2175043.html

阅读全文
0 0
原创粉丝点击