码迷,mamicode.com
首页 > 其他好文 > 详细

2.1.1 The Castle

时间:2015-06-01 22:06:37      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

简单的观察可以发现数字与墙之间有如下规律:
15-东西南北 14-东南北 13-东西南 12-东南 11-西北南 10-北南 
9-西南 8-南 7-东西北 6-东北 5-东西 4-东 3-西北 2-北 1-西 
注意:0也是, 就是周围没有墙。

主要是用flood fill,但是有一些细节:重载<的时候,扫描的时候。代码量有点大,不过思维量小,速度也还可以。

  1. /*
  2. ID: awsd1231
  3. PROG: castle
  4. LANG: C++
  5. */
  6. #include<iostream>
  7. #include<cstdio>
  8. #include<vector>
  9. #include<algorithm>
  10. using namespace std;
  11. struct T {
  12. int color;
  13. int value;
  14. T() : color(0) {}//初始化每个color为0
  15. }cas[50][50];
  16. int m, n;
  17. int c[2501] = {0};//统计每种颜色个数
  18. struct T2 {
  19. int s, x, y, turn;//turn 0down 1right
  20. }tmp;
  21. vector<T2> ans;
  22. bool operator < (T2 a, T2 b) {
  23. if(a.x != b.x) return a.x < b.x;
  24. if(a.y != b.y)return a.y > b.y;
  25. return a.turn < b.turn;
  26. }
  27. void floodFill(int x, int y, int color) {
  28. ++c[color];
  29. cas[y][x].color = color;
  30. switch (cas[y][x].value) {
  31. case 14 : if (!cas[y][x - 1].color) //left
  32. floodFill(x - 1, y, color);
  33. break;
  34. case 13 : if (!cas[y - 1][x].color) //up
  35. floodFill(x, y - 1, color);
  36. break;
  37. case 12 : if (!cas[y][x - 1].color) //left
  38. floodFill(x - 1, y, color);
  39. if (!cas[y - 1][x].color) //up
  40. floodFill(x, y - 1, color);
  41. break;
  42. case 11 : if (!cas[y][x + 1].color) // right
  43. floodFill(x + 1, y, color);
  44. break;
  45. case 10 : if (!cas[y][x - 1].color) // left
  46. floodFill(x - 1, y, color);
  47. if (!cas[y][x + 1].color) // right
  48. floodFill(x + 1, y, color);
  49. break;
  50. case 9 : if (!cas[y][x + 1].color) // right
  51. floodFill(x + 1, y, color);
  52. if (!cas[y - 1][x].color) // up
  53. floodFill(x, y - 1, color);
  54. break;
  55. case 8 : if (!cas[y][x - 1].color) // left
  56. floodFill(x - 1, y, color);
  57. if (!cas[y][x + 1].color) // right
  58. floodFill(x + 1, y, color);
  59. if (!cas[y - 1][x].color) // up
  60. floodFill(x, y - 1, color);
  61. break;
  62. case 7 : if (!cas[y + 1][x].color) // down
  63. floodFill(x, y + 1, color);
  64. break;
  65. case 6 : if (!cas[y][x - 1].color)// left
  66. floodFill(x - 1, y, color);
  67. if (!cas[y + 1][x].color)//down
  68. floodFill(x, y + 1, color);
  69. break;
  70. case 5 : if (!cas[y - 1][x].color) // up
  71. floodFill(x, y - 1, color);
  72. if (!cas[y + 1][x].color) // down
  73. floodFill(x, y + 1, color);
  74. break;
  75. case 4 : if (!cas[y][x - 1].color) // left
  76. floodFill(x - 1, y, color);
  77. if (!cas[y - 1][x].color) // up
  78. floodFill(x, y - 1, color);
  79. if (!cas[y + 1][x].color) // down
  80. floodFill(x, y + 1, color);
  81. break;
  82. case 3 : if (!cas[y][x + 1].color) // right
  83. floodFill(x + 1, y, color);
  84. if (!cas[y + 1][x].color) // down
  85. floodFill(x, y + 1, color);
  86. break;
  87. case 2 : if (!cas[y][x + 1].color) // right
  88. floodFill(x + 1, y, color);
  89. if (!cas[y][x - 1].color) // left
  90. floodFill(x - 1, y, color);
  91. if (!cas[y + 1][x].color) // down
  92. floodFill(x, y + 1, color);
  93. break;
  94. case 1 : if (!cas[y][x + 1].color) // right
  95. floodFill(x + 1, y, color);
  96. if (!cas[y + 1][x].color) // down
  97. floodFill(x, y + 1, color);
  98. if (!cas[y - 1][x].color) // up
  99. floodFill(x, y - 1, color);
  100. break;
  101. case 0 : if (!cas[y][x + 1].color) // right
  102. floodFill(x + 1, y, color);
  103. if (!cas[y][x - 1].color) // left
  104. floodFill(x - 1, y, color);
  105. if (!cas[y + 1][x].color) // down
  106. floodFill(x, y + 1, color);
  107. if (!cas[y - 1][x].color) // up
  108. floodFill(x, y - 1, color);
  109. break;
  110. }
  111. }
  112. int main() {
  113. freopen("castle.in", "r", stdin);
  114. freopen("castle.out", "w", stdout);
  115. scanf("%d%d", &m, &n);
  116. for (int i = 0; i != n; ++i) {
  117. for (int j = 0; j != m; ++j) {
  118. scanf("%d", &cas[i][j].value);
  119. }
  120. }
  121. int co(0);//赋颜色co
  122. for (int i = 0; i != n; ++i) {
  123. for (int j = 0; j != m; ++j) {
  124. if (cas[i][j].color == 0)
  125. floodFill(j, i, ++co);
  126. }
  127. }
  128. int maxS(0);
  129. for (int i = 0; i != co; ++i) {
  130. if(maxS < c[i + 1]) maxS = c[i + 1];
  131. }
  132. cout << co << endl << maxS << endl;
  133. tmp.s = tmp.x = tmp.y = tmp.turn = -1;
  134. ans.push_back(tmp);//初始化,让ans不为空
  135. for (int i = 0; i != n; ++i) {
  136. for (int j = 0; j != m - 1; ++j) {
  137. if (cas[i][j].color != cas[i][j + 1].color) {
  138. int S = c[cas[i][j].color] + c[cas[i][j + 1].color];
  139. if (S >= ans[0].s) {
  140. tmp.s = S;
  141. tmp.x = j;
  142. tmp.y = i;
  143. tmp.turn = 1;
  144. if (S > ans[0].s) ans.clear();
  145. ans.push_back(tmp);
  146. }
  147. }
  148. }
  149. }
  150. for (int i = n; i != 1; --i) {
  151. for (int j = 0; j != m; ++j) {
  152. if(cas[i][j].color != cas[i - 1][j].color) {
  153. int S = c[cas[i][j].color] + c[cas[i - 1][j].color];
  154. if (S >= ans[0].s) {
  155. tmp.s = S;
  156. tmp.x = j;
  157. tmp.y = i;
  158. tmp.turn = 0;
  159. if (S > ans[0].s) {
  160. ans.clear();
  161. }
  162. ans.push_back(tmp);
  163. }
  164. }//cout << "***" << endl;// 可以执行
  165. }//cout << "**" << endl;
  166. }
  167. cout << ans[0].s << endl;
  168. sort(ans.begin(), ans.end());
  169. cout << ans[0].y + 1 << " " << ans[0].x + 1 << " ";
  170. if(ans[0].turn) cout << "E" << endl;
  171. else cout << "N" << endl;
  172. return 0;
  173. }





2.1.1 The Castle

标签:

原文地址:http://www.cnblogs.com/liangyongrui/p/4544861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!