JZOJ 3807 【NOIP2014模拟8.25】地砖铺设

来源:互联网 发布:windows netsh wlan 编辑:程序博客网 时间:2024/04/28 00:02

地砖铺设

Description

给出一个长为N,宽为M的网格图,用一些由含有相同字母的正方形填满这个网格(一个正方形内含有的字母必须相同,不同的正方形含有的字母不一定相同),使得相邻的两个正方形含有的字母不同,输出字典序最小的满足条件的网格图。

Data Constraint

N,M<=100

Solution

贪心。
从上到下,从左到右对网格图进行构造,然后每次判断 与前面的格子进行合并构成一个更大的正方形更优 还是 该格子作为一个新的正方形的左上顶点更优,选最优方案构造即可。

Code(Pascal)

var    ch:array[0..1000,0..1000] of char;    x,y,ok,bc:array[0..1000,0..1000] of longint;    n,m,j,k,l,i:longint;    bz:array['A'..'E'] of boolean;    cc:char;begin    readln(n,m);    for i:=0 to n+1 do    for l:=0 to m+1 do ch[i,l]:=' ';    for l:=0 to m+1 do ch[0,l]:='E';    for l:=0 to n+1 do ch[l,0]:='E';    for i:=1 to n do    begin        for l:=1 to m do        begin            if ch[i,l]<>' ' then continue;            ok[i,l]:=1;            fillchar(bz,sizeof(bz),true);            bz[ch[i,l-1]]:=false;  bz[ch[i-1,l]]:=false;            if ch[i,l+1]<>' ' then bz[ch[i,l+1]]:=false;            for cc:='A' to 'E' do            if bz[cc] then break;            bc[i,l]:=1;            if ch[i,l-1]>cc then ch[i,l]:=cc else            if (ok[i,l-1]=1) and (ch[i-1,l]<>ch[i,l-1]) and (i+bc[i,l-1]<=n) then            begin                for j:=1 to bc[i,l-1] do                ch[i+j-1,l]:=ch[i,l-1];                bc[i,l]:=bc[i,l]+bc[i,l-1];                for j:=l downto l-bc[i,l-1] do                ch[i+bc[i,l-1],j]:=ch[i,l-1];            end else ch[i,l]:=cc;        end;    end;    for i:=1 to n do    begin        for l:=1 to m do write(ch[i,l]);        writeln;    end;end.
1 0
原创粉丝点击