Java 8,350 326 325 312 303 299 298 259 255字节
int r,z,c,p,i,j,k,d,u,v=1;int c(int[][]b){v(b);k=v-=u=1;v(b);return r;}void v(int[][]b){String m="",x;for(d=b.length;j<d|k<d;k+=u,j+=v,r=m.contains(x)|z!=0?1:r,m+=x)for(x="#",c=0,k*=u,j*=v;j<d&k<d;z+=i|i-1,c*=i^p^1,x+=p=i,r=++c>2?1:r,k+=v,j+=u)i=b[k][j];}
0
当它是一个有效的板时返回;1
如果对三个规则中的一个或多个无效。
-95字节感谢@Nevay。
说明:
在这里尝试。
int r,z,c,p,i,j,k,d,u,v=1;
// Temp integers on class-level
int c(int[][]b){ // Method (1) with int-matrix parameter and int return-type
v(b); // Validate the rows
k=v-=u=1; // Switch rows with columns, and reset `u` to 1
v(b); // Validate the columns
return r; // Return the result
} // End of method (1)
void v(int[][]b){ // Separated method (2) with int-matrix parameter and no return-type
String m="",s; // Two temp Strings to validate uniqueness of rows
for(d=b.length; // Set the dimension of the matrix to `d`
j<d|k<d // Loop (1) as long as either `j` or `k` is smaller than `d`
; // After every iteration:
k+=u,j+=v // Increase the loop-indexes
r=m.contains(s) // If we've found a duplicated row,
|z!=0? // or if the amount of zeroes and ones on this row aren't equal
1:r, // Set `r` to 1 (invalid due to either rule 2 or 3)
m+=s) // Append the current row to the String `m`
for(s=",", // Set String `x` to a separator-character
c=0, // Reset the counter to 0
k*=u,j*=v, // Increase the loop-indexes
j<d&k<d // Inner loop (2) as long as both `j` and `k` are smaller than `d`
; // After every iteration:
z+=i|i-1, // Increase or decrease `z` depending on whether it's a 0 or 1
c*=i^p^1, // Reset `c` if current digit `i` does not equal previous `p`
s+=p=i, // Set previous `p` to current digit, and append it to String `s`
r=++c>2? // If three of the same adjacent digits are found:
1:r; // Set `r` to 1 (invalid due to rule 1)
k+=v,j+=u) // Increase the loop-indexes
i=b[k][j]; // Set `i` to the current item in the matrix
// End of inner loop (2) (implicit / single-line body)
// End of loop (2) (implicit / single-line body)
} // End of separated method (2)