当前位置:网站首页>记忆化搜索
记忆化搜索
2022-07-22 03:17:00 【lovesickman】
记忆化搜索
下午看有向图的最小路径可重复径覆盖看懵了
滑雪
一遍过还是舒服的,明确数组的定义和dfs的定义即可
/*Love coding and thinking!*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
const int N=110;
int a[N][N],s[N][N],n,m;
//s[i][j]表示从(i,j)开始能向外扩展多少步,避免了重复搜索,加快了搜索速度
int dx[]={
-1,0,1,0};
int dy[]={
0,1,0,-1};
int dfs(int x,int y)
{
if(s[x][y])return s[x][y];
s[x][y]=1;
for(int i=0;i<4;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(nx<1||nx>n||ny<1||ny>m)continue;
if(a[nx][ny]>=a[x][y])continue;
s[x][y]=max(s[x][y],dfs(nx,ny)+1);
}
return s[x][y];
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)scanf("%d",&a[i][j]);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(!s[i][j])
s[i][j]=dfs(i,j);//dfs返回(i,j)对应的s的值
}
int ans=-1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
ans=max(ans,s[i][j]);
}
cout<<ans;
return 0;
}
Cow Travelling S
写到一半不会写了,答案表示的状态太多了
/*Love coding and thinking!*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
const int N=110;
int n,m,k;
char a[N][N];
int sx,sy,ex,ey;
int cnt[N][N][16];//表示从(sx,sy)到(i,j)使用k步的路径总数
int dx[]={
-1,0,1,0};
int dy[]={
0,1,0,-1};
void dfs(int x,int y,int step)//控制步数
{
if(cnt[x][y])return cnt[x][y];
for(int i=0;i<4;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(nx<1||nx>n||ny<1||ny>m)continue;
if(a[nx][ny]=='*')continue;
cnt[nx][ny]
}
return cnt[x][y];
}
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf(" %c",&a[i][j]);
cin>>sx>>sy>>ex>>ey;
dfs(sx,sy,k);
cout<<cnt[ex][ey][k];
}
改动之后的代码,不应该算是记忆化搜索,就是搜索加剪枝
/*Love coding and thinking!*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
const int N=110;
int n,m,k;
char a[N][N];
int sx,sy,ex,ey;
int ans;
int dx[]={
-1,0,1,0};
int dy[]={
0,1,0,-1};
void dfs(int x,int y,int step)//控制步数
{
if(x==ex&&y==ey&&!step)ans++;
if(!step)return ;
if(abs(x-ex)+abs(y-ey)>step)return; //没有这个剪纸只能50分
for(int i=0;i<4;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(nx<1||nx>n||ny<1||ny>m)continue;
if(a[nx][ny]=='*')continue;
dfs(nx,ny,step-1);
}
}
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf(" %c",&a[i][j]);
cin>>sx>>sy>>ex>>ey;
dfs(sx,sy,k);
cout<<ans;
}
Function水题
/*Love coding and thinking!*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
const int N=30;
typedef long long ll;
ll a,b,c;
ll f[N][N][N];
ll w(int a,int b,int c)//记录所有的w[1~20][1~20][1~20]的值域
{
if(a<=0||b<=0||c<=0)return 1;
if(a>20||b>20||c>20)return w(20,20,20);
if(f[a][b][c]!=-1)return f[a][b][c];
else
{
if(a<b&&b<c)
return f[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
else
return f[a][b][c]=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
}
}
int main()
{
mem(f,-1);
// freopen("1.txt","r",stdin);
while(cin>>a>>b>>c)
{
if(a==-1&&b==-1&&c==-1)break;
printf("w(%lld, %lld, %lld) = %lld\n",a,b,c,w(a,b,c));
}
}
金字塔利用记忆化写区间dp
/*Love coding and thinking!*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <vector>
#define pb push_back
#define mem(f, x) memset(f,x,sizeof(f))
#define fo(i,a,n) for(int i=(a);i<=(n);++i)
#define fo2(i,a,n) for(int i=(a);i<(n);++i)
#define debug(x) cout<<#x<<":"<<x<<endl;
#define endl '\n'
using namespace std;
typedef long long ll;
const int N=310,mod=1e9;
int f[N][N];
int solve(int l,int r)//相当于循环dp里枚举左右端点
{
if(l>r)return 0;
if(l==r)return 1;
if(f[l][r]!=-1)return f[l][r];
for(int k=l;k<r;k+=2)
{
f[l][r]=(f[l][r]+1ll*solve(l,k)*solve(k+1,r-1))%mod;
}
return f[l][r];
}
int main()
{
string s;cin>>s;
memset(f,-1,sizeof f);//初始化记忆化数组
solve(1,n);
cout<<f[1][n];
}
边栏推荐
- 多米诺骨牌上演:三箭资本崩盘始末
- 60 open-ended test questions, recite them and get a pay rise directly
- yarn 的使用
- 为什么我们开发的系统会有并发Bug,并发Bug根源到底是什么?
- The simple use of ADB command combined with monkey is super detailed
- 社交电商:链动2+1-入口快速裂变的模式
- Understand the three mountains of JS
- Know garbage recycling
- Check for degenerate boxes检查退化框
- Albumin nanoparticles / gossypol albumin nanoparticles / lapatinib albumin nanoparticles coated with DNA and photosensitizer CE6
猜你喜欢
Two bytes, carried out by the interviewer and shared with everyone
GoldenSection
2022.7.21 special matrix compression
深度卷积和普通卷积的对比
With the advent of the meta universe era, how does soul Zhang Lu team create a new social experience?
After 3 years of on-the-job testing experience, can't the interview and testing post even get 20K? Is there such a hole?
How to deal with tough interview questions
img.shape[-2:]/len(img.shape[-2:]):GeneralizedRCNN:original_image_sizes中的 torch._assert
高中学历自学测试,21岁在深圳拿10k,付出多少只有我自己知道~
Don't look for it, it's all sorted out for you -- complete collection of SQL statements
随机推荐
Section 5 of Chapter 3: return value
广告无处不在,如何利用广告去推广自己的产品?
C language to write 99 multiplication table to realize the output of tables with different triangle shapes
ROS2自学笔记:TF坐标管理
With the advent of the meta universe era, how does soul Zhang Lu team create a new social experience?
CutNoodles
C language question bank part.1
10个自动化测试框架,测试工程师用起来
ES6 - module
High school education self-study test, I took 10K in Shenzhen at the age of 21, and only I know how much I paid~
yarn 的使用
socket通信中select函数用法
Don't look for it, it's all sorted out for you -- complete collection of SQL statements
【Error】解决:Not creating XLA devices, tf_xla_enable_xla_devices not set
Niuke 2022 summer training session I chiitoitsu solution
OPENCN学习DAY3
MySQL advanced addition, deletion, modification and query operations, including detailed explanation and operation of various queries and table design explanation
C language dynamic memory allocation
Comparison between deep convolution and ordinary convolution
包裹DNA和光敏剂Ce6的白蛋白纳米粒/棉酚白蛋白纳米粒/拉帕替尼白蛋白纳米粒