UVa127,"Accordian" Patience

时间:2022-09-11 03:29:42

注意1堆的时候,pile后面没有s!!!!因为这个WA了一次,否则就1A了

犯了一个很幼稚很幼稚的错误,申请ans[]后玩了吧ans置0,结果调了好长好长时间,本来是敲完就能过的T T啊啊啊啊啊啊,一个多小时没了啊

附上我调试时写的代码(把每一次运转都输出了= =一个一个看的,真心用了好长时间,头都大了)

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#define maxn 100
using namespace std;
struct Node
{
int suit,rank;
//friend ostream& operator<<(ostream& cout,const Point& Node);
};
stack<Node> s[];
int init()
{
for (int i=;i<;i++) while (!s[i].empty()) s[i].pop();
char ch1,ch2;
cin>>ch1;
if (ch1=='#') return ;
cin>>ch2;
Node u;
if (isalpha(ch1))
switch (ch1){
case 'A':u.rank=;break;
case 'T':u.rank=;break;
case 'J':u.rank=;break;
case 'Q':u.rank=;break;
case 'K':u.rank=;break;
}
else u.rank=ch1-'';
switch (ch2){
case 'C':u.suit=;break;
case 'D':u.suit=;break;
case 'H':u.suit=;break;
case 'S':u.suit=;break;
}
s[].push(u);
for (int i=;i<=;i++){
cin>>ch1>>ch2;
if (isalpha(ch1))
switch (ch1){
case 'A':u.rank=;break;
case 'T':u.rank=;break;
case 'J':u.rank=;break;
case 'Q':u.rank=;break;
case 'K':u.rank=;break;
}
else u.rank=ch1-'';
switch (ch2){
case 'C':u.suit=;break;
case 'D':u.suit=;break;
case 'H':u.suit=;break;
case 'S':u.suit=;break;
}
s[i].push(u);
}
}
int match(Node a,Node b){
if (a.suit==b.suit||a.rank==b.rank) return ;
return ;
}
ostream& operator<<(ostream& cout,const Node& p) {
cout<<p.rank<<(char)(p.suit+'B')<<" ";
}
int tot(){
int t=;
for (int i=;i<=;i++)
t+=s[i].size();
cout<<" "<<t;
}
int main()
{
while (init()){
int i=,k,j;
while(i<=){
if (s[i].empty()){
i++;
continue;
}
k=i,j=;
while (k>=&&j<) {k--;if(!s[k].empty()) j++;}
if(j==) if(match(s[k].top(),s[i].top())){
//cout<<s[i].top()<<"->"<<s[k].top();
s[k].push(s[i].top());
s[i].pop();
//cout<<" "<<s[k].top();
//tot();cout<<endl;
i=k;
continue;
}
k=i;j=;
while (k>=&&j<){k--;if(!s[k].empty()) j++;}
if(j==)if(match(s[k].top(),s[i].top())){
//cout<<s[i].top()<<"->"<<s[k].top();
s[k].push(s[i].top());
s[i].pop();
//cout<<" "<<s[k].top();
//tot();cout<<endl;
i=k;
continue;
}
i++;
}
int ans[],top=;
memset(ans,,sizeof(ans));
for (i=;i<=;i++)
if (s[i].size()!=){
top++;
ans[top]=s[i].size();
}
if (top==)cout<<top<<" pile remaining:";
else cout<<top<<" piles remaining:";
for (i=;i<=top;i++) cout<<" "<<ans[i];
cout<<endl;
}
}