求大神指出错误
查看原帖
求大神指出错误
828195
china178楼主2023/2/23 19:54

想用连通块的方法解

#include <bits/stdc++.h>
using namespace std;
char mp[105][105];
int dx[4]={-1, 1, 0, 0}, dy[4]={0, 0, -1, 1};
int ans=0, r, c, k;
void dfs(int x, int y, int deep){
	if(deep==k){
		ans++;
		return ;
	}
	for(int i=0; i<4; i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(xx<1 || yy<1 || xx>r || yy>r || mp[xx][yy]=='#')
		    continue;
		else{
			mp[xx][yy] = '#';
			dfs(xx, yy, deep+1);
			mp[xx][yy] = '.';
		}
	}
}
int main()
{
	cin >> r >> c >> k;
	for(int i=1; i<=r; i++){
		for(int j=1; j<=c; j++){
			cin >> mp[i][j];
		}
	}
	for(int i=1; i<=r; i++){
		for(int j=1; j<=c; j++){
			if(mp[i][j]=='.'){
				mp[i][j] = '#';
				dfs(i, j, 1);
			}
		}
	}
	cout << ans;
	return 0;
}
2023/2/23 19:54
加载中...