C,813字节
#include<map>
#include<set>
#include<cstdlib>
typedef int I;typedef char*C;I a,t,s,h;struct p{I x,y;}j,k;std::map<I,std::set<I>>l;std::map<I,p>g;C m,M=" |-";I L(I n,C q,C Q){j=g[n],h=1;for(I o:l[n])if(g.find(o)!=g.end())if(!(a=(k=g[o]).y==j.y)&&k.x^j.x)h=0;else for(I x=j.x,y=j.y,e=k.y*s+k.x,b,d=(k.x<j.x||k.y<j.y)?-1:1;a?x+=d:y+=d,(b=y*s+x)^e;m[b]=q[a])if(m[b]^Q[a]){h=0;break;}}I N(){for(auto i:g)for(I n:l[i.first])if(g.find(n)==g.end())return n;for(auto i:l)if(g.find(a=i.first)==g.end())return a;exit(puts(m));}I f(){for(I n=N(),x,y=0,b;y<s;y+=2)for(x=0;x<s;x+=2)m[b=y*s+x]==*M?g[n]={x,y},m[b]=n,L(n,M+2,M),h&&f(),L(n,M,M+2),m[b]=*M,g.erase(n):0;}I main(I c,C*v){for(;--c;l[a].insert(s),l[s].insert(a))a=v[c][0],s=v[c][1];t=l.size(),s=t|1;memset(m=(C)calloc(s,s),*M,s*s-1);for(a=1;a<s;++a)m[a*s-1]=10;f();}
将输入作为命令行参数,例如:
./network AB BF BL FK LK KR KS RP SJ SP JA TV VN
aditsu的答案在规模上几乎没有竞争力,但效率更高!
这将强行使用所有可能的解决方案,但会迅速识别故障。对于这两个测试用例,它几乎立即完成,并且在更尴尬的输入上似乎只需要几秒钟。它还对接受的节点名称没有限制(尽管您不能命名一个空格|
或-
),并且对节点数量也没有限制(只要所有名称都适合一个字节,那么实际限制是252个节点,并且会在达到那么多之前变慢。
有足够的空间来加快它的速度。许多短路现象都消失在高尔夫球运动中,有些零件可能会从热回路中移出。此外,对对称性的一些观察还可以极大地减少前2个节点的位置。
分解:
#include<map>
#include<set>
#include<cstdlib>
typedef int I;
typedef char*C;
I a,t,s,h; // Variables shared between functions
struct p{I x,y;} // Coord datatype
j,k; // Temporary coord references
std::map<I,std::set<I>>l; // Bidirectional multimap of node links
std::map<I,p>g; // Map of nodes to positions
C m, // Rendered grid
M=" |-"; // Lookup table for output characters
// Line rendering function
// Sets h to 1 if all lines are drawn successfully, or 0 if there is a blocker
I L(I n,C q,C Q){
j=g[n],h=1;
for(I o:l[n]) // For each connection to the current node
if(g.find(o)!=g.end()) // If the target node has been positioned
if(!(a=(k=g[o]).y==j.y)&&k.x^j.x)h=0; // Fail if the nodes are not aligned
else
for(I x=j.x,y=j.y, // Loop from node to target
e=k.y*s+k.x,
b,d=(k.x<j.x||k.y<j.y)?-1:1;
a?x+=d:y+=d,(b=y*s+x)^e;
m[b]=q[a]) // Render character (| or -)
if(m[b]^Q[a]){h=0;break;} // Abort if cell is not blank
}
// Next node selection: finds the next connected node to try,
// or the next unconnected node if the current connected set is complete.
// Displays the result and exits if the entire graph has been rendered.
I N(){
for(auto i:g)for(I n:l[i.first]) // Search for a connected node...
if(g.find(n)==g.end())return n; // ...and return the first available
for(auto i:l) // Else search for an unconnected node...
if(g.find(a=i.first)==g.end())
return a; // ...and return the first available
exit(puts(m)); // Else draw the grid to screen and stop
}
// Recursive brute-force implementation
I f(){
for(I n=N(),x,y=0,b;y<s;y+=2) // Loop through all grid positions
for(x=0;x<s;x+=2)
m[b=y*s+x]==*M // If the current position is available
?g[n]={x,y}, // Record the location for this node
m[b]=n, // Render the node
L(n,M+2,M), // Render lines to connected nodes
h&&f(), // If line rendering succeeded, recurse
L(n,M,M+2), // Un-render lines
m[b]=*M,g.erase(n) // Un-render node
:0;
}
// Input parsing and grid setup
I main(I c,C*v){
// Parse all inputs into a bidirectional multi-map
for(;--c;l[a].insert(s),l[s].insert(a))a=v[c][0],s=v[c][1];
t=l.size(),s=t|1; // Calculate a grid size
// Allocate memory for the grid and render newlines
memset(m=(C)calloc(s,s),*M,s*s-1);
for(a=1;a<s;++a)m[a*s-1]=10;
f(); // Begin recursive solving
}
H A
并且给定的输出中没有该边缘。编辑:发现并解决问题。