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. Android开发--二维码开发应用(转载!)

    android项目开发 二维码扫描   基于android平台的二维码扫描项目,可以查看结果并且链接网址 工具/原料 zxing eclipse 方法/步骤   首先需要用到google提供的zxin ...

  2. 解决右滑返回手势和UIScrollView中的手势冲突

    当在一个viewController中添加了scrollView或者tableView的时候,贴边侧滑返回的时候会首先触发滚动而失效,要解决这个问题,需要通过requireGestureRecogni ...

  3. 平衡搜索树(二) Rb 红黑树

    Rb树简介 红黑树是一棵二叉搜索树,它在每个节点上增加了一个存储位来表示节点的颜色,可以是Red或Black.通过对任何一条从根到叶子简单 路径上的颜色来约束,红黑树保证最长路径不超过最短路径的两倍, ...

  4. 330. Patching Array--Avota

    问题描述: Given a sorted positive integer array nums and an integer n, add/patch elements to the array s ...

  5. gSoap实现ONVIF中xsd__anyType到具体结构类型的转换

    上一篇文章已经粗略计划要讨论gsoap关于序列化/解析编程. 本文则阐述一下关于gsoap生成代码的一些重要特征方法及使用.如题,下我们从ONVIF生成的C码中,挑选简单的一个类型来试验一下与xsd_ ...

  6. delphi定义自己的消息

    定义一个消息需要两个步骤: 1.声明一个消息标识符 2.声明一个消息记录类型 一个消息标识符是一个整数大小的常数.Windows自用低于1024的消息,所以当你声明你自己的消息,你应该开始高于这一数字 ...

  7. 那些年被我坑过的Python——山外有山(第四章)

    装饰器: 定义: 本质是函数,(装饰其他函数)就是为其他函数添加附加功能原则: 1.不能修改被装饰的函数的源代码 2.不能修改被装饰的函数的调用方式 优点: 装饰器带来的最直观的好处:减少对函数的细化 ...

  8. 在Adobe Reader中保存PDF表单数据的方法

    通常,Adobe Reader 用户填写表单后,是无法保存所填表单的副本的.但是,对于 Reader 8 和更高版本的用户,您可以扩展其权限,使他们可以完成此操作.如果您使用的是 Acrobat Pr ...

  9. 使用模版引擎填充重复dom元素

    引入arttemplate,定义newajax发送跨域请求获得数据,将获得的数据用定义的格式渲染 <!DOCTYPE html><html lang="en"&g ...

  10. 关于set或map的key使用自定义类型的问题

    我们都知道set或map的key使用自定义类型时必须重载<关系运算符 但是,还有一个条件,所调用重载的小于操作符,使用的对象必须是const 而对象调用的方法也必须是const的 1 #incl ...