[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;
}
[Usaco2008 Mar]Cow Travelling游荡的奶牛的更多相关文章
- Bzoj 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 动态规划
1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1006 Solved: ...
- BZOJ1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛
1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 762 Solved: ...
- BZOJ 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛( dp )
一道水 dp ...然后我一开始用 BFS ...结果 MLE 了... dp[ i ][ j ][ k ] 由它四个方向上的 k - 1 转移. -------------------------- ...
- [Usaco2008 Mar]Cow Travelling游荡的奶牛[简单DP]
Description 奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草.Farmer John ...
- [bzoj 1616][Usaco2008 Mar]Cow Travelling游荡的奶牛
题目描述 奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草.Farmer John在某个时刻看见 ...
- 【BZOJ】1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛(dp/-bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1616 我觉得bfs是可过的,但是交bfs上去是wa? 然后没办法看dp,原来这bfs能和dp联系在一 ...
- 【bzoj1616】[Usaco2008 Mar]Cow Travelling游荡的奶牛 bfs
题目描述 奶牛们在被划分成N行M列(2 <= N <= 100; 2 <= M <= 100)的草地上游走,试图找到整块草地中最美味的牧草.Farmer John在某个时刻看见 ...
- BZOJ 1616 [Usaco2008 Mar]Cow Travelling游荡的奶牛:dp【网格型】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1616 题意: 有一个n*m的网格. '.'表示平坦的草地,'*'表示挡路的树(不能走). ...
- bzoj 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛【dp】
写了个bfs发现MLE了... 设f[t][i][j]为在t时刻走到(i,j)的方案数,转移和bfs一样 #include<iostream> #include<cstdio> ...
随机推荐
- Java中构造代码块的使用
例子1 public class Client { { System.out.println("执行构造代码块1"); } { System.out.println("执 ...
- Linux后台命令导入MySQL语句
1.首先输入命令: mysql -u root -p 然后输入MySQL的密码会进入到MySQL的命令界面. 2.输入命令use+数据库名字: use databaseName 3.最后输入命令sou ...
- matplotlib的学习6-annotation的标注
import matplotlib.pyplot as plt import numpy as np ''' 当图线中某些特殊地方需要标注时,我们可以使用 annotation. matplotlib ...
- Java基础进阶:多态与接口重点摘要,类和接口,接口特点,接口详解,多态详解,多态中的成员访问特点,多态的好处和弊端,多态的转型,多态存在的问题,附重难点,代码实现源码,课堂笔记,课后扩展及答案
多态与接口重点摘要 接口特点: 接口用interface修饰 interface 接口名{} 类实现接口用implements表示 class 类名 implements接口名{} 接口不能实例化,可 ...
- python简单的函数应用
一个简单的函数应用,包括自定义函数,lambda函数,列表解析. 1 #!usr/bin/env python3 2 # -*- coding:utf-8 -*- 3 4 #开始定义函数 5 def ...
- idea 中在src/main/java中的xml扫描不到问题
<build> <!-- start idea 默认 不加载 java下的配置文件 --> <resources> <resource> <dir ...
- 连接数据库查询 将查询结果写入exce文件中
package com.cn.peitest.connectDatabase; import java.io.File; import java.lang.reflect.Field; import ...
- spring mvc与mybatis与maven+mysql框架整合
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- [leetcode712]204. Count Primes寻找范围内的素数
厄拉多塞筛选法,就是哈希表记录素数的倍数 public int countPrimes(int n) { /* 牛逼哄哄的厄拉多塞筛选法 就是从2开始,每找到一个素数,就把n以内的这个数的倍数排除 记 ...
- Mac电脑 Android Studio连接小米手机
1.设置>关于本机>点击5下MIUI版本>激活开发者模式 2.设置>更多设置>开发者选项>开启开发者选项>开启USB调试>开启USB安装>开启显示 ...