UVA 127 链表和栈的使用
2013-12-05 20:44
225 查看
刘汝佳的题目感觉都是比较难以处理的,就像这道题目,一看数据简直觉得头大。。。加上这个英文我也看的想死
最后看别人博客的题意讲解才知道原来是要移牌。
然后如果熟练的使用stack和手写链表的话,这个题目是不成问题的
最后看别人博客的题意讲解才知道原来是要移牌。
然后如果熟练的使用stack和手写链表的话,这个题目是不成问题的
#include <iostream> #include <cstdio> #include <cstring> #include <stack> #define N 100 using namespace std; struct node{ char ch[3]; }; stack<node> arr ; int next ,pre ; int t,cnt; void deletegap() { for (int i=0;i!=t;i=next[i]) { if (arr[i].empty()) { next[pre[i]]=next[i]; pre[next[i]]=pre[i]; return; } } } bool movement() { int i; for (i=next[0];i<t;i=next[i]) { int f1=pre[pre[pre[i]]]; if (f1>=0 && f1<t) { if (arr[i].top().ch[0]==arr[f1].top().ch[0] || arr[i].top().ch[1]==arr[f1].top().ch[1]) { arr[f1].push(arr[i].top()); arr[i].pop(); return true; } } int f0=pre[i]; if (arr[i].top().ch[0]==arr[f0].top().ch[0]||arr[i].top().ch[1]==arr[f0].top().ch[1]) { arr[f0].push(arr[i].top()); arr[i].pop(); return true; } } return false; } void solve() { while (movement()) { deletegap(); } } int main() { t=0; node temp; while (scanf("%s",temp.ch)) { if (temp.ch[0]=='#') break; while (!arr[t].empty()) arr[t].pop(); arr[t].push(temp); pre[t]=t-1; next[t]=t+1; t++; if (t==52) { solve(); int ans=0; for (int i=0;i!=t;i=next[i]) ans++; if (ans==1) printf("1 pile remaining: "); else printf("%d piles remaining: ",ans); for (int j=0;j!=t;j=next[j]) { if (j) putchar(' '); printf("%d",arr[j].size()); } putchar('\n'); t=0; } } return 0; }
相关文章推荐
- UVa 127 - "Accordian" Patience POJ 1214 链表题解
- UVa-127 "Accordian" Patience(栈+模拟链表)
- UVA127- "Accordian" Patience(模拟链表)
- UVa 127 "Accordian" Patience (模拟链表&栈)
- UVA 11988 Broken Keyboard(链表的使用)
- uva127 ``Accordian'' Patience ——链表模拟题
- UVa 127 - "Accordian" Patience POJ 1214 链表题解
- UVa 127 "Accordian" Patience (模拟链表&栈)
- 使用C++实现单向链表
- uva_127,栈以及vector的应用
- 【算法之链表(三)】单链表中,在仅允许使用一个指针的情况下,在指定的节点前面插入以及删除一个节点
- UVA10010使用数组和一个for循环控制前进路径方向不变
- 彻底理解链表中为何使用二级指针或者一级指针的引用
- 链表的经常使用操作
- 在单链表中将两个链表合并,合并之后的链表使用的是输入链表的节点空间,合并之后输入链表变为空表
- 线性表—使用链表实现
- Leetcode035--使用尾插法局部旋转链表
- qsort与bsearch在链表中的使用
- 25. Reverse Nodes in k-Group 怎样不使用额外空间翻转一个单向链表?
- uva-127 - "Accordian" Patience