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 ...
随机推荐
- C语言abort函数
C语言编程入门教程,C语言库函数的abort函数的作用是异常终止一个进程,意味着abort后面的代码将不再执行. #include<stdio.h> #include<stdlib. ...
- MySQL索引使用等
- .Net Mvc 返回Json,动态生成EasyUI Tree
最近做一个项目,开始接触EasyUI,感觉很强大,很适合我这种对前台不是很感冒的人.在学习Tree的过程中,感觉网上的资料挺乱的,很多只是把EasyUI API 抄了一遍.现在把最近这段时间的学到的, ...
- 关于 java swing 使用按钮关闭窗口
目的是给JButton添加点击操作,使指定JFrame窗口关闭. 网上不少说法是采用frame.dispose();的方法 但是采用frame.dispose();并没有使添加在frame上的wind ...
- 关于HTML5中Video标签无法播放mp4的解决办法
1.首先先排除掉代码问题.路径问题.浏览器不支持问题等常规问题,这些问题另行百度. <video width="500px" height="300px" ...
- shell脚本,检查给出的字符串是否为回文
[root@localhost wyb]# .sh #!/bin/bash #检查给出的字符串是否为回文 read -p "Please input a String:" numb ...
- javase(5)_面向对象
一.概述 1.面向对象是一种思想,让我们由执行者变成指挥者,执行者是面向过程,指挥者是面向对象.例如人开冰箱门,开冰箱门这个动作应该属于门而不是人,冰箱自己最清楚门应该怎么开,人只是调用了冰箱的这个动 ...
- 浅谈 Swift 2 中的 Objective-C 指针
浅谈 Swift 2 中的 Objective-C 指针 2015-09-07 499 文章目录 1. 在 Swift 中读 C 指针 2. 在 Swift 中创建 C 指针 3. 总结 作者:Ja ...
- Windows10系统可以禁止的服务(按名称排序)
1.Application LayerGateway Service(Windows必须禁止的10项服务) 2.Bluetooth Handsfree Service(没有蓝牙的用户可以关闭) 3.B ...
- 【Java_多线程并发编程】JUC原子类——AtomicLong原子类
1. AtomicLong是基本原子类中的一种 AtomicLong是对长整形进行原子操作. 1.1 AtomicLong类的函数列表 // 构造函数 AtomicLong() // 创建值为init ...