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