璟雯院

珺璟如晔,雯华若锦

用户工具

站点工具


icpc:problems:usaco21dec_walking_home_b
problems
名称Walking Home B
题目编号USACO21DEC_B3
题目链接usaco.org/…
来源USACO
算法分类DFS
难易程度容易

Walking Home B

想法

基本的搜索,但是要加一个剪枝,如果到已经了n次,但是不在第一行或者第一列的情况就可以直接剪掉。

代码实现

#include<cstdio>
#include<cstring>
 
int n, k, ans;
char map[100][100];
int x_turn[2]={1, 0};
int y_turn[2]={0, 1};
 
void DFS(int x, int y, int c, int turn)
{
	if(c == k && x != n && y != n)return ;
	if(x == n && y == n)
	{
		ans ++;
		return ;
	}
 
	if(map[x + x_turn[turn]][y + y_turn[turn]] == '.')
	{
		DFS(x+x_turn[turn], y+y_turn[turn], c, turn);
	}
	if(c < k && (x != 1 || y != 1) && map[x + x_turn[!turn]][y + y_turn[!turn]] == '.')
	{
		DFS(x+x_turn[!turn], y+y_turn[!turn], c+1, !turn);
	}
 
	return ;
}
 
int main()
{
	//freopen("in","r",stdin);
	int T;
	scanf("%d", &T);
	while(T--)
	{
		ans = 0;
		memset(map, 0 ,sizeof(map));
		scanf("%d %d\n", &n, &k);
		for(int i=1; i<=n; ++i)
			scanf("%s\n", map[i]+1);
 
		DFS(1, 1, 0, 0);
		DFS(1, 1, 0, 1);
 
		printf("%d\n", ans);
	}
 
	return 0;
}
/app/www/public/data/pages/icpc/problems/usaco21dec_walking_home_b.txt · 最后更改: 2023/09/30 13:35 由 温婕莺