最近由于复习备考(然而考得还是很炸),很久没打题目了。现在开始刷寒假作业,不得不搞POJ

  话说没有中文真的好烦啊!

  先看1753

  题目大意是说在一个4*4的格子中有黑白两色的棋子,你可以翻动其中的棋子但同时它四周(上下左右)的棋子也会被翻动,问你最少要翻几次才能翻成全黑或全白。

  首先要想到,一个棋子翻转偶数次就相当于没翻,翻了奇数次就相当于翻了一次(很好想到),最重要的是它们翻转的顺序对答案并无影响!

  所以一个棋子要么翻要么不翻,一共有2^(4*4)共65536种可能,直接爆搜即可。

  注意一下翻棋子时不要越界

  CODE

#include<cstdio>
#include<iostream>
using namespace std;
const int fx[]={,,,-,},fy[]={,,,,-};
char a[][];
int i,j,ans=;
inline bool check()
{
int w=,b=; char comp=a[][];
for (int i=;i<=;++i)
for (int j=;j<=;++j)
if (a[i][j]!=comp) return ;
return ;
}
inline void flip(int x,int y)
{
for (int i=;i<;++i)
{
int xx=x+fx[i],yy=y+fy[i];
if (xx<=||yy<=||xx>||yy>) continue;
if (a[xx][yy]=='w') a[xx][yy]='b'; else a[xx][yy]='w';
}
}
inline void DFS(int x,int y,int s)
{
if (check()) { ans=s<ans?s:ans; return; }
if (x>) return;
int xx=x,yy=y;
if (++yy>) ++xx,yy=;
flip(x,y);
DFS(xx,yy,s+);
flip(x,y);
DFS(xx,yy,s);
}
int main()
{
for (i=;i<=;++i)
for (j=;j<=;++j)
cin>>a[i][j];
DFS(,,);
if (ans!=) printf("%d",ans); else puts("Impossible");
return ;
}

  2965其实是一道基本相似的题目,也是在4*4的方格中把所有的开关都翻成‘-‘,每次翻一个就会使它所在的那一行和那一列都翻转。

  同样的做法,不过是开一个vector纪录一下答案而已。

  CODE

#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
vector <int> ans_x,ans_y;
char a[][];
int i,j,ans=;
bool f[][];
inline void copy()
{
for (int i=;i<=;++i)
for (int j=;j<=;++j)
if (f[i][j]) ans_x.push_back(i),ans_y.push_back(j);
}
inline bool check()
{
int w=,b=;
for (int i=;i<=;++i)
for (int j=;j<=;++j)
if (a[i][j]!='-') return ;
return ;
}
inline void flip(int x,int y)
{
for (int i=;i<=;++i)
if (a[i][y]=='-') a[i][y]='+'; else a[i][y]='-';
for (int j=;j<=;++j)
if (a[x][j]=='-') a[x][j]='+'; else a[x][j]='-';
if (a[x][y]=='-') a[x][y]='+'; else a[x][y]='-';
}
inline void DFS(int x,int y,int s)
{
if (check())
{
if (s<ans)
{
ans=s;
ans_x.clear(); ans_y.clear();
copy();
}
return;
}
if (x>) return;
int xx=x,yy=y;
if (++yy>) ++xx,yy=;
flip(x,y);
f[x][y]=;
DFS(xx,yy,s+);
flip(x,y);
f[x][y]=;
DFS(xx,yy,s);
}
int main()
{
for (i=;i<=;++i)
for (j=;j<=;++j)
cin>>a[i][j];
DFS(,,);
printf("%d\n",ans);
for (i=;i<ans;++i)
printf("%d %d\n",ans_x[i],ans_y[i]);
return ;
}

POJ 2965&&1753的更多相关文章

  1. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  2. poj 2965 The Pilots Brothers&#39; refrigerator(dfs 枚举 +打印路径)

    链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手.'+'代表关,'-'代表开.当16个把手都为开(即'-')时.门才干打开,问至少要几步门才干打开 改变状态规则:选定16个把手 ...

  3. 枚举 POJ 2965 The Pilots Brothers' refrigerator

    题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...

  4. poj 2965

    http://poj.org/problem?id=2965 本题要结合poj 1753 来看最好...又有了一点搜索的经验..加油... #include <iostream> #inc ...

  5. POJ 2965 The Pilots Brothers' refrigerator【枚举+dfs】

    题目:http://poj.org/problem?id=2965 来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=26732#pro ...

  6. poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)

    //题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...

  7. POJ 2965 The Pilots Brothers' refrigerator (枚举+BFS+位压缩运算)

    http://poj.org/problem?id=2965 题意: 一个4*4的矩形,有'+'和'-'两种符号,每次可以转换一个坐标的符号,同时该列和该行上的其他符号也要随之改变.最少需要几次才能全 ...

  8. poj 2965 The Pilots Brothers' refrigerator (dfs)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17450 ...

  9. POJ 2965 The Pilots Brothers' refrigerator【BFS+状压 Or 脑洞】

    题目链接: http://poj.org/problem?id=1753 题意: 给定冰箱门的开关情况,改变一个门则其所在行列的门都会发生改变,求出改变门的最少操作使得最终所有门都是打开状态. 代码: ...

随机推荐

  1. 机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据

    机器学习实战(Machine Learning in Action)学习笔记————09.利用PCA简化数据 关键字:PCA.主成分分析.降维作者:米仓山下时间:2018-11-15机器学习实战(Ma ...

  2. jQuery的介绍和选择器详解

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. jQuery 的介绍 引入 jQuery 的原因 在用 js 写代码时, ...

  3. JavaScript语法详解:运算符和表达式

    本文首发于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. 我们在上一篇文章里讲到了JS中变量的概念,本篇文章讲一下运算符和表达式. 比 ...

  4. LeetCode题解之 Sum of Left Leaves

    1.题目描述 2.问题分析 对于每个节点,如果其左子节点是叶子,则加上它的值,如果不是,递归,再对右子节点递归即可. 3.代码 int sumOfLeftLeaves(TreeNode* root) ...

  5. sqlserver 一键备份,异机还原脚本

    REM +---------------------------------------------------------------------------------+ REM |desc AU ...

  6. SQL Server 合并复制如何把备份的发布端或订阅端BAK文件还原为数据库

    SQL Server的合并复制,是可以备份发布端和订阅端数据库为BAK文件的,但是问题是合并复制在数据库中自动创建的系统表.触发器.表中的RowGuid列等也会被一起备份. 这里我们举个例子,下面图中 ...

  7. SQL Server中事务transaction如果没写在try catch中,就算中间语句报错还是会提交

    假如我们数据库中有两张表Person和Book Person表: CREATE TABLE [dbo].[Person]( ,) NOT NULL, ) NULL, ) NULL, [CreateTi ...

  8. Android external扩展工程

    Android的扩展工程包含在external文件夹中,这是一些经过修改后适应Android系统的开源工程,这些工程有些在主机上运行,有些在目标机上运行: 工程名称  工程描述  aes  高级加密标 ...

  9. 50家硅谷IT公司技术博客

    分享一下 50 家硅谷优秀 IT 公司技术博客,从中可以了解企业文化,技术特色和设计语言,如果直接列出来很单调,加上点评,算吐槽版吧. 知名大厂   1. Facebook https://www.f ...

  10. struts2框架

    详细教程 参考struts教程https://www.w3cschool.cn/struts_2/struts_configuration.html Struts2 基于MVC设计模式的web应用程序 ...