Luogu

sol

模拟退火呀

初始状态按顺序涂色,让同种颜色尽量放在一起。

每次随机交换两个位置,注意\(\Delta\)的计算

瞎JB调一下参数就行了

可以多做几次避免陷入局部最优解

code

#include<cstdio>
#include<algorithm>
#include<ctime>
#include<cmath>
using namespace std;
const int N = 100;
struct node{int a[25][25],tot;}ans;
int n,m,c,p[N],dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
double Rand(){return rand()%1000/1000.0;}
bool inside(int x,int y){return 1<=x&&x<=n&&1<=y&&y<=m;}
void SA(double T)
{
node now=ans,nw;
int x1,y1,x2,y2,d;
while (T>1e-15)
{
nw=now;
x1=1+rand()%n,y1=1+rand()%m;
x2=1+rand()%n,y2=1+rand()%m;
if (x1==x2&&y1==y2) continue;
for (int d=0;d<4;d++)
if (inside(x1+dx[d],y1+dy[d]))
nw.tot-=(nw.a[x1][y1]!=nw.a[x1+dx[d]][y1+dy[d]]);
for (int d=0;d<4;d++)
if (inside(x2+dx[d],y2+dy[d]))
nw.tot-=(nw.a[x2][y2]!=nw.a[x2+dx[d]][y2+dy[d]]);
swap(nw.a[x1][y1],nw.a[x2][y2]);
for (int d=0;d<4;d++)
if (inside(x1+dx[d],y1+dy[d]))
nw.tot+=(nw.a[x1][y1]!=nw.a[x1+dx[d]][y1+dy[d]]);
for (int d=0;d<4;d++)
if (inside(x2+dx[d],y2+dy[d]))
nw.tot+=(nw.a[x2][y2]!=nw.a[x2+dx[d]][y2+dy[d]]);
if (nw.tot<=now.tot||exp((now.tot-nw.tot)/T)>Rand()) now=nw;
if (nw.tot<ans.tot) ans=nw;
T*=0.99999;
}
}
int main()
{
scanf("%d %d %d",&n,&m,&c);
for (int i=1;i<=c;++i) scanf("%d",&p[i]);
for (int i=1,k=1;i<=n;i++)
for (int j=1;j<=m;j++)
ans.a[i][j]=p[k]?k:++k,--p[k];
for (int i=1;i<=n;++i)
for (int j=1;j<=m;++j)
{
if (i<n&&ans.a[i][j]!=ans.a[i+1][j]) ++ans.tot;
if (j<m&&ans.a[i][j]!=ans.a[i][j+1]) ++ans.tot;
}
SA(1);SA(1);SA(1);
for (int i=1;i<=n;++i,puts(""))
for (int j=1;j<=m;++j)
printf("%d ",ans.a[i][j]);
return 0;
}

[Luogu3936]Coloring的更多相关文章

  1. Luogu3936 Coloring(模拟退火)

    裸退火,每次交换两个格子即可.依旧不会调参,稍微抄了点参数并且把随机种子设成了一个神奇的数字终于过掉了. #include<iostream> #include<cstdio> ...

  2. 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 ...

  3. CF149D. Coloring Brackets[区间DP !]

    题意:给括号匹配涂色,红色蓝色或不涂,要求见原题,求方案数 区间DP 用栈先处理匹配 f[i][j][0/1/2][0/1/2]表示i到ji涂色和j涂色的方案数 l和r匹配的话,转移到(l+1,r-1 ...

  4. Codeforces Round #369 (Div. 2) C. Coloring Trees DP

    C. Coloring Trees   ZS the Coder and Chris the Baboon has arrived at Udayland! They walked in the pa ...

  5. CodeForces #369 C. Coloring Trees DP

    题目链接:C. Coloring Trees 题意:给出n棵树的颜色,有些树被染了,有些没有.现在让你把没被染色的树染色.使得beauty = k.问,最少使用的颜料是多少.   K:连续的颜色为一组 ...

  6. CodeForces 149D Coloring Brackets

    Coloring Brackets time limit per test: 2 seconds memory limit per test: 256 megabytes input: standar ...

  7. C. Coloring Trees DP

    传送门:http://codeforces.com/problemset/problem/711/C 题目: C. Coloring Trees time limit per test 2 secon ...

  8. codeforces 711C C. Coloring Trees(dp)

    题目链接: C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  9. POJ 1419 Graph Coloring(最大独立集/补图的最大团)

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4893   Accepted: 2271   ...

随机推荐

  1. 017 Java中的静态代理、JDK动态代理、cglib动态代理

    一.静态代理 代理模式是常用设计模式的一种,我们在软件设计时常用的代理一般是指静态代理,也就是在代码中显式指定的代理. 静态代理由业务实现类.业务代理类两部分组成.业务实现类负责实现主要的业务方法,业 ...

  2. 洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication【最小割】分析+题解代码

    洛谷P1345 [USACO5.4]奶牛的电信Telecowmunication[最小割]分析+题解代码 题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流. ...

  3. easyui datagrid 右边框被隐藏

    问题前: 如下图: 解决思路: 待文档加载完成后再执行dategrid函数 $(function () { $("#tt").datagrid({ //....... }); }) ...

  4. os模块中关于文件/目录常用的函数使用方法

    os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...

  5. Selenium常用方法及函数、txt参数化

    常用方法及函数: 1.表单的提交方法:submit解释:查找到表单(from)直接调用submit即可实例:driver.find_element_by_id("form1").s ...

  6. jquery validate 动态增加删除验证规则(转载)

    页面加载完成初始化form validate $("#user_regForm").validate({ errorPlacement: function(error, eleme ...

  7. linux中/bin和/sbin和/usr/bin和/usr/sbin

    首先先解释一下bin和sbin的意思: 1.bin:为任何用户都可以使用的指令 2.sbin:(super bin)也就是只有超级管理员才能使用的指令 /usr    UNIX Software Re ...

  8. 【剑指offer】04替换空格,C++实现

    0.前言 # 替换空格为字符串部分的题目,剑指offer中字符串系列的文章地址,剑指offer全系列文章地址 1.题目 # 请实现一个函数,将一个字符串中的空格替换成"%20".例 ...

  9. UESTC - 1057 秋实大哥与花 线段树

    题意 秋实大哥是一个儒雅之人,昼听笙歌夜醉眠,若非月下即花前. 所以秋实大哥精心照料了很多花朵.现在所有的花朵排成了一行,每朵花有一个愉悦值. 秋实大哥每天要对着某一段连续的花朵歌唱,然后这些花朵的愉 ...

  10. MongoDB,子查询

    //1.从sub(订单明细)对订单号分组,查询最多子订单的单号一条数据,重命名orderNo字段为num//2.根据这个sub.num(从结果集里获取第一条结果),查询main(主档表) db.mai ...