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;
    }
  • 相关阅读:
    Nginx 配置对流量、连接和请求的限制
    linux iptables规则介绍
    Javascript 语言精粹 代码片段合集
    Wowza® Media Systems 使用配置手册。
    如何去除My97 DatePicker控件上右键弹出官网的链接
    [转载]jQuery诞生记-原理与机制
    java swing JButton文字显示异常
    c# 内存拷贝 解决json序列化丢失私有数据(二进制序列化反序列化)
    c# Marshal 将字节数组转为结构 封装协议
    c# 串口通信及模拟串口进行调试
  • 原文地址:https://www.cnblogs.com/Blackops/p/5766299.html
Copyright © 2011-2022 走看看