10分求助
  • 板块题目总版
  • 楼主Ace_Maochang
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/9/9 10:07
  • 上次更新2023/11/2 22:13:13
查看原帖
10分求助
707144
Ace_Maochang楼主2023/9/9 10:07

题目:P1535[USACO08MAR]Cow Travelling S

#include<bits/stdc++.h>
using namespace std;
int dx[4]={1,-1,0,0},dy[4]={0,0,1,-1};
int t,cnt,r1,c1,r2,c2,n,m;
bool s[101][101];
void dfs(int t,int x,int y){
	if(x==r2&&y==c2&&t==0){
		cnt++;
	}
	 if(!t||abs(x-r2)+abs(y-r2)>t)return;
	for(int i=0;i<4;i++){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(xx>=1&&yy>=1&&xx<=n&&yy<=m&&(s[xx][yy])){
			dfs(t-1,xx,yy);
		}
	}
}
char temp;

int main(){
	cin>>n>>m>>t;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>temp;
			if(temp=='.')	s[i][j]=true;
			else s[i][j]=false;
		}
	}
	cin>>r1>>c1>>r2>>c2;
	dfs(t,r1,c1);
	cout<<cnt;
	return 0;
}

2023/9/9 10:07
加载中...