uva 11218 - KTV 简单回溯

简介:

   感觉有比回溯更加高效的方法,但是水平不够,只会回溯


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
bool h[10]={0};
int Max;
struct node
{
    int a,b,c,s;
}org[82];
void dfs(int r,int i,int now)
{
    if(!r)
    {
        Max=max(Max,now);
        return;
    }
    for(--i;i>=0;i--)
    {
        if(h[org[i].a]||h[org[i].b]||h[org[i].c])continue;
        h[org[i].a]=h[org[i].b]=h[org[i].c]=1;
        dfs(r-1,i,now+org[i].s);
        h[org[i].a]=h[org[i].b]=h[org[i].c]=0;
    }
    return;
}
int main()
{
    int n,i,C=0;
    while(~scanf("%d",&n)&&n)
    {
        Max=-1;
        for(i=0;i<n;i++)
            scanf("%d%d%d%d",&org[i].a,&org[i].b,&org[i].c,&org[i].s);
        dfs(3,n,0);
        printf("Case %d: %d\n",++C,Max);
    }
}


目录
相关文章
|
6月前
|
算法 机器人 C++
剑指offer(C++)-JZ13:机器人的运动范围(算法-回溯)
剑指offer(C++)-JZ13:机器人的运动范围(算法-回溯)
畅通工程 HDU - 1232
畅通工程 HDU - 1232
54 0
UPC-排队出发+AcWing-耍杂技的牛(推公式的贪心)
UPC-排队出发+AcWing-耍杂技的牛(推公式的贪心)
57 0
|
机器人
UPC—— 最勇敢的机器人(并查集+分组背包)
UPC—— 最勇敢的机器人(并查集+分组背包)
58 0
|
机器学习/深度学习
[Nowcoder | UPC] 2021年度训练联盟热身训练赛第六场 Hopscotch | 最短路 bfs
题目描述 There’s a new art installation in town, and it inspires you… to play a childish game. The art installation consists of a floor with an n×n matrix of square tiles. Each tile holds a single number from 1 to k. You want to play hopscotch on it.
93 0
|
机器学习/深度学习
[Nowcoder] 2021年度训练联盟热身训练赛第六场 Mini Battleship | 深搜 回溯 乱搞
题意: 给定一个n ∗ n n * nn∗n的矩阵,其中在X XX上一定不可以放置船,而在O OO上面一定要放置船,在′ . ′ '.' 上面可以放置船,也可以不放,问将以下m mm艘船,大小均为1 ∗ x 1 * x1∗x,放置在矩阵中的方案数量 思路: 类似经典的八皇后问题, 首先将所有的m个都成功放置之后,并且所有的O均已成功放置船艘,此时的方案书就应该 + 1 注意船的形状一共有两种情况:横着和竖着,但是对于1 * 1的情况来说就只有一种状态,这里要特判掉 我们用j u d g e ( ) judge()judge()函数来判断是否能够是否可以放置该船,
95 0
|
测试技术
HDU-1232,畅通工程(并查集)
HDU-1232,畅通工程(并查集)

热门文章

最新文章