bzoj 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛(BFS)

来源:互联网 发布:网络大v颠倒是非 编辑:程序博客网 时间:2024/06/05 15:16

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

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 1293  Solved: 725
[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

Sample Output

1


因为t很小,直接广搜t次

cnt[x][y][t]表示第t秒到达点(x, y)的方案数

#include<stdio.h>#include<queue>#include<string.h>using namespace std;typedef struct{int x;int y;}Point;Point now, temp;queue<Point> q, q2;char str[105][105];int cnt[105][105][17], vis[105][105], dir[4][2] = {1,0,0,1,-1,0,0,-1};int main(void){int n, m, i, j, t, sx, sy, ex, ey;while(scanf("%d%d%d", &n, &m, &t)!=EOF){for(i=1;i<=n;i++){for(j=1;j<=m;j++)scanf(" %c", &str[i][j]);}memset(cnt, 0, sizeof(cnt));scanf("%d%d%d%d", &sx, &sy, &ex, &ey);cnt[sx][sy][0] = 1;now.x = sx, now.y = sy;q.push(now);for(i=1;i<=t;i++){memset(vis, 0, sizeof(vis));while(q.empty()==0){now = q.front();q.pop();for(j=0;j<=3;j++){temp.x = now.x+dir[j][0];temp.y = now.y+dir[j][1];if(temp.x<1 || temp.x>n || temp.y<1 || temp.y>m || str[temp.x][temp.y]=='*')continue;cnt[temp.x][temp.y][i] += cnt[now.x][now.y][i-1];if(vis[temp.x][temp.y]==0){q2.push(temp);vis[temp.x][temp.y] = 1;}}}if(i!=t){while(q2.empty()==0){q.push(q2.front());q2.pop();}}}printf("%d\n", cnt[ex][ey][t]);}return 0;}

阅读全文
1 0