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 ...
随机推荐
- Nginx性能优化参考
nginx性能优化参考 1)调整配置文件中的配置项的值(配置文件:nginx.conf) worker_processes auto;开启的进程数,一般配置为跟逻辑CPU核数一样worker_rlim ...
- Ubuntu14.04 32位安装Youcompleteme
前一段时间在ubuntu16.04 64位上安装了vim插件Youcompleteme,花了两三天才弄好.今天在ubuntu14.04 32位上安装同样的插件,才知道之前所做的安装原来是多么的简单.今 ...
- APPScan-简单操作流程
图解安全扫描工具 AppScan使用 IBM Rational AppScan 是一个面向 Web 应用安全检测的自动化工具,使用它可以自动化检测 Web 应用的安全漏洞. 比如跨站点脚本攻击(C ...
- python hdfs初体验
新建目录 chr 新建文件hdfstest1.txt并写入内容 复制hdfstest1.txt的内容到hdfstest2.txt
- luogu愚人节比赛划水记
先放链接:愚人节比赛 说好的 不毒瘤 呢?题目都太神奇了吧! 管理员的脑洞orz T1 这个可以说是蒙数据蒙出来的,直接输出"0",AC T2 本机房dalao成功发现" ...
- thinkphp 结合phpexcel实现excel导入
控制器文件: class ExcelAction extends Action { public function __construct() { import('ORG.Util.ExcelToAr ...
- Hdu 3177 (贪心)
题目大意: 山洞的体积为\(v\) 第\(i\)个物品放在山洞里会占据\(a_i\)的空间,在搬运过程中至少需要\(b_i\)的空间 问能不能把所有物品都放下 贪心题.比较难看出贪心,但是从无顺序要求 ...
- PAT 乙级 1008
题目 题目地址:PAT 乙级 1008 思路 本题需要注意的一点是当 m > n 的时候会出现逻辑性的错误,需要在 m > n 情况下对m做模运算,即 m % n 代码 #include ...
- PAT 乙级 1009
题目 题目地址:PAT 乙级 1009 题解 本题本身属于比较简单的字符串操作题,但是因为对于string的操作和函数不熟悉导致本题做起来很费劲,需要加强对于string类以及相关方法的理解和熟练程度 ...
- GIMP中的新建Layer与更改Layer大小
这边可以直接New Layer,新建一个Layer,还可以New from Visible,第二种是将当前的状态下图像复制出来. 改变Layer的大小,一般的方法两种: Crop to Selecti ...