题意:

又是农夫和牛的故事。。。有m*n个黑白块,黑块的背面是白块,白块背面是黑块,一头牛踩一块,则这个块的上下左右的方块都会转动,问至少踩多少块,才会使所有块都变成白色?

分析:

还是开关问题,同样是一个块的转动会影响其他块的状态,但这次不是简单的线性排列,也不能只踩黑块。首先根据字典序,我们可以对第一排从00…00到11..11进行考虑(1表示踩),再后续一排一排的考虑。因为踩一块四周的块都会转动,所以必须规定个踩的原则,发现对于某块来说,他一旦改变上方块的状态,那个块就再也不会改变了,而其他块还有他的下一列改变他的机会(如果存在),所以就以上一行块为原则,如果上方为黑,则踩。最后判断最后一行是否全白。

字典序,因为他说了是把整个排列当做字符串的字典序,所以肯定是越前面的越小越好,而且从第一个例子中也能看出来。

-

代码:

#include<iostream>
#include<cstring>
using namespace std;
#define mem(s,a) memset(s,a,sizeof(s));
int m, n;
const int maxn = 25, INF = 0x3fffffff;
int x[5]={-1,0,0,0,1};
int y[5] = {0,1,0,-1,0};
int s[maxn][maxn], a[maxn][maxn], r[maxn][maxn], ans[maxn][maxn];
int cal()
{
int cnt = 0;
for(int i = 2; i <= m; i++){
for(int j = 1; j <= n; j++){
if((a[i-1][j]+r[i-1][j])%2==1) {
cnt++;
s[i][j] = 1;
}
for(int k = 0; k < 5; k++){
r[i+x[k]][j+y[k]]+=s[i][j];
}
}
}
/* for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
cout<<(r[m][i]+ a[m][i])%2;
if(j!=n) cout<<' ';
else cout<<endl;
}
}*/
for(int i =1; i <= n; i++){
if((r[m][i]+ a[m][i])%2==1) return -1;
} return cnt;
}
int main (void)
{
cin>>m>>n;
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
cin>>a[i][j];
}
}
int res = INF;
for(int i = 0; i <1<<n; i++){
mem(s,0);mem(r,0);
int t = 0;
for(int j =n; j >= 1; j--){
s[1][j] = i>>j&1;
for(int k = 0; k < 5; k++){
r[1+x[k]][j+y[k]]+=s[1][j];
}
t += s[1][j];
}
int tm = cal();
if(tm>=0&&t+tm<res){
res = t + tm;
memcpy(ans,s,sizeof(s));
}
}
if(res==INF) {
cout<<"IMPOSSIBLE"<<endl;
return 0;
}
for(int i = 1; i <= m; i++){
for(int j = 1; j <= n; j++){
cout<<ans[i][j];
if(j!=n) cout<<' ';
else cout<<endl;
}
}
return 0;
}

最新文章

  1. c#编程基础之ref、out参数
  2. 洛谷 P1182 数列分段Section II Label:贪心
  3. 如何挂自己的web项目(免费拥有自己的网站及域名)
  4. CentOS 6.3下rsync服务器的安装与配置
  5. jquery判断checkbox是否选中及改变checkbox状态
  6. Run P4 without P4factory - A Simple Example In Tutorials. -2
  7. [问题2014A06] 复旦高等代数 I(14级)每周一题(第八教学周)
  8. JAVA UDP网络编程学习笔记
  9. HUST 1017 Exact cover dance links
  10. How to: Reading an ini config file from a batch file
  11. Java多线程中易混淆的概念
  12. 数据库关于group by 两个或以上条件的分析
  13. 使用jquery实现放大镜效果
  14. android — JNI注册方法说明
  15. 添加Pods依赖
  16. GoLang(第一篇 安装)
  17. 生肖年(switch练习)
  18. springcloud(二):注册中心Eureka
  19. Groovy - 介绍
  20. JavaScript ES6中export及export default的区别

热门文章

  1. NSoup获取网页源代码
  2. 阿里云服务器 Access to the path &#39;....&#39; is denied.解决方法
  3. JVM 优点与缺点的深入分析
  4. Android(java)学习笔记202:JNI之hello.c(c代码功能实现)指针语法解析
  5. XML和JSON
  6. Perl语言入门:第七章习题:输出文件中包含一个大写字母的所有行,不输出一行的内容全是大写的
  7. 使用layer时控制台出现: Failed to load resource: the server responded with a status of 404 (Not Found)
  8. margin负值应用
  9. std::function和std::bind详解
  10. LUA-点号和冒号