Luogu3936 Coloring(模拟退火)
裸退火,每次交换两个格子即可。依旧不会调参,稍微抄了点参数并且把随机种子设成了一个神奇的数字终于过掉了。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<ctime>
using namespace std;
#define ll long long
#define N 22
#define M 55
#define T0 1
#define T1 1E-14
#define delta 0.9999
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<''||c>'')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
int n,m,c,a[M],color[N][N],cnt,ans,goal[N][N];
int wx[]={,,,-},wy[]={,,-,};
int calc(int x,int y)
{
int t=;
for (int k=;k<;k++)
if (x+wx[k]>=&&x+wx[k]<=n&&y+wy[k]>=&&y+wy[k]<=m)
if (color[x][y]!=color[x+wx[k]][y+wy[k]]) t++;
return t;
}
void anneal()
{
double T=T0;
while (T>T1)
{
int x1=rand()%n+,y1=rand()%m+,x2=rand()%n+,y2=rand()%m+;
while (x1==x2||y1==y2) x1=rand()%n+,y1=rand()%m+,x2=rand()%n+,y2=rand()%m+;
int d=;
d-=calc(x1,y1),d-=calc(x2,y2);
swap(color[x1][y1],color[x2][y2]);
d+=calc(x1,y1),d+=calc(x2,y2);
if (d<||rand()<exp(-d/T)*RAND_MAX) cnt+=d;
else swap(color[x1][y1],color[x2][y2]);
if (cnt<ans) ans=cnt,memcpy(goal,color,sizeof(color));
T*=delta;
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("color.in","r",stdin);
freopen("color.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read(),m=read(),c=read();
srand();
for (int i=;i<=c;i++) a[i]=read();
int x=,y=;
for (int i=;i<=c;i++)
while (a[i])
{
color[x][y]=i;
a[i]--;y++;
if (y>m) x++,y=;
}
for (int i=;i<=n;i++)
for (int j=;j<=m;j++)
for (int k=;k<;k++)
if (i+wx[k]>=&&i+wx[k]<=n&&j+wy[k]>=&&j+wy[k]<=m)
if (color[i][j]!=color[i+wx[k]][j+wy[k]]) cnt++;
ans=cnt>>=;
memcpy(goal,color,sizeof(color));
for (int i=;i<=;i++) anneal();
for (int i=;i<=n;i++)
{
for (int j=;j<=m;j++)
cout<<goal[i][j]<<' ';
cout<<endl;
}
return ;
}
Luogu3936 Coloring(模拟退火)的更多相关文章
- [Luogu3936]Coloring
Luogu sol 模拟退火呀 初始状态按顺序涂色,让同种颜色尽量放在一起. 每次随机交换两个位置,注意\(\Delta\)的计算 瞎JB调一下参数就行了 可以多做几次避免陷入局部最优解 code # ...
- bzoj3680模拟退火
看题意就是一道数学物理题,带权费马点 --这怎么是数学了,这也是物理的 所以要用物理方法,比如FFF 国际著名oi选手miaom曾说 模拟退火初温可以低,但是最好烧个几千次 国际著名物理课代表+1 ...
- 无题的题 & 模拟退火...
题意: 给你不超过8条一端在圆心的半径,求他们组成的凸包的最大面积. SOL: 正解怎么搞啊不会啊...然后昨天毛爷爷刚讲过模拟退火...那么就打一个吧... 然后就T了,不过三角形的部分分妥妥的.. ...
- [POJ2069]Super Star(模拟退火)
题目链接:http://poj.org/problem?id=2069 题意:求一个半径最小的球,使得它可以包围住所有点. 模拟退火,圆心每次都去找最远那个点,这样两点之间的距离就是半径,那么接下来移 ...
- [POJ2420]A Star not a Tree?(模拟退火)
题目链接:http://poj.org/problem?id=2420 求费马点,即到所有其他点总和距离最小的点. 一开始想枚举一个坐标,另一个坐标二分的,但是check的时候还是O(n)的,复杂度相 ...
- 模拟退火算法求解旅行商问题(附c和matlab源代码)
前几天在做孔群加工问题,各种假设到最后就是求解旅行商问题了,因为原本就有matlab代码模板所以当时就改了城市坐标直接用了,发现运行速度惨不忍睹,最后用上了两个队友的电脑一起跑.这次模拟结束后在想用c ...
- Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)
题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...
- CF149D. Coloring Brackets[区间DP !]
题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...
- hdu3932 模拟退火
模拟退火绝对是从OI--ACM以来接触过的所有算法里面最黑科技的orz 题意:地上有一堆hole,要找一个点,使得(距离该点最远的hole的距离)最小. sol:本来想套昨天的模拟退火模板,初值(0, ...
随机推荐
- 成都Uber优步司机奖励政策(2月7日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 苏州Uber优步司机奖励政策(1月4日~1月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 10、Java并发编程:并发容器之ConcurrentHashMap
Java并发编程:并发容器之ConcurrentHashMap(转载) 下面这部分内容转载自: http://www.haogongju.net/art/2350374 JDK5中添加了新的concu ...
- Ruby数据类型
数字类型 书写整数时,可以根据需要在整数之间任意加入下划线而不会影响数字的值 a=123_45_78 puts a # => 12345678 to_i 截掉小数点之后的数字取整 内置Math模 ...
- gdb 分析出错
1 创建测试代码test.php <?php function test1(){ while(true){ sleep(1); } }echo getmypid() "\r\n&quo ...
- C++11 type_traits 之is_pointer,is_member_function_pointer源码分析
源码如下: template<typename> struct __is_pointer_helper : public false_type { }; template<typen ...
- 博客更换至 www.zhaoch.top
博客更换至 www.zhaoch.top 随手拷了一些链接 http://www.zhaoch.top/操作系统/linux/常用命令备忘.html http://www.zhaoch.top/操作系 ...
- Ubuntu14.04 panic --not syncing: Attempt to kill init 解决方法
Ubuntu14.04 panic --not syncing: Attempt to kill init 解决方法 工作电脑装了一个虚拟机玩玩,胡乱下载了一些软件,apt-get了不少操作,后来重启 ...
- Intro to Probabilistic Model
概率论复习 概率(Probability) 频率学派(Frequentist):由大量试验得到的期望频率(致命缺陷:有些事情无法大量试验,例如一封邮件是垃圾邮件的概率,雷达探测的物体是一枚导弹的概率) ...
- Tensorflow - Implement for generating some 3-dimensional phony data and fitting them with a plane.
Coding according to TensorFlow 官方文档中文版 import tensorflow as tf import numpy as np ''' Intro. for thi ...