当前位置:网站首页>Mnemonic search
Mnemonic search
2022-07-22 16:01:00 【lovesickman】
Memory search
In the afternoon, I was confused to see the minimum path repeatable path coverage of the directed graph
skiing
It's comfortable to go through it again , Define the array and dfs The definition of
/*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] From (i,j) How many steps can you expand outward at the beginning , Avoid repeated searches , Faster search
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 return (i,j) Corresponding s Value
}
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
I can't write in half , The answer indicates too many states
/*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];// From (sx,sy) To (i,j) Use k Total number of paths in steps
int dx[]={
-1,0,1,0};
int dy[]={
0,1,0,-1};
void dfs(int x,int y,int step)// Control the number of steps
{
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];
}
The changed code , It shouldn't be a memory search , Search and pruning
/*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)// Control the number of steps
{
if(x==ex&&y==ey&&!step)ans++;
if(!step)return ;
if(abs(x-ex)+abs(y-ey)>step)return; // Without this paper cutting can only 50 branch
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 Water problem
/*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)// Record all w[1~20][1~20][1~20] Range of values
{
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));
}
}
Pyramids use memory to write intervals 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)// It's equivalent to a cycle dp Enumerate the left and right endpoints
{
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);// Initialize the memorized array
solve(1,n);
cout<<f[1][n];
}
边栏推荐
猜你喜欢
img.shape[-2:]/len(img.shape[-2:]):GeneralizedRCNN:original_ image_ Torch in sizes_ assert
Px4 module design Xi: built in framework
微服务真的不挑数据库吗?如何选择?
RS+BCH级联编译码误码率性能matlab仿真
Metauniverse enabling scenario Finance: a new track for the competitive development of commercial banks
分布式事务,原理简单,写起来全是坑
Web3 社交协议垄断性与灵魂绑定代币
NB-IOT可以应用在哪些领域
PPy-HSA导电聚合物聚吡咯PPy物质BSA白米纳米粒/白蛋白包覆纳米脂质载体BSA NLC的研究制备
DC-4-靶场实践
随机推荐
SCM peripheral devices learning strategies, small Bai must see
10 automated test frameworks for test engineers
LastWordLen
LastWordLen
PPy HSA conductive polymer polypyrrole PPy material BSA white rice nanoparticles / albumin coated nano lipid carrier BSA NLC
浅拷贝,深拷贝(实现方式)
分布式事务,原理简单,写起来全是坑
Program environment and pretreatment
ENVI_IDL: 批量制作专题地图
Jd-h5 development
Check for degenerate boxes
After 3 years of on-the-job testing experience, can't the interview and testing post even get 20K? Is there such a hole?
JS数组对象排序(es6)
Pull daily activities, use cloud functions to send wechat applet subscription messages
In what fields can nb-iot be applied
18. What is the persistence mechanism of redis? Respective advantages and disadvantages?
RS编码译码误码率性能matlab仿真
多米诺骨牌上演:三箭资本崩盘始末
Mask RCNN源码详解
Ctfshow Mengxin lower