? 枚舉 編程題#2:撥鍾問題(Coursera 程序設計與算法 專項課程4;函數memcpy的用法,switch case break 語句!)

編程題#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 0
2 2 2
2 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章