P1535 [USACO08MAR] Cow Travelling S

//URL:   
/*

*/
/*
4 5 6
. . . * .
. . . * .
. . . . .
. . . . . 
1 3 1 5

1
*/

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string.h>
//#include<queue>
//#include<vector>
//#include<bits/stdc++.h>
#define ll long long
#define ddd printf("-----------------------\n");
using namespace std;
const int maxn=1e2+10;
const int mod=998244353;
const int inf=0x3f3f3f3f;

char ch;
int mp[maxn][maxn];
int n,m,r1,r2,c1,c2,lim,ans;
int dx[5]={0,0,1,0,-1};
int dy[5]={0,-1,0,1,0};

void dfs(int r1,int c1,int lim)
{
    if(r1==r2&&c1==c2&&lim==0) {ans++; return;}
    if(lim==0||lim<abs(r2-r1)+abs(c2-c1)) return;
    
    for(int i=1;i<=4;i++)
    {
        int tx=r1+dx[i],ty=c1+dy[i];
        if(mp[tx][ty]==1&&tx>=1&&tx<=n&&ty>=1&&ty<=m)
            dfs(tx,ty,lim-1);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m>>lim;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            cin>>ch;
            if(ch=='.') mp[i][j]=1;
            else mp[i][j]=0;
        }
    
    cin>>r1>>c1>>r2>>c2;
            
    dfs(r1,c1,lim);
    
    cout<<ans<<'\n';
    
    return 0;
}

 

posted @ 2023-10-24 05:09  JMXZ  阅读(8)  评论(0)    收藏  举报