Luo Gu: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();
}

おすすめ

転載: blog.csdn.net/weixin_42721412/article/details/108477780