文字列の八方向検索
Posted feedbacks - C++
全角じゃないけど。反転した比較文字列をつくって、4方向(右方向+下方向)の文字列チェック×2で、8方向...になってるとおもう....orz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
void output(int x, int y, std::string v)
{
std::cout << "(" << x << "," << y << "), " << v.c_str() << std::endl;
}
void search8way(std::vector<std::string> lines, std::string str)
{
if (str.empty() != false)
return;
std::string rstr(str);
reverse(rstr.begin(), rstr.end());
int nLen = (int)str.size();
char cs = str[0];
char ce = str[nLen - 1];
int x = 0;
for (int y = 0; y < (int)lines.size(); y++)
{
std::string line = lines[y];
for (int x = 0; x < (int)line.size(); x++)
{
char c = line[x];
if ((c != cs) && (c != ce))
continue;
std::string strDL(""); strDL += c;
std::string strDD(""); strDD += c;
std::string strDR(""); strDR += c;
for (int d = 1; (d < nLen) && (y + d < (int)lines.size()); d++)
{
std::string buff = lines[y + d];
if ( x < (int)buff.size() ) { strDD += buff[x ]; }
if ((x - d) < (int)buff.size() && (x - d >= 0)) { strDL += buff[x - d]; }
if ((x + d) < (int)buff.size() ) { strDR += buff[x + d]; }
}
if (c == cs)
{
if (line.compare(x, nLen, str) == 0) { output(x, y, "右"); }
if (strDL.compare(str) == 0) { output(x, y, "左下"); }
if (strDD.compare(str) == 0) { output(x, y, "下" ); }
if (strDR.compare(str) == 0) { output(x, y, "右下"); }
}
if (c == ce)
{
if (line.compare(x, nLen, rstr) == 0) { output(x + (nLen - 1), y, "左"); }
if (strDL.compare(rstr) == 0) { output(x - (nLen - 1), y + (nLen - 1), "右上"); }
if (strDD.compare(rstr) == 0) { output(x , y + (nLen - 1), "上"); }
if (strDR.compare(rstr) == 0) { output(x + (nLen - 1), y + (nLen - 1), "左上"); }
}
}
}
}
int main(int argc, char* argv[])
{
std::vector<std::string> lines;
lines.push_back("ABCBCBCBA");
lines.push_back("ABCBBBCBA");
lines.push_back("ABCBABCBA");
lines.push_back("ABCBBBCBA");
lines.push_back("ABCBCBCBA");
search8way(lines, "ABC");
return 0;
}
|
方法は殆ど変わってないのですが。 下方向へのチェックだと、入力がファイルや標準入力だった時につぶしがきかない(行を先読みしないといけない)ので、上方向に修正。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | void output(int x, int y, std::string v)
{
std::cout << "(" << x << "," << y << "), " << v.c_str() << std::endl;
}
void search8way(std::vector<std::string> lines, std::string str)
{
if (str.empty() != false)
return;
std::string rstr(str);
std::reverse(rstr.begin(), rstr.end());
int nLen = (int)str.size();
char cs = str[0];
char ce = str[nLen - 1];
for (int y = 0; y < (int)lines.size(); y++)
{
std::string line = lines[y];
for (int x = 0; x < (int)line.size(); x++)
{
if ((line[x] != cs) && (line[x] != ce))
continue;
std::string strLU(""); strLU += line[x];
std::string strUP(""); strUP += line[x];
std::string strRU(""); strRU += line[x];
for (int d = 1; (d < nLen) && ((y - d) >= 0); d++)
{
std::string buff = lines[y - d];
if (((int)buff.size() > (x - d)) && ((x - d) >= 0))
strLU += buff[x - d];
if ((int)buff.size() > x)
strUP += buff[x];
if ((int)buff.size() > (x + d))
strRU += buff[x + d];
}
if (line[x] == cs)
{
if (line.compare(x, nLen, str) == 0)
output(x, y, "右");
if (strLU.compare(str) == 0) { output(x, y, "左上"); }
if (strUP.compare(str) == 0) { output(x, y, "上" ); }
if (strRU.compare(str) == 0) { output(x, y, "右上"); }
}
if (line[x] == ce)
{
if (line.compare(x, nLen, rstr) == 0)
output(x + (nLen - 1), y, "左");
if (strRU.compare(rstr) == 0)
output(x + (nLen - 1), y - (nLen - 1), "左下");
if (strUP.compare(rstr) == 0)
output(x , y - (nLen - 1), "下");
if (strLU.compare(rstr) == 0)
output(x - (nLen - 1), y - (nLen - 1), "右下");
}
}
}
}
|




kuromin #4400() Rating0/2=0.00
[ reply ]