洛谷 P1535 游荡的奶牛
题目描述
Searching for the very best grass, the cows are travelling about the pasture which is represented as a grid with N rows and M columns (2 <= N <= 100; 2 <= M <= 100). Keen observer Farmer John has recorded Bessie's position as (R1, C1) at a certain time and then as (R2, C2) exactly T (0 < T <= 15) seconds later. He's not sure if she passed through (R2, C2) before T seconds, but he knows she is there at time T.
FJ wants a program that uses this information to calculate an integer S that is the number of ways a cow can go from (R1, C1) to (R2, C2) exactly in T seconds. Every second, a cow can travel from any position to a vertically or horizontally neighboring position in the pasture each second (no resting for the cows). Of course, the pasture has trees through which no cow can travel.
Given a map with '.'s for open pasture space and '*' for trees, calculate the number of possible ways to travel from (R1, C1) to (R2, C2) in T seconds.
奶牛们在被划分成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,含义如题中所述。
输入输出样例
说明
样例说明:
草地被划分成4 行5 列,奶牛在6 秒内从第1 行第3 列走到了第1 行第5 列。
奶牛在6 秒内从(1,3)走到(1,5)的方法只有一种(绕过她面前的树)
思路:dfs+剪枝
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 110
using namespace std;
int n,m,s,ans;
int sx,sy,tx,ty;
int map[MAXN][MAXN];
int dx[]={,-,,};
int dy[]={,,,-};
void dfs(int x,int y,int tot){
if(tot>s) return ;
if(x<||x>n||y<||y>m||map[x][y]) return ;
if(abs(x-tx)+abs(y-ty)>s-tot) return ;
if(x==tx&&y==ty&&s==tot){
ans++;
return ;
}
dfs(x+,y,tot+);
dfs(x-,y,tot+);
dfs(x,y+,tot+);
dfs(x,y-,tot+);
}
int main(){
scanf("%d%d%d",&n,&m,&s);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
char s;cin>>s;
if(s=='.') map[i][j]=;
else map[i][j]=;
}
scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
dfs(sx,sy,);
cout<<ans;
}
思路:记忆化搜索。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 110
using namespace std;
int n,m,s,ans;
int sx,sy,tx,ty;
int map[MAXN][MAXN],f[MAXN][MAXN][];
int dx[]={,-,,};
int dy[]={,,,-};
void dfs(int x,int y,int tot){
if(f[x][y][tot]) return ;
if(tot>s) return ;
if(abs(x-tx)+abs(y-ty)>s-tot) return ;
if(x==tx&&y==ty&&tot==s){
f[x][y][tot]=;
return ;
}
for(int i=;i<;i++){
int cx=x+dx[i];
int cy=y+dy[i];
if(cx>=&&cx<=n&&cy>=&&cy<=m&&!map[cx][cy])
dfs(cx,cy,tot+);
f[x][y][tot]+=f[cx][cy][tot+];
}
}
int main(){
scanf("%d%d%d",&n,&m,&s);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
char s;cin>>s;
if(s=='.') map[i][j]=;
else map[i][j]=;
}
scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
dfs(sx,sy,);
cout<<f[sx][sy][];
}
洛谷 P1535 游荡的奶牛的更多相关文章
- 洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication【最小割】分析+题解代码
洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication[最小割]分析+题解代码 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流. ...
- P1535 游荡的奶牛
P1535 游荡的奶牛 题目描述 Searching for the very best grass, the cows are travelling about the pasture which ...
- 洛谷1578:[WC2002]奶牛浴场——题解
https://www.luogu.org/problemnew/show/P1578#sub 由于John建造了牛场围栏,激起了奶牛的愤怒,奶牛的产奶量急剧减少.为了讨好奶牛,John决定在牛场中建 ...
- 洛谷1345 [Usaco5.4]奶牛的电信
题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...
- POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...
- 洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication
题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...
- 洛谷 P2915 【[USACO08NOV]奶牛混合起来Mixed Up Cows】
类似于n皇后的思想,只要把dfs表示放置情况的数字压缩成一个整数,就能实现记忆化搜索了. 一些有关集合的操作: {i}在集合S内:S&(1<<i)==1: 将{i}加入集合S:S= ...
- 洛谷P13445 [USACO5.4]奶牛的电信Telecowmunication(网络流)
题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...
- 洛谷——P1345 [USACO5.4]奶牛的电信Telecowmunication
P1345 [USACO5.4]奶牛的电信Telecowmunication 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮 ...
随机推荐
- Elias-Fano编码算法——倒排索引压缩用,本质上就是桶排序数据结构思路
Elias-Fano编码过程如下:把一组整数的最低l位连接在一起,同时把高位以严格单调增的排序划分为桶. Example: 2, 3, 5, 7, 11, 13, 24 Count in unary ...
- 0423-mysql插入语句大全
/*注意: 1.字段和值要一一对应 2.值的数据类型是字段的数据类型 3.当输入的字段是表中全部字段时,字段可以省略不写: insert into login values ('zhangsan',‘ ...
- python 46 盒模型 与盒模型布局
一:盒模型 1. 盒模型的概念 广义盒模型:文档中所有功能性及内容性标签,及文档中显示性标签 侠义盒模型:文档中以块级形式存在的标签(块级标签拥有盒模型100%特性且最常用) 盒模型组成:margi ...
- 题解 UVA10587 【Mayor's posters】
先讲一下:dalao @lisuier 发布的前一篇题解严格来讲是有错误的 比如下一组数据: 1 3 1 10 1 4 7 10 显然答案是3,然而用lisuier dalao的程序做出来的答案是2( ...
- Coursera公开课-Machine_learing:编程作业6
Support Vector Machines I have some issues to state. First, there were some bugs in original code wh ...
- Entity Framework Code First -- 延迟加载和预先加载
还是以这两个表为例子 country包含零个或多个city, 这个外键关系是我后来加上去,原来没有. 然后再用Power Tool逆向, 产生如下代码 1: using System.Componen ...
- 【Oracle】数据迁移工具(1):SQL Loader
SQL Loader是一种数据加载工具,可以把外部数据加载到Oracle数据库中.SQL Loader中的参数有很多,本文只在第一部分中列出常用参数.要想运用SQL Loader工具 ,需要我们编辑一 ...
- 推荐系统:MovivLens20M数据集解析
MovieLens 是历史最悠久的推荐系统.它由美国 Minnesota 大学计算机科学与工程学院的 GroupLens 项目组创办,是一个非商业性质的.以研究为目的的实验性站点.MovieLens ...
- node.js的querystring模块
querystring主要是用于对字符串进行操作和解析.共有四个方法:querystring.parse,querystring.stringify,querystring.escape,querys ...
- js判断数组中是否包含某个值
/** * 判断数组中是否包含某个值 * @param arr 数组 * @param str 值 * @returns {boolean} */ function contains(arr, str ...