UVA 101 木块问题

原本想用数组模拟呢,不过现在是在学习栈,所以就尝试用栈来解决,可是弄了好久就是A不了,哎,我选择看了别人的解题报告,学习别人的思想

代码如下

#include <iostream>

#include <vector>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <algorithm>

#include <string>

#include <stack>

using namespace std;

const int MAX_S = 30;

stack<int> stk[MAX_S];

stack<int> tempStk;

int N; int posa, posb;

void action(const string act1, const string act2, int a, int b)

{  

int i, j;

 int temp;

 if (act1 == act2)

  return;

 bool flag = false;  //找a的位置

 for (i = 0; i < N; i++)

 {

  while (!stk[i].empty())

  {   

 temp = stk[i].top();  

  if (temp == a)   

 {   

  posa = i;   

  flag = true;  

   break;

   }   

 stk[i].pop();

   tempStk.push(temp);  

 }

  while (!tempStk.empty())

  {  

  temp = tempStk.top();  

  stk[i].push(temp);  

  tempStk.pop();

  }

  if (flag)

   break;

 }  //找b的位置

 flag = false;

 for (i = 0; i < N; i++)

 {   

while (!stk[i].empty())

  {  

  temp = stk[i].top();   

 if (temp == b)

   {   

  posb = i;  

   flag = true;  

   break;   

 }  

  stk[i].pop();   

 tempStk.push(temp);   

}  

 while (!tempStk.empty())

  {  

  temp = tempStk.top();

   stk[i].push(temp);  

  tempStk.pop();

  }  

 if (flag)

   break;

 }

 if (posa == posb)  

 return;

 if (act1 == "move")

 {  

 if (act2 == "onto")  

 {   

 while (1)

   {   

  temp = stk[posb].top();

    if (temp == b)  

    break;

    stk[posb].pop();  

   stk[temp].push(temp);

   }  

  while (1)   

 {   

  temp = stk[posa].top();  

   if (temp == a)   

   break;

    stk[posa].pop();  

   stk[temp].push(temp);

   }   

 temp = stk[posa].top();

   stk[posa].pop();

   stk[posb].push(temp);

  }

  else

  {    

while (1)  

  {  

   temp = stk[posa].top();  

   if (temp == a)

     break;   

  stk[posa].pop();

    stk[temp].push(temp);

   }  

  temp = stk[posa].top();

   stk[posa].pop();

   stk[posb].push(temp);

  }

 }  

else

 {

  if (act2 == "onto")  

 {    //清空b上的木块   

 while (1)  

  {    

 temp = stk[posb].top();   

  if (temp == b)   

   break;

    stk[posb].pop();  

   stk[temp].push(temp);

   }    

while (1)

   {  

   temp = stk[posa].top();

    if (temp == a)  

    break;

    stk[posa].pop();  

   tempStk.push(temp);

   }  

  temp = stk[posa].top();

   stk[posa].pop();

   tempStk.push(temp);  

  while (!tempStk.empty())  

  {   

  temp = tempStk.top();

    stk[posb].push(temp);  

   tempStk.pop();

   }   

}   

else  

 {

   while (1)

   {     

temp = stk[posa].top();

    if (temp == a)

     break;   

  stk[posa].pop();  

   tempStk.push(temp);

   }    

temp = stk[posa].top();

   stk[posa].pop();  

  tempStk.push(temp);

   while (!tempStk.empty())

   {   

  temp = tempStk.top();

    stk[posb].push(temp);

    tempStk.pop();

   }   

 }

 }

}

int main()

{

 freopen("F:\\input.txt","r",stdin );

 ///  

int i, j;

 int temp;

 string act1, act2;

 int a, b;  

stack<int> tempStk;

 cin >> N;

 //初始状态

 for (i = 0; i < N; i++)  

 stk[i].push(i); 

 while (1)  

{   

cin >> act1;  

 if (act1 == "quit")

   break;

  cin >> a >> act2 >> b;

  action(act1, act2, a, b);

 }

 for (i = 0; i < N; i++)

 {  

 printf("%d:", i);  

 while (!stk[i].empty())  

 {  

  temp = stk[i].top();  

  stk[i].pop();  

  tempStk.push(temp); 

  }  

 if (!tempStk.empty())

   printf(" ");  

 while (!tempStk.empty())

  {  

  temp = tempStk.top();

   printf("%d",temp);

   tempStk.pop();

   if (tempStk.empty())   

  break;

   printf(" ");

  }

  printf("\n");

 }

 return 0;

}
View Code

 

转载于:https://www.cnblogs.com/LOB104-zhanglei/articles/3179076.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值