UVa Problem 10152 ShellSort (龜殼排序)

來源:互聯網
上載者:User
// ShellSort (龜殼排序)// PC/UVa IDs: 110407/10152, Popularity: B, Success rate: average Level: 2// Verdict: Accepted// Submission Date: 2011-05-27// UVa Run Time: 0.656s//// 著作權(C)2011,邱秋。metaphysis # yeah dot net//// 如何移動才能實現最少移動步驟?這個問題花費了我一些時間去思考。考慮如下一個初始狀態和最終// 狀態(為瞭解釋方便,在烏龜的名字前面用方括弧標記了序號)://// 初始狀態:             最終狀態:// [4]Yertle            [1]Oscar// [1]Oscar             [2]Baron// [2]Baron             [3]Lord// [3]Lord              [4]Yertle// [5]King              [5]King// [7]White             [6]Kong// [6]Kong              [7]White//// 可以通過如下六步達到目標,六步是所需最少步驟。// // [4]Yertle            [6]Kong                 [5]King// [1]Oscar             [4]Yertle               [6]Kong// [2]Baron             [1]Oscar                >>>>[4]Yertle// [3]Lord      ==>>    [2]Baron       ==>>     [1]Oscar        ==>>// [5]King              [3]Lord                 [2]Baron// [7]White             >>>>[5]King             [3]Lord// >>>>[6]Kong          [7]White                [7]White//// [4]Yertle            [3]Lord                 [2]Baron// [5]King              [4]Yertle               [3]Lord// [6]Kong              [5]King                 [4]Yertle// [1]Oscar     ==>>    [6]Kong        ==>>     [5]King         ==>>// [2]Baron             [1]Oscar                [6]Kong// >>>>[3]Lord          >>>>[2]Baron            >>>>[1]Oscar// [7]White             [7]White                [7]White////// [1]Oscar// [2]Baron// [3]Lord// [4]Yertle// [5]King// [6]Kong// [7]White//// 演算法如下:假設有 n 只烏龜,編號為 1 ~ n,對於編號為 n 的烏龜,如果編號為 n - 1 的烏龜在// 編號為 n 的烏龜的下方,將編號為 n - 1 的烏龜放到頂端,然後對編號為 n - 1,n - 2,... ,// 2,1 的烏龜繼續以上操作,直到排序完畢。#include <iostream>using namespace std;#define MAXSIZE 200#ifndef DEBUG_MODE//#define DEBUG_MODE#endifstruct turtle{string name;int index;};void shell_sort(turtle start[], turtle last[], int capacity){// 為初始狀態的烏龜賦予序號。for (int i = 0; i < capacity; i++)for (int j = 0; j < capacity; j++)if (start[j].name == last[i].name){start[j].index = last[i].index;break;}// 對初始烏龜狀態排序。for (int i = capacity; i > 1; i--){// 找到序號為i和(i - 1)的兩隻烏龜在數組的位置。int current, previous;for (int j = 0; j < capacity; j++){if (start[j].index == i)current = j;if (start[j].index == (i - 1))previous = j;}// 如果序號為(i - 1)的烏龜在序號為i的烏龜的下方,則將其放到頂端。if (previous > current){cout << start[previous].name << endl;turtle tmp = start[previous];for (int j = previous; j > 0; j--){start[j].name = start[j - 1].name;start[j].index = start[j - 1].index;}start[0].name = tmp.name;start[0].index = tmp.index;}#ifdef DEBUG_MODEcout << "<Debug Begin>" << endl;for (int j = 0; j < capacity; j++)cout << start[j].index << " " << start[j].name << endl;cout << "<Debug End>" << endl;#endif}}int main(int ac, char *av[]){int cases, capacity;turtle start[MAXSIZE];turtle last[MAXSIZE];cin >> cases;while(cases--){cin >> capacity;cin.ignore();// 讀入初始狀態。for (int i = 0; i < capacity; i++)getline(cin, start[i].name);// 讀入最終狀態,並賦予每隻烏龜序號。for (int i = 0; i < capacity; i++){getline(cin, last[i].name);last[i].index = (i + 1);}// 開始龜殼排序!shell_sort(start, last, capacity);cout << endl;}return 0;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.