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 ...
随机推荐
- Sqoop找不到主类 Error: Could not find or load main class org.apache.sqoop.Sqoop
最近由于要使用Sqoop来到出数据到hdfs,可是发现Sqoop1.4.5跟hadoop2.X不兼容,需要对Sqoop1.4.5进行编译,编译的具体方法见:http://my.codeweblog.c ...
- Vue 2.3、2.4 知识点小结
2.3 style 多重值: <div :style="{ display: ['-webkit-box', '-ms-flexbox', 'flex'] }">< ...
- C# IOThread
在看微软的ASP.NET - 将 ASP.NET 用作高性能文件下载器 示例里面用到了IO 线程,以前打算自己撸的,这里贴出来 已标记一下: ///////////////////////////// ...
- Javascript Module pattern template. Shows a class with a constructor and public/private methods/properties. Also shows compatibility with CommonJS(eg Node.JS) and AMD (eg requireJS) as well as in a br
/** * Created with JetBrains PhpStorm. * User: scotty * Date: 28/08/2013 * Time: 19:39 */ ;(function ...
- grid - 隐式命名网格线名称
1.隐式的指定网格线反向指定了隐式的网格区域名称,命名的网格区域隐式的命名了网格线名称. 指定网格区域会给网格区域边线添加隐式的网格线名称.这些网格线的命名是基于网格区域来命名,只是在网格区域名称的后 ...
- Python操作redis系列之 列表(list) (五)(转)
# -*- coding: utf-8 -*- import redis r =redis.Redis(host=") 1. Lpush 命令将一个或多个值插入到列表头部. 如果 key 不 ...
- [Oracle] “表中有数据,但select count(*)的结果为0”问题的解决办法
一.问题 今天遇到了一个神奇的问题--表中有数据,但select count(*)的结果为0. 这个问题最初的表现形式是"查询报表没有分页". 最开始还以为是java端的问题.后来 ...
- 关于Discuz! X系列UC_Server 本地文件包含漏洞
最近又发现discuz论坛被挂马了,决定好好研究一下discuz的漏洞,技术债始终要还是要还的 一.问题发现 快要睡觉的时候,突然收到一封邮件,发现服务器上的文件被篡改了,立即登录服务器,清空恶意文件 ...
- mac下卸载jdk
mac下安装软件很简单,但是卸载起来相对比较麻烦,下面进入正题: 首先你得知道你的电脑中安装了哪些jdk(mac可以安装多个jdk) 打开mac的终端,输入命令: ls /Library/Java/J ...
- Gradle 打可执行jar包
初次使用Gradle,想和maven一样,把gradle项目打成可执行jar包,具体步骤: 1.下载gradle 版本,并配置环境变量, 下载地址:https://gradle.org/release ...