DIV2 1000pt

题意:对于一个n*m的矩阵,每个格子都有一个颜色B或者W。对矩阵A执行以下程序后变成矩阵B。给出矩阵B,求A。(若有多种情况,输出字典序最小的)。(n,m <= 16)

  1. For i = to H-:
  2. For j = to W-:
  3. //Get the current colors of cells (i,j) and (i,j+1)
  4. A = Color(i,j) , B = Color(i,j+)
  5.  
  6. If (A,B) == (White, White) Then:
  7. Do nothing.
  8. EndIf
  9. If (A,B) == (Black, White) Then:
  10. Repaint cells (i+,j) and (i+,j+) Black.
  11. EndIf
  12. If (A,B) == (White, Black) Then:
  13. Repaint cells (i+,j) and (i+,j+) White.
  14. EndIf
  15. if (A,B) == (Black, Black) Then:
  16. Swap the colors in cells (i+,j) and (i+,j+).
  17. EndIf
  18. EndFor
  19. EndFor

解法:

   法一,水解:首先注意到两点,一是在读取矩阵A的第i行的时候,对其第i+1行进行操作,二是矩阵A和矩阵B的第一行必然相同。

   所以,直接暴力DFS即可。由于每行最多只有16个,所以可以压缩状态来做。

   法二:比较考查思维的方法。首先仍然要注意到,读第i行时处理第i+1行,所以每行可以单独分析。其次,在读i处理i+1行时,有两种操作,对i+1行的某两个格子染色或者交换他们的颜色。若在读i行时对i+1行的某两个格子染色,则他们之前(在A矩阵中的时候)是什么颜色就不影响了,考虑到要字典序最小,所以应该让他们颜色为'B'。而其他没有被染色的格子,他们需要与变换后的位置的相应元素相同。

   这样的方法很巧,而且倒着写比较好写。

tag:think, good

法一:

  1. // BEGIN CUT HERE
  2. /*
  3. * Author: plum rain
  4. * score :
  5. */
  6. /*
  7.  
  8. */
  9. // END CUT HERE
  10. #line 11 "Algrid.cpp"
  11. #include <sstream>
  12. #include <stdexcept>
  13. #include <functional>
  14. #include <iomanip>
  15. #include <numeric>
  16. #include <fstream>
  17. #include <cctype>
  18. #include <iostream>
  19. #include <cstdio>
  20. #include <vector>
  21. #include <cstring>
  22. #include <cmath>
  23. #include <algorithm>
  24. #include <cstdlib>
  25. #include <set>
  26. #include <queue>
  27. #include <bitset>
  28. #include <list>
  29. #include <string>
  30. #include <utility>
  31. #include <map>
  32. #include <ctime>
  33. #include <stack>
  34.  
  35. using namespace std;
  36.  
  37. #define CLR(x) memset(x, 0, sizeof(x))
  38. #define CLR1(x) memset(x, -1, sizeof(x))
  39. #define PB push_back
  40. #define SZ(v) ((int)(v).size())
  41. #define zero(x) (((x)>0?(x):-(x))<eps)
  42. #define out(x) cout<<#x<<":"<<(x)<<endl
  43. #define tst(a) cout<<#a<<endl
  44. #define CINBEQUICKER std::ios::sync_with_stdio(false)
  45.  
  46. typedef vector<int> VI;
  47. typedef vector<string> VS;
  48. typedef vector<double> VD;
  49. typedef long long int64;
  50.  
  51. const double eps = 1e-;
  52. const double PI = atan(1.0)*;
  53. const int maxint = ;
  54.  
  55. int n, all, len;
  56. bool flag;
  57. int a[], ans[];
  58.  
  59. int change(int x, int y)
  60. {
  61. if (x == -) return -;
  62.  
  63. int ta[], tb[];
  64. int idxa = , idxb = ;
  65. for (int i = ; i < len; ++ i){
  66. ta[idxa++] = x & ;
  67. x >>= ;
  68. tb[idxb++] = y & ;
  69. y >>= ;
  70. }
  71. for (int i = len-; i; -- i){
  72. if (!ta[i] && ta[i-]) tb[i] = tb[i-] = ;
  73. if (ta[i] && !ta[i-]) tb[i] = tb[i-] = ;
  74. if (!ta[i] && !ta[i-]) swap(tb[i], tb[i-]);
  75. }
  76. int ret = ;
  77. for (int i = len-; i >= ; -- i)
  78. ret += (tb[i] << i);
  79. return ret;
  80. }
  81.  
  82. void DFS (int x)
  83. {
  84. if (x == n){
  85. flag = ;
  86. return;
  87. }
  88.  
  89. for (int i = ; i < all; ++ i)
  90. if (!flag && change(a[x-], i) == a[x]){
  91. ans[x] = i;
  92. DFS (x+);
  93. if (!flag) ans[x] = -;
  94. }
  95. }
  96.  
  97. class Algrid
  98. {
  99. public:
  100. vector <string> makeProgram(vector <string> A){
  101. len = A[].size();
  102. n = A.size(); all = << len;
  103. for (int i = ; i < n; ++ i){
  104. int tmp = ;
  105. for (int j = len-, k = ; j >= ; -- j, ++ k)
  106. if (A[i][j] == 'W'){
  107. tmp += ( << k);
  108. }
  109. a[i] = tmp;
  110. }
  111.  
  112. CLR1 (ans);
  113. ans[] = a[];
  114.  
  115. flag = ;
  116. DFS ();
  117.  
  118. vector<string> ret; ret.clear();
  119. string tmp;
  120. for (int i = ; i < n; ++ i){
  121. if (ans[i] == -){
  122. ret.clear(); return ret;
  123. }
  124.  
  125. tmp.clear();
  126. for (int j = len-; j >= ; -- j){
  127. if (ans[i] & (<<j)) tmp.PB ('W');
  128. else tmp.PB('B');
  129. }
  130. ret.PB (tmp);
  131. }
  132. return ret;
  133. }
  134.  
  135. // BEGIN CUT HERE
  136. public:
  137. void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); }
  138. //void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0();}
  139. private:
  140. template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
  141. void verify_case(int Case, const vector <string> &Expected, const vector <string> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
  142. void test_case_0() { string Arr0[] = {"WWBBB", "WBBBW"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); string Arr1[] = {"WWWWWWW", "WWWWWWB", "BBBBBBB" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, makeProgram(Arg0)); }
  143. void test_case_1() { string Arr0[] = {"BBBBB",
  144. "WBWBW"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); string Arr1[] = {"BBBBB", "WWBWB" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, makeProgram(Arg0)); }
  145. void test_case_2() { string Arr0[] = {"BBBB",
  146. "BBBB",
  147. "BBWB",
  148. "WWBB",
  149. "BWBB"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); string Arr1[] = { }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, makeProgram(Arg0)); }
  150. void test_case_3() { string Arr0[] = {"WWBBBBW",
  151. "BWBBWBB",
  152. "BWBBWBW",
  153. "BWWBWBB"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); string Arr1[] = {"WWBBBBW", "BBBBBWB", "BBBBBBB", "BBBWBBB" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, makeProgram(Arg0)); }
  154.  
  155. // END CUT HERE
  156.  
  157. };
  158.  
  159. // BEGIN CUT HERE
  160. int main()
  161. {
  162. // freopen( "a.out" , "w" , stdout );
  163. Algrid ___test;
  164. ___test.run_test(-);
  165. return ;
  166. }
  167. // END CUT HERE

法二:

  1. // BEGIN CUT HERE
  2. /*
  3. * Author: plum rain
  4. * score :
  5. */
  6. /*
  7.  
  8. */
  9. // END CUT HERE
  10. #line 11 "Algrid.cpp"
  11. #include <sstream>
  12. #include <stdexcept>
  13. #include <functional>
  14. #include <iomanip>
  15. #include <numeric>
  16. #include <fstream>
  17. #include <cctype>
  18. #include <iostream>
  19. #include <cstdio>
  20. #include <vector>
  21. #include <cstring>
  22. #include <cmath>
  23. #include <algorithm>
  24. #include <cstdlib>
  25. #include <set>
  26. #include <queue>
  27. #include <bitset>
  28. #include <list>
  29. #include <string>
  30. #include <utility>
  31. #include <map>
  32. #include <ctime>
  33. #include <stack>
  34.  
  35. using namespace std;
  36.  
  37. #define CLR(x) memset(x, 0, sizeof(x))
  38. #define CLR1(x) memset(x, -1, sizeof(x))
  39. #define PB push_back
  40. #define SZ(v) ((int)(v).size())
  41. #define zero(x) (((x)>0?(x):-(x))<eps)
  42. #define out(x) cout<<#x<<":"<<(x)<<endl
  43. #define tst(a) cout<<#a<<endl
  44. #define CINBEQUICKER std::ios::sync_with_stdio(false)
  45.  
  46. typedef vector<int> VI;
  47. typedef vector<string> VS;
  48. typedef vector<double> VD;
  49. typedef long long int64;
  50.  
  51. const double eps = 1e-;
  52. const double PI = atan(1.0)*;
  53. const int maxint = ;
  54.  
  55. class Algrid
  56. {
  57. public:
  58. vector <string> makeProgram(vector <string> opt){
  59. vector<string> tmp; tmp.clear();
  60. int n = opt.size(), m = opt[].size();
  61. for (int i = n-; i >= ; -- i){
  62. for (int j = m-; j >= ; -- j){
  63. char a = opt[i][j], b = opt[i][j+];
  64. char &c = opt[i+][j], &d = opt[i+][j+];
  65.  
  66. if (a == 'B' && b == 'B')
  67. swap (c, d);
  68. if (a == 'B' && b == 'W'){
  69. if (c == 'W' || d == 'W') return tmp;
  70. else d = c = '?';
  71. }
  72. if (a == 'W' && b == 'B'){
  73. if (c == 'B' || d == 'B') return tmp;
  74. else c = d = '?';
  75. }
  76. }
  77. replace (opt[i+].begin(), opt[i+].end(), '?', 'B');
  78. }
  79. return opt;
  80. }
  81.  
  82. // BEGIN CUT HERE
  83. public:
  84. void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); }
  85. private:
  86. template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
  87. void verify_case(int Case, const vector <string> &Expected, const vector <string> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
  88. void test_case_0() { string Arr0[] = {"WWWWWWW",
  89. "WWWWWWB",
  90. "BBBBBWW"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); string Arr1[] = {"WWWWWWW", "WWWWWWB", "BBBBBBB" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, makeProgram(Arg0)); }
  91. void test_case_1() { string Arr0[] = {"BBBBB",
  92. "WBWBW"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); string Arr1[] = {"BBBBB", "WWBWB" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, makeProgram(Arg0)); }
  93. void test_case_2() { string Arr0[] = {"BBBB",
  94. "BBBB",
  95. "BBWB",
  96. "WWBB",
  97. "BWBB"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); string Arr1[] = { }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, makeProgram(Arg0)); }
  98. void test_case_3() { string Arr0[] = {"WWBBBBW",
  99. "BWBBWBB",
  100. "BWBBWBW",
  101. "BWWBWBB"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[]))); string Arr1[] = {"WWBBBBW", "BBBBBWB", "BBBBBBB", "BBBWBBB" }; vector <string> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[]))); verify_case(, Arg1, makeProgram(Arg0)); }
  102.  
  103. // END CUT HERE
  104.  
  105. };
  106.  
  107. // BEGIN CUT HERE
  108. int main()
  109. {
  110. // freopen( "a.out" , "w" , stdout );
  111. Algrid ___test;
  112. ___test.run_test(-);
  113. return ;
  114. }
  115. // END CUT HERE

SRM 504(2-1000pt)的更多相关文章

  1. SRM 504.5(2-1000pt)

    DIV2 1000pt 题意:一群人排队,每次操作由要骰子决定,只要没有人中奖,游戏就不结束.若摇骰子摇出4,则队列第一个人中奖:否则,若摇的是奇数,则第一个人排队到队伍末尾去:否则,第一个人出局.若 ...

  2. TC250专场

    SRM 623 DIV2 1000pt 题意:给出一个最多50*50的矩阵,每个单元可能为'.'.'P'.'A','.'代表空地,你每次操作可以把一个P或者A拿到空地上,求一个最大的含有相同字符的矩形 ...

  3. SRM149 - SRM150(少SRM150-DIV1-LV3)

    SRM 149 DIV2 1000pt 题意: 对于n个人,第i人有pi的钱.将他们分成不超过四个组,每组统一交费x,对每个人,若他拥有的钱超过x则交费,否则不交费.问最多能使这些人交多少钱. 1&l ...

  4. Topcoder 好题推荐

    SRM SRM147 DIV1 1000pt DP SRM148 DIV1 1100pt 递归 SRM149 DIV1 1000pt math SRM150 DIV1 500pt DP SRM469 ...

  5. SRM144 - SRM 148(少144-DIV1-LV3,147-DIV2-LV3)

    SRM 144 DIV 1 500pt tag:组合 题意:彩票中奖.给定n, m,从1-n中选择m个数组成数列a1, a2, a3...am.对于数列{am}分别满足以下条件的概率: (1)数列所有 ...

  6. SRM 508(2-1000pt)

    DIV2 1000pt 题意:给定整数n和r,求有多少个这样的数列,a1,a2...an,使得a1 + a2 +...+an = a1|a2|a3|...|an,(按位或).输出这样数列的个数mod  ...

  7. tomcat 504 gateway time-out

    今天有个环境ajax调用一个请求的时候,出现一个504 gateway time-out响应,原以为是nginx找不到资源的问题,恰当我们的服务器上又配置了nginx,看了配置文件,没有指向tomca ...

  8. 关于php-fpm子进程达到上限并且浏览器访问显示504错误

    今天上班遇到一个非常奇怪的事情,公司监控服务器之前都是在正常运行,使用nginx+php-fpm,并且监控服务器上部署这其他部门在使用的几个站点,从早上上班开始发现监控显示页面打不开,各种查找原因,最 ...

  9. 服务器504——一般情况下是由nginx默认的fastcgi进程响应慢引起的

    情况一解决办法: 默认的fastcgi进程响应的缓冲区是8K,我们可以设置大一点,在nginx.conf里,加入:fastcgi_buffers 8 128k 这表示设置fastcgi缓冲区为8块12 ...

随机推荐

  1. java中怎么进行字符串替换?

    String str = "test.doc"; String newStr = str.replaceAll("doc","html");

  2. SDWebImage 在多线程下载图片时防止错乱的策略

    在我们使用sd的时候,对tableView  上cell得图片进行异步下载的时候会遇到这样一个问题: 由于cell的重用机制,在我们加载出一个cell的时候imageView数据源开启一个下载任务并返 ...

  3. 如何为jquery添加方法

    以下内容引自一位网友的帖子: jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery ...

  4. PHP表单

    二.PHP表单 1.PHP表单处理 welcome.html <html> <body> <form action="welcome.php" met ...

  5. 关于本地计算机无法启动Apache2

    最近因工作需要,要学习PHP的基础编程,于是学习架设PHP工作环境. 但按照教材上介绍的那样,安装了WMAP后,一直无法运行成功.后发现Apache一直都不在运行状态.到WMAP中的Apache选项中 ...

  6. 隐藏和显示 ng-show ng-hide

    <div ng-controller='DeathraymenueController'>    <button ng-click="toggleMenue()" ...

  7. Python 基础 文件操作

    字符串与字节之间的转换 # utf-8 一个汉字 占三个字节 # gbk 一个汉字 占两个字节 # 字符串转换成字节 print(bytes('汉字', encoding='utf-8'))print ...

  8. linux下shapely的安装

    错误 1.“from shapely.geometry import Point, LineString, Polygon”时报错: OSError: Could not find library g ...

  9. Python自动化运维之28、Django(二)

    一.FORM 1.概述 django表单系统中,所有的表单类都作为django.forms.Form的子类创建,包括ModelForm 关于django的表单系统,主要分两种 基于django.for ...

  10. C语言知识总结

    明白一些变量 熟悉一些语句 组合一些函数 C语言——>库  帮你写好放在库中 魔数 凭空变出来的数字,不知道 数字表示的含义,影响代码的可读性. C语音的参数传递 非常特殊,传递的是一个替身. ...