zoukankan      html  css  js  c++  java
  • 【记忆化搜索】游荡的奶牛

    [luogu1535]游荡的奶牛

    题目描述

    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,含义如题中所述。

    输入输出样例

    输入样例#1:
    4 5 6
    ...*.
    ...*.
    .....
    .....
    1 3 1 5
    输出样例#1:
    1
    

    说明

    样例说明:

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

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

    我一开始剪枝DFS T了一个点,后来用剪枝+DFS+记忆化把那个点给A了,结果T了另外一个点……

    改成纯记忆化才A掉……RP low

    代码(没有一点技术含量TAT)

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<algorithm>
    //#include<cmath>
    
    using namespace std;
    const int INF = 9999999;
    #define LL long long
    
    inline int read(){
    	int x=0,f=1;char c=getchar();
    	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    	return x*f;
    }
    int N,M,T;
    bool Map[101][101];
    int f[101][101][17];
    char c;
    int R1,R2,C1,C2;
    int ans;
    bool vis[101][101][17];
    int dfs(int a,int b,int ex,int ey,int t){
        if(t+abs(ex-a)+abs(ey-b)>T) return f[a][b][t]=0;
    	if(vis[a][b][t]) return f[a][b][t];
    	vis[a][b][t]=true;
    	if(a==ex&&b==ey&&t==T){
    		f[a][b][t]=1;
    		return 1;
    	}
    	if(Map[a+1][b]&&a+1<=N) f[a][b][t]+=dfs(a+1,b,ex,ey,t+1);
    	if(Map[a-1][b]&&a-1>=1) f[a][b][t]+=dfs(a-1,b,ex,ey,t+1);
    	if(Map[a][b+1]&&b+1<=M) f[a][b][t]+=dfs(a,b+1,ex,ey,t+1);
    	if(Map[a][b-1]&&b-1>=1) f[a][b][t]+=dfs(a,b-1,ex,ey,t+1);
    	return f[a][b][t];
    }
    int main(){
    	//freopen(".in","r",stdin);
    	//freopen(".out","w",stdout);
    	memset(Map,true,sizeof(Map));
    	N=read(),M=read(),T=read();
    	for(int i=1;i<=N;i++){
    		for(int j=1;j<=M;j++){
    			cin>>c;
    			if(c=='*') Map[i][j]=false;
    		}
    	}
    	R1=read(),C1=read(),R2=read(),C2=read();
    	printf("%d
    ",dfs(R1,C1,R2,C2,0));
    	return 0;
    }
  • 相关阅读:
    RecyclerView 数据刷新的几种方式 局部刷新 notify MD
    【图片】批量获取几万张图片
    RV BaseRecyclerViewAdapterHelper 总结 MD
    RecyclerView.ItemDecoration 间隔线
    Kotlin【简介】Android开发 配置 扩展
    Kotlin 特性 语法糖 优势 扩展 高阶 MD
    一个十分简洁实用的MD风格的UI主框架
    折叠伸缩工具栏 CollapsingToolbarLayout
    FloatingActionButton FAB 悬浮按钮
    Glide Picasso Fresco UIL 图片框架 缓存 MD
  • 原文地址:https://www.cnblogs.com/wxjor/p/6959071.html
Copyright © 2011-2022 走看看