欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

洛谷:P1259 黑白棋子的移动(普及/提高-,回溯,递归)

程序员文章站 2022-07-13 13:48:03
...

题目:

洛谷:P1259 黑白棋子的移动(普及/提高-,回溯,递归)

分析:既然给出了移动过程,那么题目就简单了。就是回溯啊。

代码:

#include<bits/stdc++.h>
using namespace std;
string s;
int m;
void init()
{
 string s1(m,'o');
 string s2(m,'*');
 s=s1+s2;
 s+='-';s+='-';
} 
void f(int x1,int x2,int y1,int y2)
{//x1是o的最后一个  x2是*的第一个  y1,y2是_所在位置 
 if(y2==9) return;
 cout<<s<<endl;
 s[x1]='-';
 s[x2]='-';
 s[y1]='o';
 s[y2]='*';
 cout<<s<<endl;
 if(x1==0) return;
 s[x1]='*';
 s[x2]='*';
 s[y1-2]='-';
 s[y2-2]='-'; 
 x1--;
 x2--;
 y1--;y1--;y2--;y2--;
 f(x1,x2,y1,y2);
}
void f2()
{
 cout<<"oooo****--";
 cout<<s.substr(10,s.length()-10)<<endl;
 cout<<"ooo--***o*";
 cout<<s.substr(10,s.length()-10)<<endl;
 cout<<"ooo*o**--*";
 cout<<s.substr(10,s.length()-10)<<endl;
 cout<<"o--*o**oo*";
 cout<<s.substr(10,s.length()-10)<<endl;
 cout<<"o*o*o*--o*";
 cout<<s.substr(10,s.length()-10)<<endl;
 cout<<"--o*o*o*o*";
 cout<<s.substr(10,s.length()-10)<<endl;
}
int main()
{
 cin>>m;
 init();
 f(m-1,m,m+m,m+m+1);
 f2();
}
相关标签: 回溯