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 ...
随机推荐
- 2018-2019-2 网络对抗技术 20165304 Exp6 信息搜集与漏洞扫描
2018-2019-2 网络对抗技术 20165304 Exp6 信息搜集与漏洞扫描 原理与实践说明 1.实践原理 信息搜集:渗透测试中首先要做的重要事项之一,搜集关于目标机器的一切信息 间接收集 D ...
- AES五种加密模式(CBC、ECB、CTR、OCF、CFB)
--转载https://www.cnblogs.com/starwolf/p/3365834.html https://www.freebuf.com/column/171939.html 分组密码有 ...
- BaseRecycleViewAdapterHelper
BaseRecycleViewAdapterHelper 官方文档 git说明文档 1.English 2.中文
- PHP提交订单,信息的传递
今天被之前的同事问到订单这一块(他用的是Laraver)..."多个商品在同一个订单里,怎么将相同的商品放进一个分组"... 当时我和他的反应都是,array_push, arra ...
- echars 图表提示框自定义显示
一 . 显示单条数据时在tooltip里调用formatter函数给自定义提示框内数据. 效果图显示 二 . 当显示多条数据时.为保证和原来的效果相同需要自己实现点的效果.如果不实现,提示框则不限点的 ...
- c#+.net常用功能点
1.比较差集合,排除不在集合中的,即data是data1排除data2剩下的数据 var data1 = new List<string>(); var data2 = new List& ...
- vue+el-menu设置了router之后如何跳转到外部链接
<el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="c ...
- fyi
<div> <h5 class="page-header">人件费预测项目管理</h5> <hr></div><d ...
- 模拟退火算法-旅行商问题-matlab实现
整理一下数学建模会用到的算法,供比赛时候参考食用. —————————————————————————————————————————— 旅行商问题(TSP): 给定一系列城市和每对城市之间的距离,求 ...
- esxi虚拟机克隆后的主机网卡设置
windows 在虚拟机配置页面右击属性,编辑虚拟机设置,删除掉原来的网卡,重新添加新的网卡 进入系统以后,进入 windows/system32 找到sysprep,运行等待电脑重启 重启过程会稍微 ...