您的位置:首页 > 大数据 > 人工智能

【poj】1363-Rails

2012-02-13 21:03 274 查看
#include <iostream>
#include <stdio.h>
#include <memory.h>
const int MAXSIZE = 10000;
using namespace std;
int trains[MAXSIZE];
int orders[MAXSIZE];
int stacks[MAXSIZE];
int top;
int main()
{
//   freopen("b:\\acm\\input.txt","r",stdin);
int n,m,mount;
bool bFin = true;
bool bEnd = false;
bool bFinput = true;
while( scanf("%d",&mount) != EOF )
{
bool bFound = true;
int j = 0;
memset(trains,MAXSIZE,0);
memset(orders,MAXSIZE,0);
memset(stacks,MAXSIZE,0);
top = 0;
if( mount == 0)
{
bFin = true;
if(bEnd)
return 0;
else
bEnd = true;
// cout << endl;
continue;
}
bEnd = false;
if(bFin)
{
if(bFinput)
{
bFinput = false;
}
else
{
cout << endl;
}
n = mount;;
for(int i = 0; i < n ; i++)
{
trains[i] = i+1;
cin >> orders[i];
}
bFin = false;
}
else
{
orders[0] = mount;
trains[0] = 1;
for(int i = 1 ; i < n; i++)
{
trains[i] = i+1;
cin >> orders[i];
}
}
//trains 1 2 3 4 5 6...
//orders 6 5 4 3 2 1...
for(int i = 0 ; i < n ; i++)
{
//思路:
//检查trains和stack中元素,如果
//trains中没有元素与orders[i]相等
//并且stack[top] != orers[i],No
j = 0;
/////This causes wrong answer
if(orders[i] == stacks[top-1])
{
stacks[top - 1] = 0;
top--;
if(i != n-1)
continue;
else
{
cout << "Yes" << endl;
continue;
}
}
while( (orders[i] != trains[j]) && (j < n) )
{
if(trains[j] != 0)
stacks[top++] = trains[j];
trains[j] = 0;
j++;
}
if( (stacks[top-1] != orders[i]) && (trains[j] != orders[i]))
{
cout << "No" << endl;
break;
}
else
{
if(stacks[top-1] == orders[i])
{
stacks[top-1] = 0;
top--;
}
if(trains[j] == orders[i])
{
trains[j] = 0;
}
if(i == n-1)
cout << "Yes" << endl;
}
}
}
return 0;
}


题目地址:
http://poj.grids.cn/practice/1363/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: