POJ - 3279(枚举+暴力)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 14297 | Accepted: 5257 |
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 <iostream>
#include <cstdio>
#include <cstring> using namespace std; int m,n; bool right(int i,int j){
if(i>=&&i<m&&j>=&&j<n){
return true;
}else{
return false;
}
} void flip(char grid[][],int i,int j){
int ii=i,jj=j; if(grid[ii][jj]==''){
grid[ii][jj]='';
}else{
grid[ii][jj]='';
} ii=i+,jj=j;
if(right(ii,jj)){
if(grid[ii][jj]==''){
grid[ii][jj]='';
}else{
grid[ii][jj]='';
}
} ii=i-,jj=j;
if(right(ii,jj)){
if(grid[ii][jj]==''){
grid[ii][jj]='';
}else{
grid[ii][jj]='';
}
} ii=i,jj=j+;
if(right(ii,jj)){
if(grid[ii][jj]==''){
grid[ii][jj]='';
}else{
grid[ii][jj]='';
}
} ii=i,jj=j-;
if(right(ii,jj)){
if(grid[ii][jj]==''){
grid[ii][jj]='';
}else{
grid[ii][jj]='';
}
} } int main()
{
char reserve[][];
char grid[][];
char result[][];
char output[][];
int bin[];
scanf("%d %d",&m,&n);
getchar();
for(int i=;i<m;i++){
for(int j=;j<n;j++){
scanf("%c",&reserve[i][j]);
getchar();
}
}
int coun=<<n; int mincou=;
for(int iii=;iii<coun;iii++){ memcpy(grid,reserve,sizeof(reserve));
memset(result,'',sizeof(result)); int cou=;
int t=iii;
int b_i=;
bin[]=; while(t){
bin[b_i]=t%;
t/=;
result[][b_i]=''+bin[b_i];
b_i++;
}
for(int i=;i<n;i++){
if(result[][i]==''){
cou++;
flip(grid,,i);
}
} for(int i=;i<m-;i++){
for(int j=;j<n;j++){
if(grid[i][j]==''){
flip(grid,i+,j);
result[i+][j]='';
cou++;
}
}
}
bool f=true;
for(int i=;i<n;i++){
if(grid[m-][i]==''){
f=false;
break;
}
}
if(f==true){
if(cou<mincou){
mincou=cou;
memcpy(output,result,sizeof(result));
}else if(cou==mincou){
bool flag=true;
for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(output[i][j]>result[i][j]){
break;
}else if(output[i][j]<result[i][j]){
flag=false;
break;
}
}
}
if(flag){
memcpy(output,result,sizeof(result));
}
}
}
} if(mincou==){
printf("IMPOSSIBLE\n");
}else{
for(int i=;i<m;i++){
for(int j=;j<n;j++){
if((i==m-) && (j==n-)){
printf("%c",output[i][j]);
}else{
printf("%c ",output[i][j]);
}
}
if(i!=m-){
printf("\n");
} }
} return ;
}
POJ - 3279(枚举+暴力)的更多相关文章
- POJ 3279 枚举(思维)
Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10931 Accepted: 4029 Descrip ...
- POJ - 3279 枚举 [kuangbin带你飞]专题一
这题很经典啊,以前也遇到过类似的题--计蒜客 硬币翻转. 不过这题不仅要求翻转次数最少,且翻转方案的字典序也要最小. 解法:二进制枚举第一行的翻转方案,然后处理第二行,如果第二行的k列的上一列是黑色, ...
- poj 3279(暴力)
题意:有一个n*m的格子,每个格子都有黑白两面(0表示白色,1表示黑色).我们需要把所有的格子都反转成黑色,每反转一个格子,它上下左右的格子都会跟着反转.请求出用最小步数完成反转时每个格子反转的次数. ...
- POJ - 1222 / POJ - 3279 枚举第一行
说好的高斯消元法呢,暴搜都能0ms 这种翻转就是枚举第一行控制变量下面行就全都确定了 代码参考挑战程序设计例题 #include<iostream> #include<algorit ...
- POJ 3279 枚举?
思路: 1.枚举第一行 递推剩下的 判断最后一行成不成立 2. (误)高斯消元? 如何判断1最少和字典序最小- (所以这种做法好像不可取) //By SiriusRen #include <cs ...
- 【枚举】POJ 3279
直达–>POJ 3279 Fliptile 题意:poj的奶牛又开始作孽了,这回他一跺脚就会让上下左右的砖块翻转(1->0 || 0->1),问你最少踩哪些砖块才能让初始的砖块全部变 ...
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
随机推荐
- 附近有什么?8款可以查周边的App
如今科技发达的时代,手机的功能不仅仅只是能通讯聊天,而是逐渐的走进了人们的生活中.因为有了APP,我们的生活才更丰富,并且有很多是我们生活中不可缺少的软件,而这些软件便是根据手机中的GPS定位系统而来 ...
- C++ 中的不定参数与格式化字符串 # ## vsprintf
日志打印或者格式字符串时,可能会用到不定参数的使用,这里记录一下. 格式化字符串有很多方法: snprintf std::stringstream # ##的使用 ##是一个连接符号,用于把参数连在一 ...
- 使用python实现深度神经网络 4(转)
https://blog.csdn.net/oxuzhenyi/article/details/73026807 使用浅层神经网络识别图片中的英文字母 一.实验介绍 1.1 实验内容 本次实验我们正式 ...
- GoAccess日志分析工具
1.1 GoAccess简介 GoAccess是一个非常良心的开源软件,它的良心之处体现在如下方面: 1)安装简单: 2)操作容易: 3)界面酷炫: GoAccess 官网 https://goacc ...
- 需要看源码的java类
1.数据结构相关的类,如String.ArrayList,LinkedList,HashMap和ConcurrentHashMap等等.2.线程并发相关的类,如Synchronized.Reentra ...
- 转 信号量与PV操作
在计算机操作系统中,PV操作是进程管理中的难点.首先应弄清PV操作的含义:PV操作由P操作原语和V操作原语组成(原语是不可中断的过程),对信号量进行操作,具体定义如下: P(S):①将信号量S的 ...
- InfluxDB源码阅读之snapshotter服务
操作系统 : CentOS7.3.1611_x64 go语言版本:1.8.3 linux/amd64 InfluxDB版本:1.1.0 服务模块介绍 源码路径: github.com/influxda ...
- [转]恕我直言,在座的各位根本不会写 Java!
导语 自 2013 年毕业后,今年已经是我工作的第 4 个年头了,总在做 Java 相关的工作,终于有时间坐下来,写一篇关于 Java 写法的一篇文章,来探讨一下如果你真的是一个 Java 程序员,那 ...
- 微信SDK登录无法调起,微信SDK无法接收回调的几种解决办法
今天有位同事请求帮忙调试微信登录问题,他遇到了以下2个问题,所以,写篇日志备忘,如果有其它朋友遇到此类问题,都可以照此解决! 平时在开发中,有些开发者经常会遇到微信登录SDK登录时,无法调起微信客户端 ...
- 使用h5py操作hdf5文件
HDF(Hierarchical Data Format)指一种为存储和处理大容量科学数据设计的文件格式及相应库文件.HDF 最早由美国国家超级计算应用中心 NCSA 开发,目前在非盈利组织 HDF ...