fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. #include <fstream>
  5. #include <string>
  6. #include <sstream>
  7. #include <iterator>
  8. #include <map>
  9. #include <set>
  10. #include <algorithm>
  11. #include <functional>
  12.  
  13. using namespace std;
  14.  
  15. typedef std::pair<int,int> cell;
  16.  
  17. enum island_status {COMPLETE,INCOMPLETE};
  18. enum color {WHITE = -2, BLACK = -1 };
  19.  
  20. class island
  21. {
  22. public:
  23. std::set<cell> cells;
  24. island_status status;
  25. };
  26. class nurikabe
  27. {
  28. std::vector<std::vector<int>> grid;
  29. std::map<int,island*> islands;
  30. unsigned int l1,l2;
  31. void color_black(cell);
  32. public:
  33. nurikabe();
  34. nurikabe(std::string);
  35. void solve();
  36. void print();
  37. void find_islands();
  38. unsigned int any_complete_island();
  39. int fill_adjacent(cell);
  40. };
  41.  
  42.  
  43.  
  44. void nurikabe::solve()
  45. {
  46. find_islands();
  47. unsigned int i = 0;
  48. do
  49. {
  50. i = 0;
  51. i += any_complete_island();
  52. cout<<i<<" islands complete";
  53. }while(i != 0);
  54.  
  55. }
  56.  
  57. void nurikabe::find_islands()
  58. {
  59. int i,j;
  60. for(i = 0;i < l1; ++i)
  61. {
  62. for(j = 0;j < l2; ++j)
  63. {
  64. if( grid[i][j] > 0)
  65. {
  66. islands[grid[i][j]] = new island;
  67. islands[grid[i][j]]->status = INCOMPLETE;
  68. islands[grid[i][j]]->cells.insert(make_pair(i,j));
  69. }
  70. }
  71. }
  72. }
  73. unsigned int nurikabe::any_complete_island()
  74. {
  75. int i =0;
  76. auto it = islands.cbegin();
  77. auto end = islands.cend();
  78. for( ;it != end ; ++it)
  79. {
  80. auto & an_island = (*it).second;
  81. if( an_island->status == INCOMPLETE && (*it).first == an_island->cells.size() )
  82. {
  83. ++i;
  84. an_island->status = COMPLETE;
  85. for_each(an_island->cells.cbegin(),an_island->cells.cend(),std::bind(&nurikabe::fill_adjacent,this));
  86. }
  87. };
  88. return i;
  89. }
  90.  
  91. int nurikabe::fill_adjacent(cell c)
  92. {
  93. color_black(make_pair(c.first,c.second+1));
  94. color_black(make_pair(c.first+1,c.second));
  95. color_black(make_pair(c.first,c.second-1));
  96. color_black(make_pair(c.first-1,c.second));
  97.  
  98. return 0;
  99. }
  100.  
  101. void nurikabe::color_black(cell c)
  102. {
  103. if(grid[c.first][c.second] != WHITE)
  104. grid[c.first][c.second] = BLACK;
  105. }
  106. nurikabe::nurikabe(string filename)
  107. {
  108. fstream input (filename);
  109. string aline;
  110.  
  111. unsigned int size =0;
  112. while(input)
  113. {
  114. getline(input,aline);
  115. if(aline.size() > 0)
  116. {
  117. istringstream line(aline);
  118. istream_iterator<int> begin(line), end;
  119. grid.push_back(vector<int>(begin,end));
  120. }
  121. }
  122. l2 = grid[0].size();
  123. l1 = grid.size();
  124. }
  125.  
  126. void nurikabe::print()
  127. {
  128. //print out the puzzle
  129. for( auto i = grid.cbegin();i != grid.cend();i++)
  130. {
  131. cout<<endl;
  132. auto row = *i;
  133. for( auto j = row.cbegin();j != row.cend();j++)
  134. {
  135. cout<<*j<<",";
  136. }
  137. }
  138. cout<<"\n"<<islands.size()<<" Islands present.";
  139. }
  140. int main()
  141. {
  142. nurikabe mynurikabe("D:\\Nurikabe.txt");
  143. mynurikabe.solve();
  144. mynurikabe.print();
  145. return 0;
  146. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/algorithm:63:0,
                 from prog.cpp:10:
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h: In function '_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = std::_Rb_tree_const_iterator<std::pair<int, int> >, _Funct = std::_Bind<std::_Mem_fn<int (nurikabe::*)(std::pair<int, int>)>(nurikabe*)>]':
prog.cpp:85:103:   instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:4185:2: error: no match for call to '(std::_Bind<std::_Mem_fn<int (nurikabe::*)(std::pair<int, int>)>(nurikabe*)>) (const std::pair<int, int>&)'
stdout

Standard output is empty