- 接上文,代码如下:
- #include<iostream>
- using namespace std;
- #define N 5
- #define M 5
- bool Direct;//两个方向,朝右Direct=true,朝上Direct=false
- bool Track[M+N];//存储走过的轨迹
- int count=0;//存储共有几种途径
- int main(){
- void Tracks(int n,int x,int y);//坐标化
- Tracks(0,0,0);
- cout<<count<<endl;
- return 0;
- }
- void Tracks(int n,int x,int y){
- if(n==M+N){
- for(int i=0;i<M+N;i++){
- if(Track[i])cout<<"ahead";
- else cout<<"up";
- if(i!=M+N-1)cout<<"->";
- }
- count++;
- cout<<endl;
- return ;
- }
- else{
- if(x==M){//只能朝上
- Track[n]=false;
- Tracks(n+1,x,y+1);
- }
- else if(y==N){//只能朝右
- Track[n]=true;
- Tracks(n+1,x+1,y);
- }
- else{
- Track[n]=true;
- Tracks(n+1,x+1,y);
- Track[n]=false;
- Tracks(n+1,x,y+1);
- }
- }
- }