题目链接: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. mac tomcat安装

    https://blog.csdn.net/qq_35106903/article/details/78860121

  2. ajax请求中包含中文参数

    对需要传递的中文参数先进行两次转码: 1.js文件中使用encodeURI()方法. var url = "Validate.jsp?id=" + encodeURI(encode ...

  3. 魔力Python--if __name__ == '__main__' 的理解

    if __name__ == '__main__' 的理解 __name__ 是当前模块名,当模块被直接运行时模块名为 __main__ . 这句话的意思就是,当模块被直接运行时,以下代码块将被运行, ...

  4. 使用 webpack 搭建多入口项目

    闲来无事,学习一下怎么用 webpack 自定义多入口项目的打包 项目github地址:https://github.com/xiaoliwang2016/webpack-demo 先来看一下目录结构 ...

  5. P1219 八皇后

    题目描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列有且只有一个,每条对角线(包括两条主对角线的所有平行线)上至多有一个棋子. 上面的布局可以用序列2 4 6 1 3 ...

  6. PHP使用RabbitMQ实例

    相关博文:CentOS6.9安装RabbitMQ和源码编译安装php的RabbitMQ扩展RabbitMQ入门基础CentOS7源码编译安装nginx+php7.2+mysql5.7并使用system ...

  7. Spring再接触 Annotation part2

    resource resource beans.xml <?xml version="1.0" encoding="UTF-8"?> <bea ...

  8. java 将mysql中Blob类型转为字符串或数字

    引入Blob类型貌似不行,不知道是版本问题还是java中的Blob类型为oracle,后来使用byte[]转换成功. public float byte2float(byte[] b) { if(b! ...

  9. 如何激活已经运行过的Activity, 而不是重新启动新的Activity

    Intent i=new Intent(this,Activity1.class);   i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);   st ...

  10. linux下的dhcp服务器实现

    一.得到udhcpd(udhcp服务端): 1.解压busybox 2.配置busybox Networking Utilities——> [*] udhcp server(udhcpd) [* ...