DIV2 1000pt
题意:对于一个n*m的矩阵,每个格子都有一个颜色B或者W。对矩阵A执行以下程序后变成矩阵B。给出矩阵B,求A。(若有多种情况,输出字典序最小的)。(n,m <= 16)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 For i = 0 to H-2: 2 For j = 0 to W-2: 3 //Get the current colors of cells (i,j) and (i,j+1) 4 A = Color(i,j) , B = Color(i,j+1) 5 6 If (A,B) == (White, White) Then: 7 Do nothing. 8 EndIf 9 If (A,B) == (Black, White) Then: 10 Repaint cells (i+1,j) and (i+1,j+1) Black.11 EndIf12 If (A,B) == (White, Black) Then:13 Repaint cells (i+1,j) and (i+1,j+1) White.14 EndIf15 if (A,B) == (Black, Black) Then:16 Swap the colors in cells (i+1,j) and (i+1,j+1).17 EndIf18 EndFor19 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
法一:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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 #include12 #include 13 #include 14 #include 15 #include 16 #include 17 #include 18 #include 19 #include 20 #include 21 #include 22 #include 23 #include 24 #include 25 #include 26 #include 27 #include 28 #include 29 #include
30 #include 31 #include