您的位置:首页 > 其它

[Usaco2008 Mar]Cow Travelling游荡的奶牛

2015-09-05 09:16 295 查看
记录这题主要因为我是16班的嘛。。。(题号1616)

对于苟蒻来说这道dp是极好的(hhhhh)

1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 928  Solved: 505

[Submit][Status][Discuss]

Description

奶牛们在被划分成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)的奶牛可能经过的路径有哪些。

Input

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

Output

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

Sample Input

4 5 6

...*.

...*.

.....

.....

1 3 1 5

输入说明:

草地被划分成4行5列,奶牛在6秒内从第1行第3列走到了第1行第5列。

Sample Output

1

奶牛在6秒内从(1,3)走到(1,5)的方法只有一种(绕过她面前的树)。

因为草地小,时间少,所以f[i][j][k]表示到达(i,j)用时k秒的方案数

于是f[i][j][k] = sum{f[ii][jj][k-1]}

#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cstring>

#include<string>

#include<queue>

#include<stack>

#include<vector>

#include<cstdlib>

#include<map>

#include<cmath>

#include<set>

using namespace std;

const int maxn = 110;

const int dx[4] = {1,0,-1,0};

const int dy[4] = {0,1,0,-1};

int f[maxn][maxn][20],n,m,i,j,r1,c1,r2,c2,t;

char c[maxn];

bool tree[maxn][maxn];

int main()

{
#ifndef ONLINE_JUDGE
#ifndef YZY
 freopen(".in","r",stdin);
 freopen(".out","w",stdout);
#else
 freopen("yzy.txt","r",stdin);
#endif
#endif

cin >> n >> m >> t;
memset(tree,false,sizeof(tree));
for (i = 0; i < n; i++)
{
scanf("%s",c);
for (j = 0; j < m; j++)
 if (c[j] == '*') tree[i + 1][j + 1] = true;
}
memset(f,0,sizeof(f));
cin >> r1 >> c1 >> r2 >> c2;
f[r1][c1][0] = 1;
for (int k = 1; k <= t; k++)
for (i = 1; i <= n; i++)
  for (j = 1; j <= m; j++)
   
{
   
if (tree[i][j]) continue;
   
for (int l = 0; l < 4; l++)
   
{
   
int xx = i + dx[l];
   
int yy = j + dy[l];
   
if (xx < 1 || xx > n || yy < 1 || yy > m || tree[xx][yy]) continue;
   
f[i][j][k] += f[xx][yy][k - 1];
   
}
   
}
cout << f[r2][c2][t];
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: