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. 在阿里云上搭建 Spark 实验平台

    在阿里云上搭建 Spark 实验平台 Hadoop2.7.3+Spark2.1.0 完全分布式环境 搭建全过程 [传统文化热爱者] 阿里云服务器搭建spark特别坑的地方 阿里云实现Hadoop+Sp ...

  2. 关于echarts 报错 初始化对象未定义

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  3. 利用ajax获取网页表单数据,并存储到数据库之一(使用JDBC)

    所谓JDBC就是利用java与数据库相连接的技术,从数据库获取既有的信息或者把网页上的信息存储到数据库. 这里简单的介绍公司的一个小项目中的一部分,由于代码较多,所以用图片形式进行展示.源码请查看源码 ...

  4. Python世界里的赋值运算符

    Python赋值运算符 以下假设变量a为10,变量b为20: "=" 的作用是把右边的数值赋值给左边的变量 示例1:编程实现145893秒是几天几小时几分钟几秒钟? total = ...

  5. 工作中代码笔记 -- adb命令篇

    1.抓log方法 (bat文件) mkdir D:\logcatset /p miaoshu=请描述操作:adb logcat -v threadtime > D:\logcat\%miaosh ...

  6. SQL2005查询死锁的表和具体的语句

    查是哪个进程死锁了哪些表 select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName fro ...

  7. Spring注解简介

    提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖. 1. 使用注解方式配置 我们需要修改spring配置文件的头信息,修改部分红色标注,如下: <c ...

  8. java I/O框架 (一)总览

    一.前言 java io框架非常庞大,各种功能的类让人目不暇接,为了系统学习io框架,搜集了各种资料,整理出这篇文章,尽可能详细的讲述java io框架,其中会牵扯到许多信息,不仅包括框架内各种类的方 ...

  9. DataCleaner第一章

    Part1. Introduction to DataCleaner 介绍DataCleaner |--What is data quality(DQ) 数据质量? |--What is data p ...

  10. android的Findbugs问题整理

    1. 对字符串不相等的判断 错误 if (chapterInfo.getPayMode() != String.valueOf(com.shuqi.base.common.Constant.PAYMO ...