POJ--3279(开关问题2个不同时间写的代码)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 19730 | Accepted: 7118 |
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 思路:
因为求解的是最小按键次数,如果将所有都遍历的话,时间复杂度为 2^(N*M)太大了,因为开关按下,上下左右和它自己都会变化,所以,让第一行出现2^m次不同的情况,用二进制表示
也就是for(int i=0;i< 1<<m; i++)种不同情况, 然后将i转化为二进制。这样就可以代表具体开关的地方,第二行以及以后就可以根据第一行进行遍历了。因为第一行已经确定了所以,第二行就专门去寻找第一行还没有关的灯,比如:map[i][j] = 1,就按下a[i+1][j]处的开关保证i+1行开关按后,第i行的灯全部关闭为0 最后判断这种情况是否正确,然后判断是否最小,记录下来输出。
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int map[][],mm[][];
int a[][],A[][];
int d[][] = {,,,,-,,,,,-}; int get_s(int x, int y){
if(map[x][y]==) return ;
else return ;
}
int main(){
int n,m;
cin>>n>>m;
memset(map,,sizeof(map));
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
cin>>map[i][j];
}
}
memcpy(mm,map,sizeof(map)); int minn = 1e9;
for( int i = ; i < <<m; i++ ) {
int count=,flag = ;
memset(a,,sizeof(a));
memcpy(map,mm,sizeof(mm));
//第一行解决 将 i 转化为 二进制
for(int j=;j<=m;j++){
a[][j] = (i>>(j-))&;
if( a[][j] == ){
count++;
map[][j] = get_s(,j);
map[][j-] = get_s(,j-);
map[][j+] = get_s(,j+);
map[][j] = get_s(,j);
}
} for( int j = ; j < n; j++ ) {
for( int k = ; k <= m; k++ ) {
if( map[j][k] == ) {
a[j+][k] = ;
int dx = j+, dy = k, tx, ty;
count++;
for( int z = ; z < ; z++ ) {
tx = dx + d[z][];
ty = dy + d[z][];
// cout<<tx<<" "<<ty<<endl;
map[tx][ty] = get_s(tx,ty);
}
// cout<<map[j][k]<<" "<<" j = "<<j<<" k = "<<k<<endl;
} }
}
for( int j = ; j <= m; j++ ) {
if(map[n][j]==) flag = ;
} if( flag ) continue;
else {
if( count < minn ) {
minn = count;
memcpy(A,a,sizeof(a));
}
} }
if(minn == 1e9) printf("IMPOSSIBLE\n");
else{
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
printf("%d%c",A[i][j],j==m?'\n':' ');
}
return ;
}
POJ--3279(开关问题2个不同时间写的代码)的更多相关文章
- poj 3279(开关问题)(待完成)
传送门:Problem 3279 #include<iostream> #include<cstdio> #include<cstring> using names ...
- POJ.3279 Fliptile (搜索+二进制枚举+开关问题)
POJ.3279 Fliptile (搜索+二进制枚举+开关问题) 题意分析 题意大概就是给出一个map,由01组成,每次可以选取按其中某一个位置,按此位置之后,此位置及其直接相连(上下左右)的位置( ...
- 【枚举】POJ 3279
直达–>POJ 3279 Fliptile 题意:poj的奶牛又开始作孽了,这回他一跺脚就会让上下左右的砖块翻转(1->0 || 0->1),问你最少踩哪些砖块才能让初始的砖块全部变 ...
- POJ 3279(Fliptile)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定长宽的黑白棋棋盘摆满棋子,每次操作可以反转一个位置和其上下左右共五个位置的棋子的颜色,求要使用最少翻转次数将所有棋子反转为黑 ...
- POJ 3279 Fliptile(翻格子)
POJ 3279 Fliptile(翻格子) Time Limit: 2000MS Memory Limit: 65536K Description - 题目描述 Farmer John kno ...
- 状态压缩+枚举 POJ 3279 Fliptile
题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...
- 挤点时间写博客-php&MySQL实践
hi 晚上要吃火锅的嘛,挤点时间写点东西吧,别被老板发现哦 1.PHP与MySQL 五.文章发布系统之后台 5.2 创建配置文件和初始化文件 为了统一配置以及管理方便,还有就是减少代码的冗余. 分别为 ...
- 基于jQuery发展历程时间轴特效代码
分享一款基于jQuery发展历程时间轴特效代码,带左右箭头,数字时间轴选项卡切换特效下载.效果图如下: 在线预览 源码下载 实现的代码. html代码: <div id="time ...
- html文件在head标签中引入js地址和直接写js代码,所用时间是不同的,因为引入js地址,文件加载的时候需要通过通讯协议去解析地址,读取外部文件
html文件在head标签中引入js地址和直接写js代码,所用时间是不同的,因为引入js地址,文件加载的时候需要通过通讯协议去解析地址,读取外部文件
随机推荐
- Linux进程地址空间与虚拟内存
http://blog.csdn.net/xu3737284/article/details/12710217 32位机器上linux操作系统中的进程的地址空间大小是4G,其中0-3G是用户空间,3G ...
- jquery的fadeTo方法的淡入淡出轮播图插件
由于对基于jquery的简单插件开发有了一定的了解,慢慢的也对基于jquery的插件开发有了兴趣,在上班结束之后就研究各种插件的思路逻辑.最近开发了一款基于jquery的fadeTo方法的轮播图插件, ...
- c++ 浅拷贝和深拷贝 指针和引用的区别 malloc(free)和new(delete)的区别 重载重写重定义
4.malloc(free)和new(delete)的区别 malloc()函数: 1.1 malloc的全称是memory allocation,中文叫动态内存分配. 原型:extern void ...
- Docker实战(五)之端口映射与容器互联
除了网络访问外,Docker还提供了两个很方便的功能来满足服务访问的基本需求:一个是允许映射容器内应用的服务端口到本地宿主主机;另一个是互联机制实现多个容器间通过容器名来快速访问. 1.端口映射实现访 ...
- 鴻雁 Anser cygnoides
鴻雁 Anser cygnoides,其中 Anser 是屬名.雁屬的模式種是 Anser anser 灰雁,在中國也有分佈,但不如鴻雁和中國人關係密切.中國人所說的「大雁」一般指鴻雁,偶爾指灰雁或是 ...
- java中抽象的(abstract)方法是否可同时是静态的(static),是否可同时是本地方法(native),是否可同时被synchronized修饰
1.abstract与static what abstract:用来声明抽象方法,抽象方法没有方法体,不能被直接调用,必须在子类overriding后才能使用. static:用来声明静态方法,静态方 ...
- CSDN博客清理缓存
CSDN博客清理缓存 清着清着 訪问量少了将近2w 积分少了几百 唉 你这是要闹什么呀 说9点结束 如今都几点了
- ios开发UI篇—UITextfield
概述 UITextField在界面中显示可编辑文本区域的对象. 您可以使用文本字段来使用屏幕键盘从用户收集基于文本的输入.键盘可以配置许多不同类型的输入,如纯文本,电子邮件,数字等等.文本字段使用目标 ...
- OO学习体会与阶段总结(多线程程序)
前言 在最近一个月的面向对象编程学习中,我们进入了编写多线程程序的阶段.线程的创建.调度和信息传递,共享对象的处理,线程安全类的编写,各种有关于线程的操作在一定程度上增加了近三次作业的复杂度与难度,带 ...
- linux下pcf8563驱动时钟使用
环境: HelperA64开发板 Linux3.10内核 时间:2019.01.17 目标:PCF8563实时时钟驱动的使用 问题:因为pcf8563的驱动是linux内核自带的,网上也有很多分析的方 ...