C#,604字节
完整的程序,接受来自STDIN的输入(以行分隔的布局,无尺寸),输出至STDOUT。
using C=System.Console;class P{static void Main(){int w=0,W,i,j,t,k,l,c;string D="",L;for(;(L=C.ReadLine())!=null;D+=L)w=L.Length;var R=new[]{-1,0,1,w,-w};L="X";for(W=i=D.Length;i-->0;){var M=new int[W];for(k=j=i;i>0;){M[j]=++k;t=j+R[c=D[j]%5];if(t<0|t>=W|c<3&t/w!=j/w|c>2&t%w!=j%w)break;j=t;if((l=M[j])>0){var J=new int[W+1];System.Func<int,int>B=null,A=s=>J[s]<0?0:J[k=B(s)]=k==W?k:i;B=x=>J[x]==x?x:B(J[x]);for(i=J[W]=W;i>0;)J[--i]=M[i]<l?i%w<1|i%w>w-2|i<w|i>W-w?W:i:-1;for(;i<W;)if(J[++i]<0)l=D[i]%5/2-1;else{A(i-1);if(i>w)A(i-w);}for(c=W;i-->0;L=""+(c>2?c:0)*l)c-=J[i]<0?0:B(i)/W;}}}C.WriteLine(L);}}
该程序的工作方式是先读取布局,不用说,然后遍历每个单元格。然后,我们从每个单元格运行一个“蛇”,跟随箭头,直到它越过边缘或碰到自身。如果它碰到了自己,那么我们知道我们已经找到了一个循环(或其中的一个“>”事物),并且它还知道循环中有多少条蛇。
知道有一个循环后,我们知道循环中有哪些像元,然后从每个像元(出于某种原因,为+1)到其自身-1
(表示在循环中)或W
(整个宽度)创建一个映射如果它在边缘(或+1(位于index处,W
以简化操作))。
在执行此操作的同时,我们还会找到循环的“最后一个”元素所具有的方向(即,最后一行上的循环的最后一个元素具有来自循环的元素)。该元素必须为“ <”或“ ^”,这告诉我们循环的时脉(CW / CCW)(转换为-1 / + 1)。
然后,我们执行分离集传递,将循环外的所有元素分配给该W
集。然后,我们减去其中的多少,W
以获取循环中和循环中包含的数字。如果该数字小于3,则将其替换为0。将其乘以时钟频率,将其设置为结果,然后以某种方式从for循环中逸出,在该循环中输出结果。
但是,如果以上大多数操作都没有发生(因为没有蛇发现过),那么结果将保留为“ X”,并输出。
using C=System.Console;
class P
{
static void Main()
{
int w=0, // width
W, // full length
i, // used for iterating over all the cells
j, // keeps track of where the snake as got to
t, // t is next j
k, // how far along the snake we are, kind of
// later on, k is used as temp for A
l, // stores a threshold for how far along the snake the loop starts
// later on, l stores the last seen pointer - this tells us the clockness
c; // the translated direction
// later on, c is a backwards-count
string D="", // D is the map
L; // used for reading lines, and then storing the result
// might not be the best yay of doing this
for(;(L=C.ReadLine())!=null; // read a line, while we can
D+=L) // add the line to the map
w=L.Length; // record the width
var R=new[]{-1,0,1,w,-w}; // direction table (char%5) - might be able to replace this array with some bit bashing/ternary
L="X"; // can't seem to fit this in anywhere... (don't strictly need to re-use L)
for(W=i=D.Length;i-->0;) // for each cell, we send a 'snake' to try to find the loop from that cell
{
var M=new int[W]; // stores how far along the snake this point is
for(k=j=i; // k's value doesn't really matter, as long as it's not stupidly big
i>0;) // the i>0 check is just for when we return (see comment at the end of the code)
{
M[j]=++k; // store snake point and advance distance
t=j+R[c=D[j]%5]; // t is position after move (translate <>v^ to 0234 (c is direction))
//c=D[j]%5; // translate <>v^ to 0234 (c is direction)
//t=j+R[c]; // t is position after move
if(t<0|t>=W|c<3&t/w!=j/w|c>2&t%w!=j%w)
break; // hit an edge - will always happen if we don't find a loop - give up on this snake
j=t; // move to new position
if((l=M[j])>0) // we've been here before...
{
// disjoint sets (assign all the edges to one set, assign all the ones on the line to another set, do adjacent disjoint, return size-outteredge (minus if necessary)
var J=new int[W+1]; // looks like we can reuse M for this
System.Func<int,int>B=null,
// whatever s points at should point to i, unless s points to W, in which case it should keep point to W
A=s=>J[s]<0?0:J[k=B(s)]=k==W?k:i;
// read the value this points to
B=x=>J[x]==x?x:B(J[x]);
for(i=J[W]=W;i>0;)
J[--i]=M[i]<l? // if we are not part of the loop
i%w<1|i%w>w-2|i<w|i>W-w? // if we are on the edge
W: // on the edge
i: // not on the edge
-1; // this is on the loop
// now fill in
// we don't have to worry about wrapping, the important bit being an un-wrapping closed loop
// i = 0
for(;i<W;)
if(J[++i]<0) // we are on the loop
l=D[i]%5/2-1; // last one must be ^(4) or <(0)
else{ // can probably crush this into an l returning l assigning term (with if above)
A(i-1);
if(i>w)
A(i-w);
}
// now count the number of non-edges
for(c=W; // assume everything is a non-edge
i-->0;
L=""+(c>2?c:0)*l) // set output to be number of non-edges * clockness (or 0 if too few)
c-=J[i]<0?0:B(i)/W; // subtract 1 if an edge (B(i) is W), othewise 0
// at this point, i is 0, so we will fall out of all the loops
}
}
}
C.WriteLine(L); // output result
}
}