P3654 First Step (ファーストステップ)

代码设计思路

基础思路:以每一个点为起点,分别向右,向下搜索连续长度为k 的片段。
递归边界:step > k,即已经有了长度为k 的片段,直接输出答案
注意*:1. 书写dfs时不能写成

if(a[i][j]){
	dfs(step,x + 1, y );
	dfs(step,x , y + 1);
	}

因为,如果每次搜索只能有一个方向,而不存在先向右后向下的情况。
2. 如果k == 1,即只有一个人的时候,所求出的答案 要除以2,因为向右和向下都满足情况,会重复计算


#include <iostream>
#include <cstdio>
#include <numeric>
#include <functional>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <bitset>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int maxn = 1e6 + 3;
const int N = 1e5 + 5;
int r,c,k;
int a[105][105];
int ans;
int dy[2] = { 0,1 };
int dx[2] = { 1,0 };
void dfs(int step,int x,int y,int i) {
	if (step > k) {
		ans++;
		return;
	}
	if (x > r || y > c|| a[x][y] == 0) return;
	dfs(step + 1, x + dx[i], y + dy[i], i);
	return;
	
}
int main() {
	cin >> r >> c >> k;
	for (int i = 1; i <= r; ++i) {
		for (int j = 1; j <= c; ++j) {
			char c;
			cin >> c;
			if (c == '.') a[i][j] = 1;
			else a[i][j] = 0;
			//cout << a[i][j] << ' ';
		}
		//cout << endl;
	}
	for (int i = 1; i <= r; ++i) {
		for (int j = 1; j <= c; ++j) {
			if (a[i][j])
			{
				for(int k = 0; k <= 1; k++)
					dfs(1, i, j,k);
			}
		}
	}
	if (k == 1)
		ans /= 2;
	cout << ans;
	
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值