DIV2 1000pt

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

 For i =  to H-:
For j = to W-:
//Get the current colors of cells (i,j) and (i,j+1)
A = Color(i,j) , B = Color(i,j+) If (A,B) == (White, White) Then:
Do nothing.
EndIf
If (A,B) == (Black, White) Then:
Repaint cells (i+,j) and (i+,j+) Black.
EndIf
If (A,B) == (White, Black) Then:
Repaint cells (i+,j) and (i+,j+) White.
EndIf
if (A,B) == (Black, Black) Then:
Swap the colors in cells (i+,j) and (i+,j+).
EndIf
EndFor
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

法一:

 // BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "Algrid.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define CLR1(x) memset(x, -1, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ; int n, all, len;
bool flag;
int a[], ans[]; int change(int x, int y)
{
if (x == -) return -; int ta[], tb[];
int idxa = , idxb = ;
for (int i = ; i < len; ++ i){
ta[idxa++] = x & ;
x >>= ;
tb[idxb++] = y & ;
y >>= ;
}
for (int i = len-; i; -- i){
if (!ta[i] && ta[i-]) tb[i] = tb[i-] = ;
if (ta[i] && !ta[i-]) tb[i] = tb[i-] = ;
if (!ta[i] && !ta[i-]) swap(tb[i], tb[i-]);
}
int ret = ;
for (int i = len-; i >= ; -- i)
ret += (tb[i] << i);
return ret;
} void DFS (int x)
{
if (x == n){
flag = ;
return;
} for (int i = ; i < all; ++ i)
if (!flag && change(a[x-], i) == a[x]){
ans[x] = i;
DFS (x+);
if (!flag) ans[x] = -;
}
} class Algrid
{
public:
vector <string> makeProgram(vector <string> A){
len = A[].size();
n = A.size(); all = << len;
for (int i = ; i < n; ++ i){
int tmp = ;
for (int j = len-, k = ; j >= ; -- j, ++ k)
if (A[i][j] == 'W'){
tmp += ( << k);
}
a[i] = tmp;
} CLR1 (ans);
ans[] = a[]; flag = ;
DFS (); vector<string> ret; ret.clear();
string tmp;
for (int i = ; i < n; ++ i){
if (ans[i] == -){
ret.clear(); return ret;
} tmp.clear();
for (int j = len-; j >= ; -- j){
if (ans[i] & (<<j)) tmp.PB ('W');
else tmp.PB('B');
}
ret.PB (tmp);
}
return ret;
} // BEGIN CUT HERE
public:
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(); }
//void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0();}
private:
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(); }
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; } }
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)); }
void test_case_1() { string Arr0[] = {"BBBBB",
"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)); }
void test_case_2() { string Arr0[] = {"BBBB",
"BBBB",
"BBWB",
"WWBB",
"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)); }
void test_case_3() { string Arr0[] = {"WWBBBBW",
"BWBBWBB",
"BWBBWBW",
"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)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
Algrid ___test;
___test.run_test(-);
return ;
}
// END CUT HERE

法二:

 // BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "Algrid.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define CLR(x) memset(x, 0, sizeof(x))
#define CLR1(x) memset(x, -1, sizeof(x))
#define PB push_back
#define SZ(v) ((int)(v).size())
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int maxint = ; class Algrid
{
public:
vector <string> makeProgram(vector <string> opt){
vector<string> tmp; tmp.clear();
int n = opt.size(), m = opt[].size();
for (int i = n-; i >= ; -- i){
for (int j = m-; j >= ; -- j){
char a = opt[i][j], b = opt[i][j+];
char &c = opt[i+][j], &d = opt[i+][j+]; if (a == 'B' && b == 'B')
swap (c, d);
if (a == 'B' && b == 'W'){
if (c == 'W' || d == 'W') return tmp;
else d = c = '?';
}
if (a == 'W' && b == 'B'){
if (c == 'B' || d == 'B') return tmp;
else c = d = '?';
}
}
replace (opt[i+].begin(), opt[i+].end(), '?', 'B');
}
return opt;
} // BEGIN CUT HERE
public:
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(); }
private:
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(); }
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; } }
void test_case_0() { string Arr0[] = {"WWWWWWW",
"WWWWWWB",
"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)); }
void test_case_1() { string Arr0[] = {"BBBBB",
"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)); }
void test_case_2() { string Arr0[] = {"BBBB",
"BBBB",
"BBWB",
"WWBB",
"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)); }
void test_case_3() { string Arr0[] = {"WWBBBBW",
"BWBBWBB",
"BWBBWBW",
"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)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
Algrid ___test;
___test.run_test(-);
return ;
}
// 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. Spring MVC返回json数据给Android端

    原先做Android项目时,服务端接口一直是别人写的,自己拿来调用一下,但下个项目,接口也要自己搞定了,我想用Spring MVC框架来提供接口,这两天便抽空浅学了一下该框架以及该框架如何返回json ...

  2. WCF存储图片到指定文件夹下

    string path = System.IO.Directory.GetCurrentDirectory() + @"\POIImages\"; Guid imgid = Gui ...

  3. Asp.Net 注册 邮箱激活

    数据库 表的设计 State为用户状态  0为禁用  1为可用  默认为0,下面有个UserGUID,这个字段将来用于激活账户 首先你要写一个表单,验证码神马的,这个我就不写了..直接写处理的 代码在 ...

  4. Delphi ControlCount和ComponentCount的区别

    ComponentCount指打开的窗体所拥有的控件个数,包含所有子组件.孙组件(子组件内的子组件) 如上图,Form1的ComponentCount是13,而Panel1的ComponentCoun ...

  5. MATLAB中mexFunction函数的接口规范

    MEX文件的调用极为方便,其调用方式与MATALAB的内建函数完全相同,只需要在命令窗口内输入对应的文件名称即可. C语言MEX程序代码文件有计算子例程(Computational routine)和 ...

  6. 线程取消 (pthread_cancel)

    线程取消(pthread_cancel) 基本概念pthread_cancel调用并不等待线程终止,它只提出请求.线程在取消请求(pthread_cancel)发出后会继续运行,直到到达某个取消点(C ...

  7. 常见的iis日志代码!

    2xx  成功 200  正常:请求已完成. 201  正常:紧接 POST 命令. 202  正常:已接受用于处理,但处理尚未完成. 203  正常:部分信息 — 返回的信息只是一部分. 204   ...

  8. in_array 判断问题的疑惑解决。

    面试题中有一条是关于in_array判断的,题目如下: 如何大家没有深入了解in_array的类型判断过程,而是根据经验来选择,肯定很多人也是是选择了D答案的,具体的原因我也是从牛人的博客里面得到答案 ...

  9. 基于php-fpm的配置详解[转载]

    php自带php-fpm/usr/local/php/etc/php-fpm.confpid = run/php-fpm.pidpid设置,默认在安装目录中的var/run/php-fpm.pid,建 ...

  10. KVO初探

    一,概述 KVO,即:Key-Value Observing,它提供一种机制,当指定的对象的属性被修改后,则对象就会接受到通知.简单的说就是每次指定的被观察的对象的属性被修改后,KVO就会自动通知相应 ...