ZOJ-1259 输出所有的出站情况
2017-06-14 22:37
190 查看
题目大意:车厢进站,判断给出的出战顺序是否可能。
关于这道的解法,网上有很多答案,今天我尝试了一下,怎样把所有可能的情况都输出来。废话少说,直接上代码。
//
// main.cpp
// ZOj-1259
//
// Created by scnulpc on 2017/6/12.
// Copyright © 2017年 scnulpc. All rights reserved.
//
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
struct node {
int sin;
int sout;
node* left;
node* right;
node(){left=right=NULL;}
};
int counter=0;
//生成情况树
node *builtTree(int sin,int sout)//sin:栈内元素个数 sout:栈外元素个数
{
node* temp = new node();
temp->sin=sin;
temp->sout=sout;
if (sout==0) {
return temp;
}
if (sout>0) { //栈外有元素,可以入栈,生成左孩子
temp->left=builtTree(sin+1, sout-1);
}
if (sin>0) { //栈内有元素,可以出栈,生成右孩子
temp->right=builtTree(sin-1, sout);
}
return temp;
}
//遍历情况树,深度遍历法,并输出可能的情况
void DFS(node* p,stack<int> path,queue<int>source,queue<int>result)
//P:树根. path:站中的车厢顺序 source:未进站的车厢顺序 result:出战的车厢顺序
{
if (p->sout==0) {
counter++;
while (!path.empty()) {
result.push(path.top());
path.pop();
}
//输出情况
while (!result.empty()) {
cout<<result.front()<<" ";
result.pop();
}//while
return;
}//if
stack<int> tempPath = path;
queue<int> tempSource = source;
//向左走,压栈
if (p->left!=NULL) {
path.push(source.front());
source.pop();
DFS(p->left, path, source, result);
}
path = tempPath;
source = tempSource;
//向右走,出栈
if (p->right!=NULL) {
result.push(path.top());
path.pop();
DFS(p->right, path, source, result);
}
}
int main(int argc, const char * argv[]) {
int n=0;
cin>>n; //输入车厢数量
node* boot = builtTree(0, n);
stack<int> store;
queue<int> source;
queue<int> result;
for (int i=1; i<=n; i++) {
source.push(i);
}
DFS(boot, store, source, result);
return 0;
}
ps:自己测试的数据不多,觉得没问题。 如果发现问题,希望大神指正!
关于这道的解法,网上有很多答案,今天我尝试了一下,怎样把所有可能的情况都输出来。废话少说,直接上代码。
//
// main.cpp
// ZOj-1259
//
// Created by scnulpc on 2017/6/12.
// Copyright © 2017年 scnulpc. All rights reserved.
//
#include <iostream>
#include <stack>
#include <queue>
using namespace std;
struct node {
int sin;
int sout;
node* left;
node* right;
node(){left=right=NULL;}
};
int counter=0;
//生成情况树
node *builtTree(int sin,int sout)//sin:栈内元素个数 sout:栈外元素个数
{
node* temp = new node();
temp->sin=sin;
temp->sout=sout;
if (sout==0) {
return temp;
}
if (sout>0) { //栈外有元素,可以入栈,生成左孩子
temp->left=builtTree(sin+1, sout-1);
}
if (sin>0) { //栈内有元素,可以出栈,生成右孩子
temp->right=builtTree(sin-1, sout);
}
return temp;
}
//遍历情况树,深度遍历法,并输出可能的情况
void DFS(node* p,stack<int> path,queue<int>source,queue<int>result)
//P:树根. path:站中的车厢顺序 source:未进站的车厢顺序 result:出战的车厢顺序
{
if (p->sout==0) {
counter++;
while (!path.empty()) {
result.push(path.top());
path.pop();
}
//输出情况
while (!result.empty()) {
cout<<result.front()<<" ";
result.pop();
}//while
return;
}//if
stack<int> tempPath = path;
queue<int> tempSource = source;
//向左走,压栈
if (p->left!=NULL) {
path.push(source.front());
source.pop();
DFS(p->left, path, source, result);
}
path = tempPath;
source = tempSource;
//向右走,出栈
if (p->right!=NULL) {
result.push(path.top());
path.pop();
DFS(p->right, path, source, result);
}
}
int main(int argc, const char * argv[]) {
int n=0;
cin>>n; //输入车厢数量
node* boot = builtTree(0, n);
stack<int> store;
queue<int> source;
queue<int> result;
for (int i=1; i<=n; i++) {
source.push(i);
}
DFS(boot, store, source, result);
return 0;
}
ps:自己测试的数据不多,觉得没问题。 如果发现问题,希望大神指正!
相关文章推荐
- 求一个集合的所有子集 输出一个数所有平方和的情况 背包问题的递归解决
- 将一个正整数n,拆分成连续的自然数之和,输出所有可能的情况
- DFS求解DecodeWays输出所有情况和方法数
- (hdu step 4.3.2)Prime Ring Problem(n个数成环,输出两两之和为质数的所有情况)
- 输出N个数中取M个数的所有组合,排列情况
- 2016.3 网易笔试 '9'和'g'看不清,输出所有情况
- 将n个相异的球按每小组m个球进行分组,不足m个的也算一个小组。输出所有可能的分组方式;对其中的一种分组方式,都指明每个小组包含的球的情况!
- 八皇后问题(DFS加回溯)输出排列的所有情况
- N个数组,从每个数组中取出一个数,组成一个序列,输出所有可能的情况
- 输出字符串的所有排列组合情况
- 给定一个序列,输出所有满足栈规则的所有情况
- 输出前M个字母中任取N个的所有组合情况
- 给定入栈顺序,输出所有可能出栈情况及所有情况的总数
- 输出一个string的所有排列情况
- DFS求解出栈顺序,输出所有情况
- 给定入栈顺序,输出所有可能的出栈情况,并判断给定的序列是否为正确的输出序列
- 给定一个数组,n个数,打印所有可能的r位数的组合。例如{1,2,3,4}n=4,r=2.输出{1,2}{1,3}{1,4}{2,3}{2,4}{3,4}
- python 输出当前执行目录下的所有文件和子文件
- 《程序员面试金典》--输出二叉树中某一层的所有元素
- 实战:mysql统计指定架构的所有表的数据和索引大小情况