UVa 127 – “Accordian” Patience 資料結構專題

來源:互聯網
上載者:User
127 - "Accordian"
Patience

題目連結:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=63


題目類型: 資料結構, 鏈表


題意:

發一副牌(52張,分兩行), 從左往右開始, 對於當前這張牌,如果有左邊第一個或左邊第三個的數值(face-value)或者形狀(suit)是一樣的,就把該張牌移動到左邊第一行或者第三行(如果左1和左3都可以移,則優先移動到左3),移動後,如果繼續滿足移動條件,則繼續移動,直到不能移動為止。
當有一堆牌為空白時,要把“空的”消去。


解題思路:

這題就是簡單的類比,對於每一堆牌,用一個棧來儲存。 解體的關鍵關鍵在於把“空的牌堆”消去的這一過程。 開始時直接用數組做, 但是由於數組刪除一個元素要移動很多,效率低,導致TLE。 然後又準備用鏈表解決。開始時打算用STL的list,但是發現用得很不爽。於是就自己寫了用數組實現的鏈表來做,成功AC。

10269644 127 "Accordian"
Patience
Accepted C++ 0.412 2012-06-29 04:26:42

輸入:

QD AD 8H 5S 3H 5H TC 4D JH KS 6H 8S JS AC AS 8D 2H QS TS 3S AH 4H TH TD 3C 6S
8C 7D 4C 4S 7S 9H 7C 5D 2S KD 2D QH JD 6D 9D JC 2C KH 3D QC 6C 9S KC 7H 9C 5C
AC 2C 3C 4C 5C 6C 7C 8C 9C TC JC QC KC AD 2D 3D 4D 5D 6D 7D 8D TD 9D JD QD KD
AH 2H 3H 4H 5H 6H 7H 8H 9H KH 6S QH TH AS 2S 3S 4S 5S JH 7S 8S 9S TS JS QS KS
#


輸出:

6 piles remaining: 40 8 1 1 1 1
1 pile remaining: 52

#include<iostream>#include<cstdio>#include<stack>using namespace std;int nSize;struct String{    char face[3];};struct Pile{     stack<String>face;}arr[100];int prev[100], next[100];// 消除“空的牌堆”void DelGaps(){    int i,j;    for(i=0; i!=nSize; i=next[i]){        if(arr[i].face.empty()){            next[prev[i]] = next[i];            prev[next[i]] = prev[i];            return ;        }    }}// 進行移牌bool Move(){    int cnt=1;    for(int i=next[0]; i!=nSize; i=next[i],++cnt){        int last3, last1;        if(cnt>2) { // 如果當前位置在3以上,則先判斷可不可以移動到左3            last3 = prev[prev[prev[i]]]; // 獲得左3的座標            if( arr[i].face.top().face[0]==arr[last3].face.top().face[0]                    || arr[i].face.top().face[1]==arr[last3].face.top().face[1]){                arr[last3].face.push(arr[i].face.top() );                arr[i].face.pop();                return true;            }          }        last1= prev[i];  // 獲得左1的座標        if(arr[i].face.top().face[0]==arr[last1].face.top().face[0]                 || arr[i].face.top().face[1]==arr[last1].face.top().face[1]){            arr[last1].face.push(arr[i].face.top());            arr[i].face.pop();            return true;        }    }    // 不可一繼續移動了,返回false    return false;}void Solve(){    // 只要可以移動,就一直移動下去    while(Move())        DelGaps();  // 每次移動後,到進行消除空堆的過程}int main(){    freopen("input.txt","r",stdin);    nSize=0;    String temp;    while(scanf("%s", temp.face)){        if(temp.face[0]=='#') break;        while(!arr[nSize].face.empty())            arr[nSize].face.pop();        arr[nSize].face.push(temp);                prev[nSize] = nSize-1;        next[nSize] = nSize+1;        ++nSize;                if(nSize==52) {                     Solve();                        int num=0;            for(int i=0; i!=nSize; i=next[i])                ++num;                       if(num==1) printf("1 pile remaining: ");   // POJ的這題, 這個pile也有加s            else printf("%d piles remaining: ", num);                        for(int i=0; i!=nSize; i=next[i]){                if(i) printf(" ");                printf("%d", arr[i].face.size());            }            printf("\n");            nSize=0;        }    }    return 0;}


——      生命的意義,在於賦予它意義。 

                   原創 http://blog.csdn.net/shuangde800 , By
  D_Double



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.