1.组合的输出
#include
#include
#include
using namespace std;
int m,n;
int a[22];
void input()
{
cin>>m>>n;
}
void print()
{
for(int i = 1;i<=n;i++)
{
cout<
2.八皇后问题
#include
using namespace std;
int ps[9];
bool l,r;
int num = 1;
void dfs(int b);
void print();
void dfs(int b){
if(b == 9){
print();
return;
}
else{
for(int i = 1;i<=8;i++){
bool yi = false;
for(int j = b-1;j>0;j--){
if((i+b == j+ps[j])||(b-i == j-ps[j])||(i == ps[j])) yi = true;
}
if(!yi){
ps[b] = i;
dfs(b+1);
}
}
}
}
void print(){
cout<<"No. "<
3.求先序排列
#include
#include
using namespace std;
struct Tree;
struct Tree{
char date;
Tree* leftTree=NULL,*rightTree=NULL;
};
void input(string &zhong,string &hou)
{
cin>>zhong>>hou;
}
void chli(Tree* &node,string zhong,string hou)
{
if(zhong == ""||hou == "") return;
char nodec = hou[hou.size()-1];
node = new(Tree);
node->date = nodec;
string zleft="",zright="";
bool y = false;
for(int i = 0;ileftTree,zleft,hleft);
chli(node->rightTree,zright,hright);
}
void print(Tree* node)
{
if(node == NULL) return;
else
{
cout<date;
print(node->leftTree);
print(node->rightTree);
}
}
int main(){
string zhong,hou;
Tree *head;
input(zhong,hou);
chli(head,zhong,hou);
print(head);
return 0;
}
4.P1827 [USACO3.4] 美国血统 American Heritage
#include
#include
using namespace std;
struct Tree;
struct Tree{
char date;
Tree* leftTree=NULL,*rightTree=NULL;
};
void input(string &zhong,string &hou)
{
cin>>zhong>>hou;
}
void chli(Tree* &node,string zhong,string hou)
{
if(zhong == ""||hou == "") return;
char nodec = hou[0];
node = new(Tree);
node->date = nodec;
string zleft="",zright="";
bool y = false;
for(int i = 0;ileftTree,zleft,hleft);
chli(node->rightTree,zright,hright);
}
void print(Tree* node)
{
if(node == NULL) return;
else
{
print(node->leftTree);
print(node->rightTree);
cout<date;
}
}
int main(){
string zhong,hou;
Tree *head;
input(zhong,hou);
chli(head,zhong,hou);
print(head);
return 0;
}
5.马走日
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int xj[] = {1,2,2,-1,1,-1,-2,-2},
yj[] = {2,1,-1,2,-2,-2,1,-1};
bool qipan[9][9];
int T,n,m,cx,cy;
int ans;
void input();
void print();
void dfs(int x,int y);
void run();
bool pd();
int main()
{
run();
return 0;
}
void input()
{
memset(qipan,0,sizeof(qipan));
ans = 0;
cin>>n>>m>>cx>>cy;
}
void print()
{
cout<>T;
while(T--)
{
input();
dfs(cx,cy);
print();
}
}
bool pd(){
for(int i = 0;i=n||y>=m||qipan[x][y]) return;
else
{
qipan[x][y] = true;
for(int i = 0;i<8;i++){
dfs(x+xj[i],y+yj[i]);
}
qipan[x][y] = false;
}
}
}