最近由于复习备考(然而考得还是很炸),很久没打题目了。现在开始刷寒假作业,不得不搞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. 使用angular-cli脚手架快速搭建项目

    第一步 安装全局的angular-cli, npm install -g @angular/cli 或者 cnpm install -g @angular/cli@v1.0.0-rc.2 – 国内淘宝 ...

  2. Python字符串和编码

    在最早的时候只有127个字符被编码到计算机里,也就是大小写英文字母.数字和一些符号,这个编码被成为ASCII编码. 但是要处理中文显然一个字节是不够的,至少需要两个字节,而且还不能和ASCII编码冲突 ...

  3. PHP检测数组中的每个值是否含有特殊字符

    本文出至:新太潮流网络博客 /** * [TestArray 检测数组是一维还是二维] * @E-mial wuliqiang_aa@163.com * @TIME 2017-04-07 * @WEB ...

  4. c#经典算法之冒泡排序(Bubble Sort)

    转载于:https://www.cnblogs.com/shen-hua/p/5422676.html 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面, ...

  5. elasticsearch DSL查询

    总结一个DSL的用法吧,语法网上查去,只记录一点心得,也是研究了半天,太麻烦了 先附上python代码 #!/usr/bin/env python # _*_ coding:utf-8 _*_ fro ...

  6. [CentOS7] [VMWARE] 增加磁盘空间后扩大逻辑分区

    Learn to rescan disk in Linux VM when its backed vdisk in vmware is extended. This method does not r ...

  7. Sqlserver数据库中,跨权限执行语句

    问题来源:最近有同事需要执行批量删除语句.根据他提供的业务需求,推荐他使用“TRUNCATE TABLE”语句.但使用该语句需要 ALTER权限,这与执行用户的角色不符. 解决办法:使用EXECUTE ...

  8. SQLServer导数据到Oracle

    从SQLServer导数据到Oracle大概有以下几种方法: 使用SSMS的导出数据向导,使用Microsoft ODBC for Oracle或Oracle Provider for OLE DB连 ...

  9. 转:双向链表dblinklist

    数据结构C#版笔记--双向链表(DbLinkList)   这是数据结构C#版笔记--线性表(Data Structure)之单链表(LinkList)的继续,对于双向链接,节点上除了Next属性外, ...

  10. python基础学习7----编码与解码

    一.python2 python2中默认以ASCII编码 str='hello world' gbk_to_unicode=str.decode('gbk')#将gbk解码为unicode print ...