题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=21&page=show_problem&problem=1885

Dynamic Programming. dp[i][j]表示以ith nut为结尾,状态j下的最少步数。假设有n个nuts,状态有(2^(n-1))-1个。一步一步的添加得到最后结果。代码如下:

 #include <iostream>
#include <math.h>
#include <stdio.h>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
#include <vector>
#include <functional>
#include <cmath>
#include <set>
#define SCF(a) scanf("%d", &a)
#define IN(a) cin>>a
#define FOR(i, a, b) for(int i=a;i<b;i++)
#define Infinity 999999999
typedef long long Int;
using namespace std; struct point {
int x, y;
}; int x, y;
point nuts[];
int num = ;
int dp[][];
int dis[][]; int main()
{
while (scanf("%d %d", &x, &y) != EOF)
{
num = ;
getchar();
FOR(i, , x)
{
FOR(j, , y)
{
char c = getchar();
if (c == 'L')
{
nuts[].x = i;
nuts[].y = j;
}
else if (c == '#')
{
nuts[num].x = i;
nuts[num++].y = j;
}
}
getchar();
} if (num == )
{
printf("%d\n", );
continue;
} FOR(i, , num)
{
FOR(j, i, num)
{
dis[i][j] = dis[j][i] = max(abs(nuts[i].x - nuts[j].x), abs(nuts[i].y - nuts[j].y));
}
} int states = ( << (num - )) - ;
for (int s = ; s <= states; s++)
{
for (int i = ; i < num; i++)
{
dp[i][s] = Infinity;
}
} for (int i = ; i < num; i++)
dp[i][ << (i - )] = dis[][i]; for (int i = ; i <= states; i++)
{
for (int j = ; j < num; j++)
{
if (i & ( << (j - )))
{
for (int k = ; k < num; k++)
{
if (!(i & ( << (k - ))))
{
if (dp[k][i + ( << (k - ))] > dp[j][i] + dis[j][k])
dp[k][i + ( << (k - ))] = dp[j][i] + dis[j][k];
}
}
}
}
}
int finalAns = Infinity;
for (int i = ; i < num; i++)
{
if (dp[i][states] + dis[i][] < finalAns)
finalAns = dp[i][states] + dis[i][];
}
printf("%d\n", finalAns); }
return ;
}

UVA 10944 Nuts for nuts..的更多相关文章

  1. Nuts & Bolts Problem

    Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping ...

  2. openfeign 使用方法和执行流程

    1.用法 1.1引入依赖 <!-- feign client --> <dependency> <groupId>org.springframework.cloud ...

  3. Timus 2068. Game of Nuts 解题报告

    1.题目描述: 2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still i ...

  4. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

  5. [LintCode] Nuts & Bolts Problem 螺栓螺母问题

    Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping ...

  6. Lintcode399 Nuts & Bolts Problem solution 题解

    [题目描述] Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one m ...

  7. Lintcode: Nuts & Bolts Problem

    Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping ...

  8. How To Make A Swipeable Table View Cell With Actions – Without Going Nuts With Scroll Views

    How To Make A Swipeable Table View Cell With Actions – Without Going Nuts With Scroll Views  Ellen S ...

  9. (转)Nuts and Bolts of Applying Deep Learning

    Kevin Zakka's Blog About Nuts and Bolts of Applying Deep Learning Sep 26, 2016 This weekend was very ...

随机推荐

  1. H5使用codovar插件实现微信支付(微信APP支付模式,前端)

    H5打包的app实现微信支付及支付宝支付,本章主要详解微信支付,支付宝支付请查看另一篇“H5使用codovar插件实现支付宝支付(支付宝APP支付模式,前端)” ps:本文只试用H5开发的,微信 AP ...

  2. Swift处理异常

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  3. Android Jetpack 组建介绍(一)——Lifecycler

    转自带你领略Android Jetpack组件的魅力 Android Jetpack 对于任何一个产品来说,我们开发中都会面对哪些问题?如:产品交互.用户体验.代码结构.数据获取.数据存储.网络优化. ...

  4. POC iis短文件名

    __author__ = '*'# -*- coding:utf-8 -*- from lib.httpparse import httpparse def verify(protocol,ip,po ...

  5. (Solved) Nexiq USB link with CAT ET: connection error 149

    User issue: I'm using a Nexiq USB link with CAT ET 2011b and I need some help with connection settin ...

  6. material palette

    https://www.materialpalette.com/

  7. redis各类错误可能的解决方案

    .报Timeout performing EVAL 这个可能是连到读库从库了,直接改成写库就没问题了. 2. 各种ConnectTimeout 一般是偶尔或经常就超时,这种情况,找了各种回答,最后在s ...

  8. ASP.Net的工作线程与请求队列

    当 ASP.NET 接收针对页的请求时,它从线程池中提取一个线程并将请求分配给该线程. 一个普通的(或同步的)页在该请求期间保留线程,从而防止该线程用于处理其他请求.如果一个同步请求成为 I/O bo ...

  9. .gitinore配置失效问题

    问题:在.gitinore中配置忽略项,配置失效 原因:新增加忽略项已经提交过,在暂存区或分支上被版本控制 解决:删除暂存区或分支上的文件(本地需要使用, 只是不希望这个文件被版本控制), 可以使用 ...

  10. Zookeeper配置文件

    zookeeper的默认配置文件为zookeeper/conf/zoo_sample.cfg,需要将其修改为zoo.cfg.其中各配置项的含义,解释如下: 1.tickTime:Client-Serv ...