NOJ——1628Alex’s Game(III)(DFS+回溯)
来源:互联网 发布:钱夫人雪梨淘宝店 编辑:程序博客网 时间:2024/06/16 11:45
[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 34 2 62 10 2
- 样例输出
6
#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——1627Alex’s Game(II)(尺取)
- Alex’s Game(I)
- Alex’s Game(I)
- NOJ [1085] kiki's game
- poj 2488A Knight's Journey(DFS+回溯)
- hdu5242——Game(dfs贪心)
- POJ 1753 Flip Game(回溯查找,dfs)
- Toposort(拓扑排序)——DFS递归回溯版
- Toposort(拓扑排序)——DFS递归回溯版
- Toposort(拓扑排序)——DFS递归回溯版
- POJ——1321棋盘问题(DFS+回溯)
- HDU——3786找出直系亲属(DFS+回溯)
- ZOJ 3449:Doraemon's Number Game III
- ZOJ 3449 Doraemon's Number Game III
- dfs——回溯法求最短路径
- POJ1321 棋盘问题 —— DFS回溯
- 1691(dfs + 回溯)
- 即将到来的Xcode8 都更新了什么?
- [LeetCode 206] Reverse Linked List(迭代法)
- xcode中遇到的英文名词(更新)
- mysql主从复制原理详解
- 回望过去,展望未来---关于2015it报告的总结
- NOJ——1628Alex’s Game(III)(DFS+回溯)
- java反射的理解
- 注解编程-简单的反射实现
- java的未来需要掌控者
- mybatis使用log4j打印sql
- 单片机编程小思
- Android中事件处理机制一分钟理清思路
- 学习android源码第一天
- 深入了解java泛型