您的位置:首页 > 其它

汉诺塔(三)栈的应用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");
    }
}

不懂得可以私信。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: