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 ...
随机推荐
- XML文件的解析和序列化
序列化: private void createXml() { XmlSerializer serializer = Xml.newSerializer();// xml文件生成器 File file ...
- apache下设置域名多站点访问及禁止apache访问80端口
apache下设置域名多站点访问 当前系统:macOS High Sierra 域名访问配置指定端口后,不同域名只能配置不同的端口 apache配置目录: sudo vim /etc/apache2/ ...
- 一次性删除数据库所有表和所有存储过程 SQL语句
一次性删除数据库所有表和所有存储过程 SQL语句 今天转移数据库数据,需要把数据库原来的表和存储过程清空.删除所有的表:如果由于外键约束删除table失败,则先删除所有约束: --/第1步****** ...
- Redis性能优化之redis.cnf配置参数
redis调优总结 1.相应的参数调优 加内存2.redis使用结构调优3.使用合理的数据类型说明:redis存储的数据为redis hash(字符映射表) 单key多字段结构. 1)调整配置文件中配 ...
- 洛谷 P3183 [HAOI2016]食物链
题目描述 如图所示为某生态系统的食物网示意图,据图回答第1小题现在给你n个物种和m条能量流动关系,求其中的食物链条数.物种的名称为从1到n编号M条能量流动关系形如a1 b1a2 b2a3 b3.... ...
- UVA10917 A walk trough the Forest (最短路,dp)
求出家到其他点的最短路径,题目的条件变成了u->v不是回头路等价于d[u]>d[v]. 然后根据这个条件建DAG图,跑dp统计方案数,dp[u] = sum(dp[v]). #includ ...
- 贴一发STL源码
int my_lower_bound(int size, long long key){ int first = 0, middle; int half, len; len = si ...
- Android(java)学习笔记140:常用的对话框
一.常见对话框属性: 1. AlertDialog.Builder属性 • setTitle: 为对话框设置标题 :• setIcon : 为对话框设置图标:• setMessage: 为对话框设置 ...
- webpack 使用流程
webpack loader 读文件的 脚手架 vue-cli: 自动化的小工具,帮咱们把项目的架子搭起来 -------------------------------------- 开发环境 n ...
- PHP框架深度解析
PHP成为世界上最流行的脚本语言有许多原因:灵活性,易用性等等.但通常只用PHP或者其他语言编码就会显得单调.重复,这时候就需要一个PHP框架来代替程序员完成那些重复不变的部分.本文通过回答What, ...