UVA 10944 Nuts for nuts..
题目链接: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..的更多相关文章
- Nuts & Bolts Problem
Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping ...
- openfeign 使用方法和执行流程
1.用法 1.1引入依赖 <!-- feign client --> <dependency> <groupId>org.springframework.cloud ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- (转)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 ...
随机推荐
- Eclipse Tomcat部署web项目时出现There are no resources that can be added or removed from the server解决办法
问题原因是:tomcat版本和java版本不匹配.
- [leetcode]332. Reconstruct Itinerary
Given a list of airline tickets represented by pairs of departure and arrival airports [from, to], r ...
- Django之视图
Django之视图 Django的View(视图) 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应. 响应可以是一张网页的HTML内容,一个 ...
- delphi frame 添加 create onshow 事件
delphi frame 添加 create onshow 事件 https://www.cnblogs.com/Gufan/archive/2011/09/06/2538932.html proc ...
- 39_redux_counter应用_redux版本
一.下载 要想使用redux,首先要下载它 npm install --save redux 二.核心API 1.createStore() 作用:创建包含指定reducer的store对象 编码:i ...
- activiti官网实例项目activiti-explorer实操详情
参考链接:https://www.xuchuruo.cn/Activiti-modeler%E6%95%B4%E5%90%88%E5%88%B0Spring.html 按照链接文章提示操作完成之后,启 ...
- U3D学习资料收集
1,风宇冲的博客 2,gkEngine 3,@浅墨_毛星云 4,聊聊引擎底层如何实现BRDF渲染算法
- CentOS编译安装软件过程中遇到zlib.h: No such file or directory
使用命令:yum install zlib-devel 解决问题.
- python3基础-set
集合:无序的,不重复的数据组合 作用: 1.去重,把一个列表变成集合,就自动去重了 2.关系测试,测试两组数据之前的交集.差集.并集等关系 set和dict类似,也是一组key的集合,但不存储valu ...
- 谈谈你对spring的理解?
spring么,就是春天了.春天,动物.....不可描述的季节……你懂得!!! 希望这么说能把面试官逗乐吧. spring可以实现java模块化开发,贯穿表现层,业务层,逻辑层,实现了各个层之间的解耦 ...