[Usaco2008 Mar]Cow Travelling游荡的奶牛

题目描述

奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草。Farmer John在某个时刻看见贝茜在位置 (R1, C1),恰好T (0 < T <= 15)秒后,FJ又在位置(R2, C2)与贝茜撞了正着。 FJ并不知道在这T秒内贝茜是否曾经到过(R2, C2),他能确定的只是,现在贝茜在那里。 设S为奶牛在T秒内从(R1, C1)走到(R2, C2)所能选择的路径总数,FJ希望有一个程序来帮他计算这个值。每一秒内,奶牛会水平或垂直地移动1单位距离(奶牛总是在移动,不会在某秒内停在它上一秒所在的点)。草地上的某些地方有树,自然,奶牛不能走到树所在的位置,也不会走出草地。 现在你拿到了一张整块草地的地形图,其中'.'表示平坦的草地,'*'表示挡路的树。你的任务是计算出,一头在T秒内从(R1, C1)移动到(R2, C2)的奶牛可能经过的路径有哪些。

输入格式

第1行: 3个用空格隔开的整数:N,M,T

第2..N+1行: 第i+1行为M个连续的字符,描述了草地第i行各点的情况,保证 字符是'.'和''中的一个 第N+2行: 4个用空格隔开的整数:R1,C1,R2,以及C2

输出格式

第1行: 输出S,含义如题中所述


*典型的广搜题

设f(x,y,t)表示t秒时有多少条路径可以到达坐标(x,y),每次扩展时更新即可。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#define maxn 110
#define maxt 20
using namespace std;
const int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  
struct node{
    int x, y, step;
    node(){}
    node(int _x, int _y, int _step){
        x = _x, y = _y, step = _step;
    }
};
queue<node> q;
  
bool mmap[maxn][maxn], vis[maxn][maxn][maxt];
int f[maxn][maxn][maxt];
int n, m, t;
int sx, sy, Tx, Ty;
  
bool check(const int &x, const int &y){
    return 1 <= x && x <= n && 1 <= y && y <= m;
}
  
inline void bfs(){
    q.push(node(sx, sy, 0));
    f[sx][sy][0] = 1;
    vis[sx][sy][0] = true;
    while(q.size()){
        node now = q.front();
        q.pop();
        if(now.step >= t) break;
        int x = now.x, y = now.y;
        for(register int i = 0; i < 4; i++){
            int tx = x + dir[i][0];
            int ty = y + dir[i][1];
            if(mmap[tx][ty] || !check(tx, ty)) continue;
            f[tx][ty][now.step + 1] += f[x][y][now.step];
            if(!vis[tx][ty][now.step + 1]){
                q.push(node(tx, ty, now.step + 1));
                vis[tx][ty][now.step + 1] = true;
            }
        }
    }
}
  
int main(){
    scanf("%d %d %d", &n, &m, &t);
    for(int i = 1; i <= n; i++){
        getchar();
        for(int j = 1; j <= m; j++){
            mmap[i][j] = (getchar() == '.' ? false : true);
        }
    }
    scanf("%d %d %d %d", &sx, &sy, &Tx, &Ty);
    bfs();
    printf("%d\n", f[Tx][Ty][t]);   
    return 0;
}

转载于:https://www.cnblogs.com/akura/p/10853204.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值