Skip to content

Instantly share code, notes, and snippets.

@theoremoon
Created July 2, 2018 01:56
Show Gist options
  • Select an option

  • Save theoremoon/644f83d89b528ec2219293edebfd5095 to your computer and use it in GitHub Desktop.

Select an option

Save theoremoon/644f83d89b528ec2219293edebfd5095 to your computer and use it in GitHub Desktop.
オイラー数を算出するプログラム例です
import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
/// 任意のマスクにマッチする回数を数える
int filter(const(int[][]) data, const(int[][]) mask)
{
int count = 0;
foreach (y; 0..data.length) {
foreach (x; 0..data[y].length) {
// マスクに一致ていたらtrue
bool mask_ac = true;
maskloop: foreach (my; 0..mask.length) {
foreach (mx; 0..mask[my].length) {
// 範囲チェック
if (y + my >= data.length || x + mx >= data[y].length) {
if (mask[my][mx] == 1) {
mask_ac = false;
break maskloop;
}
continue;
}
if (mask[my][mx] == 1 && data[y + my][x + mx] != 1) {
mask_ac = false;
break maskloop;
}
}
}
if (mask_ac) { count++; }
}
}
return count;
}
void main(string[] args)
{
if (args.length <= 1) {
stderr.writefln("Usage: %s <imgfile>", args[0]);
return;
}
auto file = File(args[1], "r");
scope(exit) file.close();
int[][] data = [];
foreach (line; file.byLine) {
data ~= line.strip.split(",").to!(int[]);
}
auto v = data.filter([[1]]);
auto e = data.filter([[1, 1]]) + data.filter([[1], [1]]);
auto d = data.filter([[1, 0], [0, 1]]) + data.filter([[0, 1], [1, 0]]);
auto t = data.filter([[1, 1], [1, 0]]) + data.filter([[1, 0], [1, 1]]) +
data.filter([[0, 1], [1, 1]]) + data.filter([[1, 1], [0, 1]]);
auto f = data.filter([[1, 1], [1, 1]]);
writef("V-->%d\n",v);
writef("E-->%d\n",e);
writef("D-->%d\n",d);
writef("T-->%d\n",t);
writef("F-->%d\n",f);
auto g4 = v-e+f;
auto g8 = v-e-d+t-f;
writef("4連結オイラー数G4-->%d\n",g4);
writef("8連結オイラー数G8-->%d\n",g8);
}
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 1 0 1 0 1 0 1 0 1
1 0 1 0 1 0 1 0 1 0
0 0 0 0 0 0 0 0 0 0
0 1 1 1 0 0 1 0 1 0
1 0 1 0 0 0 1 0 0 0
1 1 1 1 0 0 1 1 0 0
1 0 1 0 0 0 1 0 1 0
1 1 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment