zoukankan      html  css  js  c++  java
  • [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
    ", f[Tx][Ty][t]);   
        return 0;
    }
    
  • 相关阅读:
    MVP模式与MVVM模式
    webpack的配置处理
    leetcode 287 Find the Duplicate Number
    leetcode 152 Maximum Product Subarray
    leetcode 76 Minimum Window Substring
    感知器算法初探
    leetcode 179 Largest Number
    leetcode 33 Search in Rotated Sorted Array
    leetcode 334 Increasing Triplet Subsequence
    朴素贝叶斯分类器初探
  • 原文地址:https://www.cnblogs.com/akura/p/10853204.html
Copyright © 2011-2022 走看看