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地址,文件加载的时候需要通过通讯协议去解析地址,读取外部文件
随机推荐
- Git Hooks、GitLab CI持续集成以及使用Jenkins实现自动化任务
Git Hooks.GitLab CI持续集成以及使用Jenkins实现自动化任务 前言 在一个共享项目(或者说多人协同开发的项目)的开发过程中,为有效确保团队成员编码风格的统一,确保部署方式的统一, ...
- CentOS 7下启动、关闭、重启、查看MySQL服务
1.启动命令 [root@xufeng Desktop]# service mysqld startRedirecting to /bin/systemctl start mysqld.service ...
- AOP-方法拦截器-笔记
方法拦截器的继承层次图: 这些拦截器具体长什么样?? 一.MethodBeforeAdviceInterceptor 这个拦截器只有一个属性就是前置通知.需要注意的是前置通知和返回通知的拦截器才会持有 ...
- P3558 [POI2013]BAJ-Bytecomputer
题目描述 A sequence of integers is given. The bytecomputer is a device that allows the following operati ...
- numpy的array数据类型(创建)
import numpy as np # 创建 # 创建一维数组 a = np.array([1, 2, 3]) print(a) ''' [1 2 3] ''' # 创建多维数组 b = np.ar ...
- nodejs 跨域设置cookie
在做vue全家桶+express 做项目. 想做一个免登陆功能,选择session方案. 服务器设置session 后, 返回的响应头有Set-Cookie,但是浏览器的Application的coo ...
- 关于C#的静态类和静态构造函数
静态构造函数是C#的一个新特性,其实好像很少用到.不过当我们想初始化一些静态变量的时候就需要用到它了.这个构造函数是属于类的,而不是属于哪里实例的,就是说这个构造函数只会被执行一次.也就是在创建第一个 ...
- sqoop数据导入命令 (sql---hdfs)
mysql------->hdfs sqoop导入数据工作流程: sqoop提交任务到hadoop------>hadoop启动mapreduce------->mapreduce通 ...
- ddt 接口框架数据处理调用excel 处理
之前在csdn写过这里就不赘述了: https://blog.csdn.net/chen498858336/article/details/84351146
- Spring源码分析(九)解析默认标签中的自定义标签元素
摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 到这里我们已经完成了分析默认标签的解析与提取过程,或许涉及的内容太多,我 ...