最近由于复习备考(然而考得还是很炸),很久没打题目了。现在开始刷寒假作业,不得不搞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. angularjs -- 页面模板清除

    前几天项目在上线过程中,出现了一些新问题.页面在切换时由于前一个页面的模板清理不及时,会造成页面的重叠.导致这个问题的原因是:页面模板缓存,即上一个页面退出时,浏览器没有及时清空上一个页面的模板,导致 ...

  2. hashCode()与equals()方法的对比

        Java对于eqauls方法和hashCode方法是这样规定的: 1.如果两个对象相同,那么它们的hashCode值一定要相同: 2.如果两个对象的hashCode相同,它们并不一定相同(上面 ...

  3. oracle学习之pl/sql使用==转载

    PLSQL循序渐进全面学习教程(全):https://blog.csdn.net/spark998/article/details/2065269

  4. leetCode题解之Longest Palindrome

    1.题目描述 2.问题分析 直接用hash table 做就行. 3.代码 int longestPalindrome(string s) { ) ; map<char,int> m; f ...

  5. JSONCPP to Visual Studio

    I am having some trouble getting the JSONCPP Library into Visual Studio. I have downloaded the libra ...

  6. Innodb的体系结构

    MySQL的体系结构,两部分组成:MySQL的server层和存储引擎层. 存储引擎层innodb体系结构: innodb的整个体系结构就是由多个内存块组成的缓冲池及多个后台进程组成.我们可以从三方面 ...

  7. iOS 固定定位不兼容、input获取焦点后位置不对。

    第一次写博客~  大家悠着看,有则改之,无则加冕,对不对的给个回复,让我知道你的存在. 在做活动页的时候,经常会碰到一些需要弹出显示的输入框(情节前提:本人安卓机~),前天自己写的时候自己调试没问题后 ...

  8. ArcGIS js api 手动构建FeatureLayer

    坐标系 var spatialReference = new SpatialReference(4326);1要素坐标点 var pointArr = [ new Point(116.94431351 ...

  9. 2017-2018-2 20165318 实验三《Java面向对象程序设计》实验报告

    2017-2018-2 20165318 实验三<Java面向对象程序设计>实验报告 一.实验报告封面 课程:Java程序设计        班级:1653班        姓名:孙晓暄  ...

  10. 在openresty或nginx编译nginx-upsync-module&nginx_upstream_check_module

    针对我在编译在两个模块的过程中遇到的一系列问题,特此记录编译流程的一些细节 1.下载 install git git clone https://github.com/weibocom/nginx-u ...