zoukankan      html  css  js  c++  java
  • 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
    ",r);
    	}
    	return 0;
    }
  • 相关阅读:
    Python-2d形变 动画 表格
    Python-JS (JS介绍~JS的基础数据类型)
    Python-CSS高级 题目
    Python-ccs动画及阴影
    Python-css高级
    Python3 shevel模块,更高级的json序列化数据类型模块(比pickle更高级)
    Python3 第五周大纲(模块,导入方法、import本质,导入优化,模块的分类)
    python3 5月26日 time模块常用时间转换 &datetime()模块学习 random()
    Python软件目录结构
    Python3 json &pickle 数据序列化
  • 原文地址:https://www.cnblogs.com/Blackops/p/5766299.html
Copyright © 2011-2022 走看看