您的位置:首页 > 其它

UVA 127 链表和栈的使用

2013-12-05 20:44 225 查看
刘汝佳的题目感觉都是比较难以处理的,就像这道题目,一看数据简直觉得头大。。。加上这个英文我也看的想死

最后看别人博客的题意讲解才知道原来是要移牌。

然后如果熟练的使用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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: