Skip to content

Instantly share code, notes, and snippets.

@mashiro
Created April 5, 2010 01:52
Show Gist options
  • Select an option

  • Save mashiro/355910 to your computer and use it in GitHub Desktop.

Select an option

Save mashiro/355910 to your computer and use it in GitHub Desktop.
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
std::vector<std::size_t> make_diffs(std::size_t n1)
{
std::vector<std::size_t> v;
v.push_back(n1);
return v;
}
std::vector<std::size_t> make_diffs(std::size_t n1, std::size_t n2)
{
std::vector<std::size_t> v;
v.push_back(n1);
v.push_back(n2);
return v;
}
std::vector<std::size_t> make_diffs(std::size_t n1, std::size_t n2, std::size_t n3)
{
std::vector<std::size_t> v;
v.push_back(n1);
v.push_back(n2);
v.push_back(n3);
return v;
}
bool find(const std::string& tehai, const std::vector<std::size_t>& diffs,
bool is_machi, std::string& tehai_out, std::string& result_out)
{
std::string tehai_tmp = tehai;
std::string result_tmp;
const char base = tehai_tmp[0];
tehai_tmp.erase(0, 1);
result_tmp += is_machi ? "[" : "(";
result_tmp += base;
for (std::size_t i = 1; i < diffs.size(); ++i)
{
const std::size_t pos = tehai_tmp.find_first_of(base + diffs[i]);
if (pos == -1)
return false;
result_tmp += tehai_tmp[pos];
tehai_tmp.erase(pos, 1);
}
result_tmp += is_machi ? "]" : ")";
tehai_out = tehai_tmp;
result_out = result_tmp;
return true;
}
void machi(const std::string& tehai, bool has_atama, bool has_machi, const std::string& result)
{
if (tehai.empty())
{
if (has_atama && has_machi)
{
// OK
std::cout << result << std::endl;
}
}
else
{
std::string tehai_tmp;
std::string result_tmp;
// 刻子
if (find(tehai, make_diffs(0, 0, 0), false, tehai_tmp, result_tmp))
machi(tehai_tmp, has_atama | false, has_machi | false, result + result_tmp);
// 順子
if (find(tehai, make_diffs(0, 1, 2), false, tehai_tmp, result_tmp))
machi(tehai_tmp, has_atama | false, has_machi | false, result + result_tmp);
// 頭
if (!has_atama && find(tehai, make_diffs(0, 0), false, tehai_tmp, result_tmp))
machi(tehai_tmp, has_atama | true, has_machi | false, result + result_tmp);
// 単騎待ち
if (!has_atama && !has_machi && find(tehai, make_diffs(0), true, tehai_tmp, result_tmp))
machi(tehai_tmp, has_atama | true, has_machi | true, result + result_tmp);
// 刻子待ち
if (!has_machi && find(tehai, make_diffs(0, 0), true, tehai_tmp, result_tmp))
machi(tehai_tmp, has_atama | false, has_machi | true, result + result_tmp);
// 両面待ち, 辺張待ち
if (!has_machi && find(tehai, make_diffs(0, 1), true, tehai_tmp, result_tmp))
machi(tehai_tmp, has_atama | false, has_machi | true, result + result_tmp);
// 嵌張待ち
if (!has_machi && find(tehai, make_diffs(0, 2), true, tehai_tmp, result_tmp))
machi(tehai_tmp, has_atama | false, has_machi | true, result + result_tmp);
}
}
void machi(const std::string& tehai)
{
machi(tehai, false, false, "");
}
int main(int argc, char* argv[])
{
#if defined(DEBUG) || defined(_DEBUG)
std::vector<std::string> tests;
tests.push_back("1112224588899");
tests.push_back("1112223335559");
tests.push_back("1223344888999");
tests.push_back("1112345678999");
for (std::size_t i = 0; i < tests.size(); ++i)
{
std::string tehai = tests[i];
std::sort(tehai.begin(), tehai.end());
machi(tehai);
std::cout << std::endl;
}
#else
while (1)
{
std::string tehai;
std::cout << "input: ";
std::cin >> tehai;
if (tehai == "exit")
break;
std::sort(tehai.begin(), tehai.end());
machi(tehai);
std::cout << std::endl;
}
#endif
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment