C ++ 11,得分= 992 1024
该算法确实很愚蠢,但到目前为止,也没有人提出过一种严肃的算法,因此我将其发布。它或多或少随机地将单词粘贴在方形板上,然后在无法使单词适合的情况下重新开始。它试图以最大程度地与现有单词重叠的方式进行操作,但实际上效率很低。
编辑:通过将1加到边长上并在失败50次后尝试使用矩形来提高得分。还按大小对输入字符串进行排序,而不是对顺序进行随机化。
#include <iostream>
#include <cstring>
#include <string>
#include <random>
#include <algorithm>
using namespace std;
struct grid {
char *g;
int h,w;
grid(int h, int w):h(h),w(w) {
g = new char[h*w];
memset(g,0,h*w*sizeof(*g));
}
grid(const grid &o) {
h=o.h, w=o.w;
g = new char[h*w];
memcpy(g,o.g,h*w*sizeof(*g));
}
grid(grid &&o) {
h=o.h, w=o.w;
g = o.g;
o.g = 0;
}
grid& operator=(const grid &o) {
h=o.h, w=o.w;
memcpy(g,o.g,h*w*sizeof(*g));
return*this;
}
grid& operator=(grid &&o) {
h=o.h, w=o.w;
g = o.g;
o.g = 0;
return*this;
}
char& operator()(int i, int j) {
return g[i*w+j];
}
~grid() { delete []g; }
};
typedef struct { int n, i, j; grid g; } ng;
const int qmax = 140;
const bool sizesort = true;
const int maxtries = 50;
inline int sq(int x){return x*x;}
bool operator<(const ng &a, const ng& b) {return a.n < b.n;}
void search(vector<string>& s) {
int tl = 0;
for(auto&x: s) tl += x.size();
int l = 0;
while(l*l < tl) l++;
vector<string*> v;
for(size_t i = 0; i < s.size(); i++) v.push_back(&s[i]);
struct{bool operator()(string*a,string*b){return a->size()>b->size();}} scmp;
if(sizesort) sort(v.begin(), v.end(), scmp);
mt19937 rng;
for(;;l--) {
int tries = 0;
int side2 = l;
retry:
tries++;
if(!sizesort) shuffle(v.begin(), v.end(), rng);
if(tries == maxtries) cout<<"rectangle",side2++;
grid g(l,side2);
for(string* x: v) {
string& z = *x;
vector<ng> p;
for(int i = 0; i < g.h; i++)
for(int j = 0; j < g.w; j++) {
if(g(i,j) && g(i,j) != z[0]) continue;
p.push_back({!g(i,j), i,j, g});
p.back().g(i,j) = z[0]|32;
}
for(size_t zi = 1; zi < z.size(); zi++) {
vector<ng> p2;
for(ng &gg: p) {
for(int i = max(gg.i-1,0); i <= min(gg.i+1,g.h-1); i++)
for(int j = max(gg.j-1,0); j <= min(gg.j+1,g.w-1); j++) {
if(!gg.g(i,j) || gg.g(i,j) == z[zi]) {
p2.push_back({gg.n+!g(i,j),i,j,gg.g});
p2.back().g(i,j) = z[zi]|32;
}
}
}
shuffle(p2.begin(), p2.end(), rng);
sort(p2.begin(), p2.end());
if(p2.size() > qmax) p2.erase(p2.begin() + qmax, p2.end());
p = move(p2);
}
if(p.empty()) goto retry;
g = p[0].g;
for(int i = 0; i < g.h; i++)
for(int j = 0; j < g.w; j++)
g(i,j) &= ~32;
}
cout<<g.w*g.h;
for(int i = 0; i < g.h; i++) {
cout<<'\n';
for(int j = 0; j < g.w; j++)
cout<<(g(i,j)?g(i,j):'X');
}
cout<<endl;
}
}
int main()
{
vector<string> v = {"T","WP","GVI","CIHM","EGWIV","QUTYFZ","LWJVPNG","XJMJQWSW","JLPNHFDUW","SWMHBBZWUG","XVDBMDQWDEV","TIUGAVZVUECC","IWDICFWBPSPQR","MMNWFBGMEXMSPY","YIHYXGJXKOUOIZA","BZSANEJNJWWNUJLJ","XTRMGOVPHVZYLLKKG","FLXFVVHNTWLMRRQYFQ","VZKJRAFQIYSBSXORTSH","FNQDIGCPALCHVLHDNZAV","GEAZYFSBSWCETXFKMSWLG","KWIZCEHVBDHEBGDGCJHOID","SKMQPHJAPDQKKHGTIPJCLMH","ZSFQDNYHALSUVWESQVVEUIQC","HXHBESUFCCECHNSTQGDUZPQRB","DSLXVHMOMLUXVHCNOJCBBRPVYB","DVTXKAOYYYRBVAVPSUAOYHIPPWN","PJAIYAWHMTNHTQDZDERPZYQEMLBZ","SYNSHJNOIWESMKWTBIANYUAUNRZOS","WADGUKIHUUFVRVUIBFUXQIOLAWIXAU","LGLXUFIXBEPSOFCKIAHXSHVKZPCXVP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