当前位置:网站首页>Niuke monthly race 22- collect pieces of paper
Niuke monthly race 22- collect pieces of paper
2022-07-01 09:13:00 【Lovely and beautiful girl】
The question :
It's just one. n*m The matrix of ,n and m all <=20, And then there's the most 10 There's a piece of paper somewhere , Now let's take you from your initial position , The shortest distance to return to the original position after picking up all the pieces of paper .
reflection :
At that time, I saw n Very small , Just want to press . I've seen a lot of things recently . Then I don't think it's easy to write , In fact dfs Enumeration method or next_permutation Just go . I chose dfs Because I thought this would happen , From the initial point to a The paper returns to its original point , Until then b Paper, etc , It is the case that the middle will return to the initial point first , But this topic will not be like this , Because it must be a straight line , The straight line between two points is the shortest , If it's a picture, it's not necessarily . The main thing is to look at the pressure dp How is it written .
Code :
dfs( So when the title is a picture ):
int T,n,m,k;
int minn;
PII va[N];
int vb[N];
int vis[M];
void dfs(int now,int sum,int cnt)
{
if(cnt==k+1)
{
sum += abs(va[0].fi-va[now].fi)+abs(va[0].se-va[now].se);
minn = min(minn,sum);
return ;
}
for(int i=0;i<=k;i++)
{
if(now==i) continue;
if(vis[i]==0)
{
if(i!=0) vis[i] = 1;
dfs(i,sum+abs(va[i].fi-va[now].fi)+abs(va[i].se-va[now].se),cnt+(i!=0));
vis[i] = 0;
}
}
}
signed main()
{
IOS;
cin>>T;
while(T--)
{
int a,b;
cin>>n>>m>>a>>b>>k;
for(int i=1;i<=k;i++) cin>>va[i].fi>>va[i].se;
va[0] = {
a,b};
for(int i=0;i<=k;i++) vis[i] = 0;
minn = inf;
dfs(0,0,1);
cout<<"The shortest path has length "<<minn<<"\n";
}
return 0;
}
Pressure dp:
int T,n,m,k;
PII va[N];
int vb[M][M];
int dp[(1ll<<10)][15];
signed main()
{
IOS;
cin>>T;
while(T--)
{
cin>>n>>m>>va[0].fi>>va[0].se>>k;
for(int i=1;i<=k;i++) cin>>va[i].fi>>va[i].se;
for(int i=0;i<=k;i++)
{
for(int j=0;j<=k;j++)
vb[i][j] = abs(va[i].fi-va[j].fi)+abs(va[i].se-va[j].se);
}
for(int i=0;i<(1ll<<n);i++)
{
for(int j=0;j<=n;j++)
dp[i][j] = inf; // All initialization inf
}
for(int i=1;i<=n;i++) dp[1ll<<(i-1)][i] = vb[0][i]; // When the last point is i, And the state of all points 1ll<<(i-1) The weight of the time . such as i==3, that dp[100][1] This state is like this .
for(int i=0;i<(1ll<<n);i++)
{
for(int j=1;j<=n;j++)
{
if((i>>(j-1)&1)) continue;
for(int k=1;k<=n;k++)
{
if((i>>(k-1)&1)==0) continue; //j yes 0, from k Transfer of ,k by 1.
dp[i+(1ll<<(j-1))][j] = min(dp[i+(1ll<<(j-1))][j],dp[i][j]+vb[k][j]);
}
}
}
int minn = inf;
for(int i=1;i<=k;i++) minn = min(minn,dp[i][(1ll<<n)-1]+vb[i][0]); // When all are achieved , And then finally i A weight .
cout<<"The shortest path has length "<<minn<<"\n";
}
return 0;
}
General section :
Attention to detail , Think more , A lot of summary .
边栏推荐
- Shell脚本-echo命令 转义符
- pcl_viewer命令
- Embedded Engineer Interview frequently asked questions
- In the middle of the year, where should fixed asset management go?
- Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
- 2.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于物联网的GY906红外测温门禁刷卡系统
- Record a redis timeout
- Naoqi robot summary 28
- 【pytorch】nn.AdaptiveMaxPool2d
猜你喜欢

Mise en œuvre simple de l'équilibrage de la charge par nacos

Ape anthropology topic 20 (the topic will be updated from time to time)

Pain points and solutions of fixed assets management of group companies

Vsync+ triple cache mechanism +choreographer

3D打印Arduino 四轴飞行器

NoSQL数据库的安装和使用

2.4 activation function

Imitation of Baidu search results top navigation bar effect

Football and basketball game score live broadcast platform source code /app development and construction project

2.2 【pytorch】torchvision. transforms
随机推荐
序列化、监听、自定义注解
Common interview questions for embedded engineers 2-mcu_ STM32
Shell script echo command escape character
2.2 【pytorch】torchvision. transforms
如何做好固定资产管理?易点易动提供智能化方案
2.4 activation function
Shell script - positional parameters (command line parameters)
Daily office consumables management solution
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
猿人学第20题(题目会不定时更新)
Simple load balancing with Nacos
Meituan machine test in 2022
Shell脚本-echo命令 转义符
如何高效拉齐团队认知
【检测技术课案】简易数显电子秤的设计与制作
Preparing for the Blue Bridge Cup -- bit operation
Pain points and solutions of fixed assets management of group companies
Shell脚本-read命令:读取从键盘输入的数据
Installing Oracle EE
通过 代码实例 理解 浅复制 与 深复制