求助站外题
  • 板块灌水区
  • 楼主zhangchunmeng
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/11/23 21:28
  • 上次更新2024/11/23 23:33:59
查看原帖
求助站外题
882137
zhangchunmeng楼主2024/11/23 21:28

题目


#include<bits/stdc++.h>
using namespace std;
int n,cnt;
int fx[]={0,1,0,-1};
int fy[]={1,0,-1,0};
bool a[10][10];
int pos[100][3];
void print(int ct)
{
	cout<<++cnt<<":";
	for(int i=1;i<=ct;i++)
	{
		cout<<pos[i][1]<<","<<pos[i][2]; 
		if(i!=ct) cout<<"->"; 
	}
	cout<<endl;
	
}
void dfs(int x,int y,int ct)
{
	pos[ct][1]=x;
	pos[ct][2]=y;
	if(x==n&&y==n)
	{
		print(ct);
		return ;
	}
	for(int i=0;i<4;i++)
	{
		int tx=x+fx[i];
		int ty=y+fy[i];
		if(tx>=1&&tx<=n&&ty>=1&&ty<=n&&a[tx][ty]==false)
		{
			a[tx][ty]=true;
			dfs(tx,ty,ct+1);
			a[tx][ty]=false;
		}
	}
	
}
int main()
{
	cin>>n;
	dfs(1,1,1);
	return 0;
}

样例没过。有没有大佬帮我看一下

2024/11/23 21:28
加载中...