NOJ——1628Alex’s Game(III)(DFS+回溯)
[1628] Alex’s Game(III)
- 时间限制: 1000 ms 内存限制: 65535 K
- 问题描述
Alex likes to play with one and zero as you know .
But Alex does’t have a girlfriend now because his girlfriend is trapped in Alcatraz Island.So Alex wants to rescue her.Now Alex has some information about this place.there are two coast(海岸线) which parallel to each otherand there some castles(城堡) in each cosat.Each coast have n(n<=15) castles,each castle has a id which begins from 1.Each castle just has a undirected road with it's adjacent castle .What's more,He must build exactly k bridges between two coast,and the bridge can only be built between two castles which have the same id and the cost is zero.Now Alex is in the first castle in the first coast and his girlfriend is in the last castle in the second coast ,please calculate the shortest distance Alex needs to walk.
- 输入
- First are two integers n (n<=15) and k (1 <= k<=n);
Next contains two lines ,every line contains n-1 integers
which represent the distance between two adjacent castles and the value is less than 100. - 输出
- For each case output the answer represent shortest distance Alex needs to walk.
- 样例输入
4 3
4 2 6
2 10 2- 样例输出
6
感觉跟CF上某一题比较像。借助于DFS的树形DP?n只有15的话爆搜好了
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=110;
int n,k,r;
int dx[35][35];
int vis[N];
void dfs(int now,int sum,int bri)
{
if(now==2*n)
{
r=min(r,sum);
return ;
}
if(now<0||now>2*n)
return ;
if(now<=n)
{
if(!vis[now+1])
{
vis[now+1]=1;
dfs(now+1,sum+dx[now][now+1],bri);
vis[now+1]=0;
}
if(!vis[now+n]&&bri+1<=k)
{
vis[now+n]=1;
dfs(now+n,sum+dx[now][now+n],bri+1);
vis[now+n]=0;
} }
else if(now<=2*n&&now>n)
{
if(!vis[now+1])
{
vis[now+1]=1;
dfs(now+1,sum+dx[now][now+1],bri);
vis[now+1]=0;
}
if(!vis[now-n]&&bri+1<=k)
{
vis[now-n]=1;
dfs(now-n,sum+dx[now][now-n],bri+1);
vis[now-n]=0;
}
}
}
int main(void)
{
int i,j,d;
while (~scanf("%d%d",&n,&k))
{
MM(dx,0);
r=INF;
for (i=1; i<=n-1; i++)
{
scanf("%d",&d);
dx[i][i+1]=d;
dx[i+1][i]=d;
}
for (i=1; i<=n-1; i++)
{
scanf("%d",&d);
dx[i+n][i+n+1]=d;
dx[i+n+1][i+n]=d;
}
dfs(1,0,0);
printf("%d\n",r);
}
return 0;
}
NOJ——1628Alex’s Game(III)(DFS+回溯)的更多相关文章
- NOJ 1074 Hey Judge(DFS回溯)
Problem 1074: Hey Judge Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit interger IO format: ...
- 素数环(dfs+回溯)
题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)
哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- uva 193 Graph Coloring(图染色 dfs回溯)
Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...
- P1074 靶形数独 dfs回溯法
题目描述 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向 Z 博士请教,Z 博士拿出了他最近发明的“靶 ...
- 剪格子---(dfs回溯)
如图p1.jpg所示,3 x 3 的格子中填写了一些整数. 我们沿着图中的红色线剪开,得到两个部分,每个部分的数字和都是60. 本题的要求就是请你编程判定:对给定的m x n 的格子中的整数,是否可以 ...
- 蓝桥杯 算法提高 8皇后·改 -- DFS 回溯
算法提高 8皇后·改 时间限制:1.0s 内存限制:256.0MB 问题描述 规则同8皇后问题,但是棋盘上每格都有一个数字,要求八皇后所在格子数字之和最大. 输入格式 一个8*8 ...
随机推荐
- 详解ASP.NET缓存机制
文中对ASP.NET的缓存机制进行了简述,ASP.NET中的缓存极大的简化了开发人员的使用,如果使用得当,程序性能会有客观的提升.缓存是在内存存储数据的一项技术,也是ASP.NET中提供的重要特性之一 ...
- Android ScrollView嵌套RecyclerView导致在三星s8曲面屏显示不全问题
当RecyclerView适配显示不全时可以单独给其嵌套一个相对布局!!!(必须是相对布局),这样在曲面屏手机就可以全部显示出来如下图所示 <RelativeLayout android:lay ...
- iphone开发设置默认字体
It seems to be possible in iOS 5 using the UIAppearance proxy. [[UILabel appearance] setFont:[UIFont ...
- dp 20190618
C. Party Lemonade 这个题目是贪心,开始我以为是背包,不过也不太好背包,因为这个L都已经是1e9了. 这个题目怎么贪心呢?它是因为这里有一个二倍的关系,所以说val[i]=val[i- ...
- C# DateTime.Now函数
// 2008年4月24日 System.DateTime.Now.ToString( " D " );// 2008-4-24 System.DateTime.Now.ToStr ...
- vue 动态合并单元格、并添加小计合计功能
1.效果图 2.后台返回数据格式(平铺式) 3.后台返回数据后,整理所需要展示的属性存储到(items)数组内 var obj = { "id": curItems[i].id, ...
- vue 获取汉字的全拼、简拼、首拼
1.封装公共方法,获取汉字的全拼.简拼.首拼 export const Pinyin = { _JMcode:{ "-":"", "—":& ...
- Nginx代理tcp端口实现负载均衡
Nginx代理tcp端口实现负载均衡 1.修改配置文件 vi /etc/nginx/nginx.conf 添加如下配置: stream { ###XXX upstream notify { has ...
- Java开发工具下载
一.Tomcat下载: http://tomcat.apache.org/ 二.Maven下载: http://maven.apache.org/download.cgi 三.eclipse下载: h ...
- NFS网络共享服务 挂载参数及优化 内核优化建议
配置NFS服务端 nfs01上安装软件 [root@nfs01 ~]# yum install nfs-utils rpcbind -y nfs-utils:NFS服务的主程序,包括rpc.nfsd. ...