汉诺塔(三)栈的应用NYOJ93
2014-04-01 18:02
423 查看
水题,会用栈就行了
#include
#include
using namespace std;
int main()
{
int n;
scanf("%d",&n);
int p,q;
stack s1,s2,s3;
int x,y;
while(n--){
int mark=1;
scanf("%d%d",&p,&q);
s1.push(0);s2.push(0);s3.push(0);
while(p!=0){
s1.push(p);
p--;
}
while(q--){
scanf("%d
%d",&x,&y);
if(mark){
switch(x){
case 1:if(s1.top()==0)mark=0;else{
x=s1.top();s1.pop();
};break;
case 2:if(s2.top()==0)mark=0;else{
x=s2.top();s2.pop();
};break;
case 3:if(s3.top()==0)mark=0;else{
x=s3.top();s3.pop();
};break;
default : mark=0;break;
}
if(y==1){y=s1.top(); s1.push(x);}
else
if(y==2){y=s2.top();s2.push(x);}
else
if(y==3){y=s3.top();s3.push(x);}
else
mark=0;
if(y
}
}
if(mark)printf("legal\n");
else printf("illegal\n");
}
}
不懂得可以私信。。。
#include
#include
using namespace std;
int main()
{
int n;
scanf("%d",&n);
int p,q;
stack s1,s2,s3;
int x,y;
while(n--){
int mark=1;
scanf("%d%d",&p,&q);
s1.push(0);s2.push(0);s3.push(0);
while(p!=0){
s1.push(p);
p--;
}
while(q--){
scanf("%d
%d",&x,&y);
if(mark){
switch(x){
case 1:if(s1.top()==0)mark=0;else{
x=s1.top();s1.pop();
};break;
case 2:if(s2.top()==0)mark=0;else{
x=s2.top();s2.pop();
};break;
case 3:if(s3.top()==0)mark=0;else{
x=s3.top();s3.pop();
};break;
default : mark=0;break;
}
if(y==1){y=s1.top(); s1.push(x);}
else
if(y==2){y=s2.top();s2.push(x);}
else
if(y==3){y=s3.top();s3.push(x);}
else
mark=0;
if(y
}
}
if(mark)printf("legal\n");
else printf("illegal\n");
}
}
不懂得可以私信。。。
相关文章推荐
- NYOJ93 汉诺塔3(栈的应用)
- NYOJ 93 汉诺塔(三) 【栈的简单应用】
- nyoj 93 汉诺塔(三)
- nyoj 93 汉诺塔(三)
- NYOJ-93 汉诺塔(三)(栈)
- nyoj 93 汉诺塔(三) 【stack】
- NYOJ 93 汉诺塔(三) 栈
- NYOJ-93 汉诺塔(三)【模拟】
- NYOJ93 汉诺塔(三)【栈】
- NYOJ - 93 - 汉诺塔(三)(STL - stack)
- NYOJ:93 汉诺塔(三)(简单栈)
- NYOJ93 汉诺塔(三)
- NYOJ 93--汉诺塔(三)【stack】
- NYOJ 93 汉诺塔(三)(栈<stack>)
- nyoj--93 汉诺塔(三)(stack)
- NYOJ 93 汉诺塔(三)
- nyoj-93-汉诺塔(三)
- nyoj 93 汉诺塔(三)
- NYOJ 93 汉诺塔(三)(栈)
- nyoj93 汉诺塔(三)