[Usaco2008 Mar]Cow Travelling游荡的奶牛

 

# 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)的奶牛可能经过的路径有哪些。

 

# Format

## Input
第1行: 3个用空格隔开的整数:N,M,T

第2..N+1行: 第i+1行为M个连续的字符,描述了草地第i行各点的情况,保证 字符是'.'和'*'中的一个

第N+2行: 4个用空格隔开的整数:R1,C1,R2,以及C2

## Output
输出S,含义如题中所述

# Samples

```input1
4 5 6
...*.
...*.
.....
.....
1 3 1 5
```

```output1
1
```

 Sol1:最SB的,拿 了30分

#include <cstdio>
int map[200][200] , dx[] = {1 , 0 , -1 , 0} , dy[] = {0 , 1 , 0 , -1} , f[20][110][110];
int qx[200000] , qy[200000] , qt[200000] , l , r;
char str[110];
int main()
{
    int n , m , T , i , j , r1 , c1 , r2 , c2 , x , y , t;
    scanf("%d%d%d" , &n , &m , &T);
    for(i = 1 ; i <= n ; i ++ )
    {
        scanf("%s" , str + 1);
        for(j = 1 ; j <= m ; j ++ )
            if(str[j] == '.')
                map[i][j] = 1;
    }
    scanf("%d%d%d%d" , &r1 , &c1 , &r2 , &c2);
    l=r=1;
    qx[1] = r1 , qy[1] = c1 , qt[1] = 0;
    f[0][r1][c1] = 1;
    int ans=0;
    while(l <= r)
    {
        x = qx[l] , y = qy[l] , t = qt[l];
      
        if(t > T) break;
        for(i = 0 ; i < 4 ; i ++ )
        {
            if(map[x + dx[i]][y + dy[i]])
            {
               
                    r ++ ;
                    qx[r] = x + dx[i];
                    qy[r] = y + dy[i];
                    qt[r] = t + 1;
                    if(qx[r]==r2&&qy[r]==c2&&qt[r]==T)
                        ans++;
               
            }
        }
        l++;
    }
    printf("%d\n" , ans);
    return 0;
}

  

 

Sol2:数组开大,然后加些cut,90分

#include <bits/stdc++.h>
using namespace std;
int mapp[200][200] , dx[] = {1 , 0 , -1 , 0} , dy[] = {0 , 1 , 0 , -1} , f[20][110][110];
int qx[62200000] , qy[62200000] , qt[62200000] , l , r;
char str[110];
int main()
{
//	freopen("d1.out","w",stdout);
    int n , m , T , i , j , r1 , c1 , r2 , c2 , x , y , t;
    scanf("%d%d%d" , &n , &m , &T);
    for(i = 1 ; i <= n ; i ++ )
    {
        scanf("%s" , str + 1);
        for(j = 1 ; j <= m ; j ++ )
            if(str[j] == '.')
                mapp[i][j] = 1;
    }
    scanf("%d%d%d%d" , &r1 , &c1 , &r2 , &c2);
    l=r=1;
    qx[1] = r1 , qy[1] = c1 , qt[1] = 0;
    f[0][r1][c1] = 1;
    int ans=0;
    while(l <= r)
    {
        x = qx[l] , y = qy[l] , t = qt[l];
        if(t > T) break;
        if (abs(r2-x)+abs(c2-y)>T-t)
           {
           	l++;continue;
		   }
        for(i = 0 ; i < 4 ; i ++ )
        {
            if(mapp[x + dx[i]][y + dy[i]])
          
            {
                    r ++ ;
                    f[t + 1][x + dx[i]][y + dy[i]]=1;
                    qx[r] = x + dx[i];
                    qy[r] = y + dy[i];
                    qt[r] = t + 1;
                  //  cout<<r<<"  "<<qx[r]<<" "<<qy[r]<<"  "<<qt[r]<<endl;
                    if(qx[r]==r2&&qy[r]==c2&&qt[r]==T)
                        ans++;
               
            }
        }
        l++;
    }
    printf("%d\n" , ans);
    return 0;
}

  

SOL2:控制下结点进队列

 

#include <cstdio>
int map[200][200] , dx[] = {1 , 0 , -1 , 0} , dy[] = {0 , 1 , 0 , -1} , f[20][110][110];
int qx[200000] , qy[200000] , qt[200000] , l , r;
char str[110];
int main()
{
    int n , m , T , i , j , r1 , c1 , r2 , c2 , x , y , t;
    scanf("%d%d%d" , &n , &m , &T);
    for(i = 1 ; i <= n ; i ++ )
    {
        scanf("%s" , str + 1);
        for(j = 1 ; j <= m ; j ++ )
            if(str[j] == '.')
                map[i][j] = 1;
    }
    scanf("%d%d%d%d" , &r1 , &c1 , &r2 , &c2);
    l=r=1;
    qx[1] = r1 , qy[1] = c1 , qt[1] = 0;
    f[0][r1][c1] = 1;
    while(l <= r)
    {
        x = qx[l] , y = qy[l] , t = qt[l];
      
        if(t > T) break;
        for(i = 0 ; i < 4 ; i ++ )
        {
            if(map[x + dx[i]][y + dy[i]])
            {
                if(!f[t + 1][x + dx[i]][y + dy[i]])
//如果这个状态从前没有加入过队列 { r ++ ; qx[r] = x + dx[i]; qy[r] = y + dy[i]; qt[r] = t + 1; } f[t + 1][x + dx[i]][y + dy[i]] += f[t][x][y]; } } l++; } printf("%d\n" , f[T][r2][c2]); return 0; }

 

 

  

 Sol3:

Dp

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int flg[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,m,t,X1,X2,Y1,Y2,f[105][105][15];
char ch[105][105],Now[105];

int main(){

    scanf("%d%d%d",&n,&m,&t);gets(Now);
    memset(ch,'*',sizeof(ch));
    for(int i=1;i<=n;i++){
        gets(Now+1);
        for(int j=1;j<=m;j++) ch[i][j]=Now[j];
    }
    scanf("%d%d%d%d",&X1,&Y1,&X2,&Y2);
    f[X1][Y1][0]=1;
    for(int tim=1;tim<=t;tim++){
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        if(ch[i][j]=='.') 
		   f[i][j][tim]=f[i-1][j][tim-1]+f[i][j-1][tim-1]+f[i+1][j][tim-1]+f[i][j+1][tim-1];
		else f[i][j][tim]=0;

    }
    printf("%d\n",f[X2][Y2][t]);
    return 0;
}

  

 

记忆化

#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(){
	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;
}

  

posted @ 2022-06-09 22:05  我微笑不代表我快乐  阅读(38)  评论(0编辑  收藏  举报