Created
February 25, 2010 23:09
-
-
Save miau/315157 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// coding: utf-8 | |
import std.stdio; | |
import std.string; | |
int main(string[] argv) { | |
auto fin = File("patchworkinput.txt"); | |
char[][] data = [ "dummy".dup ]; // ダミー。後で "." に置き換える | |
foreach (line; fin.byLine) { | |
data ~= ("." ~ line ~ "."); // 両側に sentinel を配置 | |
} | |
auto height = data.length - 1; | |
auto width = data[1].length - 2; | |
writef("height: %d, width: %d\n", height, width); | |
// 一行目を sentinel に | |
data[0] = data[1].dup; | |
data[0][] = '.'; | |
// 末尾行も | |
data ~= data[0].dup; | |
// var_dump みたいなのないのかな? | |
foreach (line; data) { | |
writeln(line); | |
} | |
// 各座標の文字を小文字に置き換えながら、面積が最大の箇所を探す | |
auto max_count = 0; // 塗りつぶした面積の最大値 | |
int[][] max_points = []; // 面積が最大となる塗りつぶし開始位置の配列 | |
for (auto y = 1; y <= height; y++) { | |
for (auto x = 1; x <= width; x++) { | |
auto c = data[y][x]; | |
if (c != 'A' && c != 'B') { | |
continue; | |
} | |
// 小文字に置き換え。cast の代わりに std.ctype.tolower() が使えたかも。 | |
auto count = getFilledCount(data, y, x, c, cast(char)(c + 0x20)); | |
if (count > max_count) { | |
max_count = count; | |
max_points = []; | |
} | |
if (count == max_count) { | |
max_points ~= [ y, x ]; | |
} | |
} | |
} | |
foreach (line; data) { | |
writeln(line); | |
} | |
// 面積が最大だった箇所を "_" に置き換え | |
foreach (point; max_points) { | |
// 多重代入とかできないの? | |
auto y = point[0]; | |
auto x = point[1]; | |
auto c = data[y][x]; | |
getFilledCount(data, y, x, c, '_'); | |
} | |
foreach (line; data) { | |
writeln(line); | |
} | |
// 各行の "_" の個数を出力 | |
for (auto y = 1; y <= height; y++) { | |
writeln(count(cast(string)data[y], "_")); | |
} | |
return 0; | |
} | |
// (y, x) のデータが c だったら c2 に置き換えて、上下左右の座標に対して再帰処理 | |
int getFilledCount(ref char[][] data, int y, int x, char c, char c2) { | |
auto count = 0; | |
if (data[y][x] != c) { | |
return count; | |
} | |
data[y][x] = c2; | |
count += 1; | |
count += getFilledCount(data, y , x+1, c, c2); | |
count += getFilledCount(data, y , x-1, c, c2); | |
count += getFilledCount(data, y+1, x , c, c2); | |
count += getFilledCount(data, y-1, x , c, c2); | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment