HDU/HDOJ 2563 统计问题 回溯发DFS

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2563

深搜回溯超时,于是可耻的打了个表,0MS AC了。。

 

#include<iostream>

#include<cstdio>

#include<cmath>

#include<fstream>

using namespace std;

int ans[21]={0,3,7,17,41,99,239,577,1393,3363,8119,19601,47321,114243,275807,665857,1607521,3880899,9369319,22619537,54608393};

int main()

{

	int c;

	cin>>c;

	while(c--)

	{

		int n;

		cin>>n;

		cout<<ans[n]<<endl;

	}

	return 0;

}


看一下我深搜的思路吧,很简单的往三个方向搜,做标记:

 

 

#include<iostream>

#include<cstdio>

#include<cmath>

#include<fstream>

using namespace std;

bool maze[25][50];

int n,sum;

int dir[3][2]={0,1,0,-1,-1,0};

void dfs(int x,int y,int step)

{

	if(step==n)sum++;

	else {

		

		for(int i=0;i<3;i++)

		{

			int newx=x+dir[i][0];

			int newy=y+dir[i][1];

			if(maze[newx][newy])continue;

			maze[newx][newy]=1;

			dfs(newx,newy,step+1);

			maze[newx][newy]=0;

		}

	}

	

}

int main()

{

	int c;

	cin>>c;

	int ans[25];

	ans[0]=0;

	for(n=1;n<=20;n++)

	{

		memset(maze,0,sizeof(maze));

		maze[20][20]=1;

		sum=0;

		dfs(20,20,0);

		ans[n]=sum;

	}

	ofstream fout;

	fout.open("file1.txt");

	for(int i=1;i<=20;i++)fout<<ans[i]<<" ";

	/*

while(c--)

	{

		cin>>n;

		cout<<ans[n]<<endl;

	}*/

	return 0;

}


 

 

你可能感兴趣的:(HDU)