博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 127 - "Accordian" Patience
阅读量:4344 次
发布时间:2019-06-07

本文共 2244 字,大约阅读时间需要 7 分钟。

Algorithm:

1. Gradually input the card

2. Insert the card at its temperarory 'final' place

3. Start from the next position of the inserted card, rearrange the position of the piles of the cards

 

The idea is implemented as follow:

1 #include 
2 #include
3 #include
4 using namespace std; 5 6 struct Pair{ 7 char num; 8 char suit; 9 };10 11 typedef vector< list
> Piles;12 typedef int Position;13 14 bool match(Pair c1, Pair c2){15 return c1.num == c2.num || c1.suit == c2.suit;16 }17 18 Position insert(Pair card, Piles& p, Position pos){19 Position left = pos-1, left3rd = pos-3;20 21 if(left3rd >= 0 && match(p[left3rd].front(), card))22 return insert(card, p, left3rd);23 else if(left >= 0 && match(p[left].front(), card))24 return insert(card, p, left);25 else26 return pos;27 }28 29 void rearrange(Piles& p, Position pos){30 if( pos == p.size() )31 return;32 Pair card = p[pos].front();33 Position new_pos = insert(card, p, pos);34 if( new_pos != pos ){35 p[new_pos].push_front(card);36 p[pos].pop_front();37 if( p[pos].empty() ){38 Piles::iterator it = p.begin();39 p.erase( it+pos );40 }41 }42 rearrange(p, new_pos+1);43 }44 45 int main(int argc, char *argv[]){46 47 Piles p;48 Pair card;49 50 while(cin >> card.num && card.num != '#'){51 if(card.num == '\n')52 continue;53 else54 cin >> card.suit;55 // Find the 'temperary final' position of the card56 Position pos = insert(card, p, p.size());57 if (pos == p.size()){58 list
temp;59 temp.push_front(card);60 p.push_back(temp);61 }62 else63 p[pos].push_front(card);64 65 // Rearrange the cards66 rearrange(p, pos+1);67 68 }69 cout << p.size() << " piles remaining: ";70 for(int i = 0; i < p.size() ; i++){71 cout << p[i].size() << " ";72 }73 cout << endl;74 return 0;75 76 }

 

 

转载于:https://www.cnblogs.com/frankdj412/archive/2013/01/18/2866352.html

你可能感兴趣的文章
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
两台电脑如何实现共享文件
查看>>
组合模式Composite
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>
我的第一篇CBBLOGS博客
查看>>
【MyBean调试笔记】接口的使用和清理
查看>>
07 js自定义函数
查看>>
jQueru中数据交换格式XML和JSON对比
查看>>
form表单序列化后的数据转json对象
查看>>
[PYTHON]一个简单的单元測试框架
查看>>
[BZOJ4303]数列
查看>>
一般处理程序在VS2012中打开问题
查看>>
C语言中的++和--
查看>>
thinkphp3.2.3入口文件详解
查看>>
POJ 1141 Brackets Sequence
查看>>
Ubuntu 18.04 root 使用ssh密钥远程登陆
查看>>
Servlet和JSP的异同。
查看>>
虚拟机centOs Linux与Windows之间的文件传输
查看>>
IOS内存管理
查看>>
[Bzoj1009][HNOI2008]GT考试(动态规划)
查看>>