POJ3279 搜索/思维
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 10457 | Accepted: 3866 |
Description
Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M × N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is colored black on one side and white on the other side.
As one would guess, when a single white tile is flipped, it changes to black; when a single black tile is flipped, it changes to white. The cows are rewarded when they flip the tiles so that each tile has the white side face up. However, the cows have rather large hooves and when they try to flip a certain tile, they also flip all the adjacent tiles (tiles that share a full edge with the flipped tile). Since the flips are tiring, the cows want to minimize the number of flips they have to make.
Help the cows determine the minimum number of flips required, and the locations to flip to achieve that minimum. If there are multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicographical ordering in the output when considered as a string. If the task is impossible, print one line with the word "IMPOSSIBLE".
Input
Lines 2..M+1: Line i+1 describes the colors (left to right) of row i of the grid with N space-separated integers which are 1 for black and 0 for white
Output
Sample Input
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
Sample Output
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0
Source
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int N,M;
int e1[20][20];
int e2[20][20];
int tim[20][20];
int an[20][20];
bool e3[20];
int ans=inf;
int fx[4][2]={1,0,-1,0,0,1,0,-1};
void turn(int x,int y)
{
e2[x][y]=(e2[x][y]==0?1:0);
for(int i=0;i<4;++i){
int dx=x+fx[i][0];
int dy=y+fx[i][1];
if(dx<1||dy<1||dx>N||dy>M) continue;
e2[dx][dy]=(e2[dx][dy]==0?1:0);
}
}
void make()
{
int i,j;
for(i=2;i<=N;++i){
for(j=1;j<=M;++j){
int up=e2[i-1][j];
if(up==0) continue;
else{
turn(i,j);
tim[i][j]++;
}
}
}int s=0;
for(i=1;i<=M;++i) if(e2[N][i]==0) s++;
if(s==M){
s=0;
for(i=1;i<=N;++i){
for(j=1;j<=M;++j) {
s+=tim[i][j];
// cout<<tim[i][j];
// if(j!=M) cout<<" ";
}
//cout<<endl;
}
if(s<ans){
ans=s;
memcpy(an,tim,sizeof(int)*400);
}
}
}
void solve()
{
int i,j,k;
memcpy(e2,e1,sizeof(int)*400);
memset(tim,0,sizeof(tim));
for(i=1;i<=M;++i){
if(e3[i]){
turn(1,i);
tim[1][i]++;
}
}
make();
}
void dfs(int x)
{
if(x==M+1){
solve();
return;
}
for(int i=0;i<2;++i){
e3[x]=i;
dfs(x+1);
}
}
int main()
{
int i,j,k;
while(cin>>N>>M){ans=inf;
for(i=1;i<=N;++i)
for(j=1;j<=M;++j) {
cin>>e1[i][j];
}
dfs(1);
if(ans==inf) puts("IMPOSSIBLE");
else{
for(i=1;i<=N;++i){
for(j=1;j<=M;++j){
if(j!=1) cout<<" ";
cout<<an[i][j];
}
cout<<endl;
}
}
}
return 0;
}
POJ3279 搜索/思维的更多相关文章
- “玲珑杯”ACM比赛 Round #18--最后你还是AK了(搜索+思维)
题目链接 DESCRIPTION INPUT OUTPUT SAMPLE INPUT 1 4 2 1 2 5 2 3 5 3 4 5 5 5 SAMPLE OUTPUT 35 HINT 对于样例, ...
- 【CCF】无线网络 搜索+思维
#include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...
- android搜索框列表布局,流程及主要步骤思维导图
android搜索框列表布局,流程及主要步骤思维导图 android搜索框列表布局,流程及主要步骤思维导图 activity_coin_search.xml----------<com.scwa ...
- 33、搜索旋转排序数组 | 算法(leetode,附思维导图 + 全部解法)300题
零 标题:算法(leetode,附思维导图 + 全部解法)300题之(33)搜索旋转排序数组 一 题目描述! 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 & ...
- 35、搜索插入位置 | 算法(leetode,附思维导图 + 全部解法)300题
零 标题:算法(leetode,附思维导图 + 全部解法)300题之(35)搜索插入位置 一 题目描述 二 解法总览(思维导图) 三 全部解法 1 方案1 1)代码: // 方案1 "无视要 ...
- 2017广东工业大学程序设计竞赛决赛 题解&源码(A,数学解方程,B,贪心博弈,C,递归,D,水,E,贪心,面试题,F,贪心,枚举,LCA,G,dp,记忆化搜索,H,思维题)
心得: 这比赛真的是不要不要的,pending了一下午,也不知道对错,直接做过去就是了,也没有管太多! Problem A: 两只老虎 Description 来,我们先来放松下,听听儿歌,一起“唱” ...
- POJ3279 Fliptile 枚举+简单搜索
题意:一个矩阵,每个点1或0,然后每次翻一个点,它周围上下左右(包括自己)1->0,0->1,问最少翻几次可以矩阵全是0,忽略题目说的字典序 分析:枚举第一行所有的情况,然后下面几行也随之 ...
- 相邻行列相互影响的状态类问题(类似状压dp的搜索)(POJ3279)
POJ3279http://poj.org/problem?id=3279 题意:黑白的板,每次选择一个十字形翻转(十字板内黑白互换,若是边界则不管),求最小将原图变为全白的策略. 这是一道对于每个格 ...
- LOJ2323. 「清华集训 2017」小 Y 和地铁 【搜索】【思维】【好】
LINK 思路 首先如果直接算每一个段有三个决策 左/右 上/下 跨不跨过端点 这样的复杂度是\((2^3)^{22}\),显然是无法接受的 然后考虑怎么优化这个东西 首先左右这个决策是没有意义的 因 ...
随机推荐
- 您好,前端使用https,后端使用https是会有冲突的情况,所以默认后端都是http 负载均衡即可管理证书,不需要在后端ECS上绑定证书。
您前端使用https,那么前端就是加密的,后端使用https就是会访问出现问题的,目前阿里云负载均衡默认的配置前端使用https,后端默认就是http,也是无法更改的. 前端使用https,目前只有一 ...
- Python-Select/Poll/Epoll使用
select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当select()返回后,该数组中就绪的文件描述符便会被内核修改标志位,使 ...
- android进入页面会定位到ListView问题解决方法
在我们的页面中如果存在有ListView,当我们进入这个activity时,页面会定位到ListView的位置去,而不是activity的头部,这是由于ListView会去默认获取焦点所造成的. 解决 ...
- Redis缓冲区设置
对于Redis服务器的输出(也就是命令的返回值)来说,其大小通常是不可控制的.有可能一个简单的命令,能够产生体积庞大的返回数据.另外也有可能因为执行了太多命令,导致产生返回数据的速率超过了往客户端发送 ...
- android qq开合表
qq悬浮列表功能暂未实现 main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/andr ...
- jsp内置对象与servlet的关系
Servlet与JSP九大内置对象的关系 JSP对象 怎样获得 out->response.getWriter request ->Service方法中的req参数 response ...
- [py]django重置密码
django的admin用户被我多动症一样的测试,给密码弄丢了,需要重置. 从数据库重置的可能性为0,因为django对于密码有保护策略.考虑从运行程序的地方进行重置: 1.在程序的文件夹下,执行这样 ...
- mydumper安装
安装依赖包: yum install glib2-devel mysql-devel zlib-devel pcre-devel openssl-devel cmake 下载二进制包: wget ht ...
- zip和tgz以及exe的区别
在下载东西的时候总是碰见后缀是.tar.gz和.zip的问题,搞不清楚是怎么回事,不晓得下载哪个文件才是对自己有用的. 后来才知道,其实这两个压缩文件里面包含的内容是一样的,只是压缩格式不一样, ta ...
- docker——网络配置
一.网络启动与配置参数 Docker启动时会在主机上自动创建一个docker0虚拟网桥,实际上是一个Linux网桥,可以理解为一个软件交换机,它会在挂载其上的接口之间进行数据转发.同时,Docker随 ...