题目链接: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. JavaScript:鼠标拖拽效果

    (之前的那个模板方法模式实在没搞懂...等几天再去研究8) 预览效果: 限制拖动范围在视口内.调整窗口时自动居中... <!DOCTYPE html> <html lang=&quo ...

  2. Python爬虫与数据分析之进阶教程:文件操作、lambda表达式、递归、yield生成器

    专栏目录: Python爬虫与数据分析之python教学视频.python源码分享,python Python爬虫与数据分析之基础教程:Python的语法.字典.元组.列表 Python爬虫与数据分析 ...

  3. Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?

    当我们使用gson 转对象时,并且这个对象中有一些属性是懒加载时如 @Entity @Table(name = "user") public class User { @Id @C ...

  4. Intellij IDEA常用快捷键介绍 Intellij IDEA快捷键大全汇总

    其他的快捷键还有很多,象Ctrl+G(跳转到指定行).Ctrl+F4(关闭当前编辑页面).Ctrl+F(搜索)等等,这些快捷键由于是各个编辑器都会提供的,而且定义的键位也都差不多,就没什么可说的了: ...

  5. C++builder Tokyo 调用com 不正确的变量类型

    C++builder Tokyo 调用com 不正确的变量类型 tt.OleFunction("interface_call","MS01",&erro ...

  6. Django07-cookie和session

    一.Cookie 1.Cookie的由来 大家都知道HTTP协议是无状态的.无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响 ...

  7. 基于C/S 结构的IM即时通讯软件--上篇

    目的:实现类似QQ群聊的聊天室,可以看到好友列表及互相传送信息. 分析:可基于C/S结构实现即时通讯 1.创建基于对话框的MFC程序(支持windows套接字),并增加相应的类与结构体,完善对话框界面 ...

  8. Iview Modal 点击确定就关闭,我想禁止关闭该怎么办呢?

    网上各种loading,不推荐.用下面的方法直接就解决了 <div slot="footer"> <Button type="text" si ...

  9. Java学习--变量

    参考  http://www.runoob.com/java/java-variable-types.html java变量使用前 java中的所有变量在使用前必须声明并且初始化 类变量和实例变量会默 ...

  10. 【转】How to initialize a two-dimensional array in Python?

    [wrong way:] m=[[element] * numcols] * numrows for example: >>> m=[['a'] *3] * 2>>> ...