Daily Training: BFS && DFS (Los Valley title)

Daily Training: BFS && DFS


First, the Los Valley P1118: digital triangle


A look at the subject that this is a DFS
but the beginning TLE three points. . . . .
Read bigwigs of problem solutions and found: I go, is not that Pascal's Triangle do, you can use Pascal's Triangle pretreatment ah
so there will be the following code:

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<set>
#include<vector>
#include<map>
#include<queue>
#define INF 0x3f3f3f3f
#define ll long long
using namespace std;
int n,sum;
int f[15][15];
int num[15];
int vis[15];
int flag;
void print(){
    printf("%d",num[1]);
    for(int i=2;i<=n;i++){
        printf(" %d",num[i]);
    }
    cout<<endl;
    return ;
}
void dfs(int step,int ans){
    if(ans>sum||flag)
        return ;
    if(ans==sum&&step==n+1){
        flag=1;
        print();
        return ;
    }
    for(int i=1;i<=n;i++){
        if(vis[i]==0){
            num[step]=i;
            vis[i]=1;
            dfs(step+1,ans+i*f[n][step]);
            vis[i]=0;
        }
    }
}
int main(){
    scanf("%d%d",&n,&sum);
    f[1][1]=1;
    for(int i=2;i<=n;i++){
        for(int j=1;j<=i;j++){
            f[i][j]=f[i-1][j]+f[i-1][j-1];
        }
    }
    memset(vis,0,sizeof(vis));
    flag=0;
    dfs(1,0);
    return 0;
}


Second, Luo Valley P1434: Skiing


Is a search problem, began to feel have to use memory search , or search violence, will definitely TLE
but still do not know where to start, or less entitled to do, or else this will not, behind only the code below

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<set>
#include<vector>
#include<map>
#include<queue>
#define INF 0x3f3f3f3f
#define ll long long 
using namespace std;
int n,m;
int mp[110][110];
int dis[110][110];             //记忆化搜索
int dir[4][2]={-1,0,0,1,1,0,0,-1};
int dfs(int x,int y){
    if(dis[x][y]!=1)
        return dis[x][y];
    int b=0;
    for(int k=0;k<4;k++){
        int fx=x+dir[k][0];
        int fy=y+dir[k][1];
        if(mp[x][y]>mp[fx][fy])
            b=max(b,dfs(fx,fy));
    }
    dis[x][y]=max(dis[x][y],b+1);
    return dis[x][y];
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%d",&mp[i][j]);
            dis[i][j]=1;
        }
    }
    int maxdis=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            maxdis=max(maxdis,dfs(i,j));
        }
    }
    //getchar();
    printf("%d\n",maxdis);
    //getchar();
    return 0;
}

Third, the Los Valley P1433: eat cheese


Began to read the title, I do not know why the first thought is that BFS, is it due to the minimum distance? ?
Is a DFS, this problem does nothing start thinking, after reading the solution to a problem found that if the rate of a distance pretreatment look like it, this feels a bit unexpected. . . .

#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#include<set>
#include<vector>
#include<map>
#include<queue>
#define INF 0x3f3f3f3f
#define ll long long 
using namespace std;
int n;
double x[20],y[20];
int vis[20];
double dis[20][20];
double ans=99999999.0;
void dfs(int i,int now,double len){
    if(len>ans){
        return ;
    }
    if(i==n){
        ans=min(ans,len);
        return ;
    }
    for(int k=1;k<=n;k++){
        if(vis[k]==0){
            vis[k]=1;
            dfs(i+1,k,len+dis[now][k]);
            vis[k]=0;
        }
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%lf%lf",&x[i],&y[i]);
    }
    x[0]=0,y[0]=0;
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            dis[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
        }
    }
    memset(vis,0,sizeof(vis));
    dfs(0,0,0.0);
    //getchar();
    printf("%.2f\n",ans);
    //getchar();
    return 0;
}

Fourth, the Los Valley P1074: target-shaped Sudoku


A bit difficult, to be completed ...

Published 127 original articles · won praise 32 · views 10000 +

Guess you like

Origin blog.csdn.net/boliu147258/article/details/103450816