NOJ——1628Alex’s Game(III)(DFS+回溯)

32 篇文章 0 订阅
26 篇文章 0 订阅

  • [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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值