POJ 3279 (状态压缩暴力枚举)
2016-03-23 23:10
411 查看
Fliptile
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
Line 1: Two space-separated integers: M and N
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
Lines 1..M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
Sample Input
Sample Output
Source
USACO 2007 Open Silver
题意:给你一个M*N的数字矩阵,数字是0或1,让你反转数次使得整个矩阵所有数字都为0。反转规则是:当你反转一个数字,其相邻的数字也会被反转。问最少需要反转几次,输出每个点的反转次数。
题解:好吧,看到这个题完全懵逼了,这里根据题意可以先枚举第1行的所有反转状态,使用状态压缩的思想枚举第一行的所有可能性,然后就是枚举了,如果数字矩阵里
G[I-1][J]==1,那么就需要反转G[I][J]这个数字块了
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 6357 | Accepted: 2424 |
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
Line 1: Two space-separated integers: M and N
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
Lines 1..M: Each line contains N space-separated integers, each specifying how many times to flip that particular location.
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
USACO 2007 Open Silver
题意:给你一个M*N的数字矩阵,数字是0或1,让你反转数次使得整个矩阵所有数字都为0。反转规则是:当你反转一个数字,其相邻的数字也会被反转。问最少需要反转几次,输出每个点的反转次数。
题解:好吧,看到这个题完全懵逼了,这里根据题意可以先枚举第1行的所有反转状态,使用状态压缩的思想枚举第一行的所有可能性,然后就是枚举了,如果数字矩阵里
G[I-1][J]==1,那么就需要反转G[I][J]这个数字块了
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<set> #include<queue> #include<string> #include<bitset> #include<utility> #include<functional> #include<iomanip> #include<sstream> #include<ctime> using namespace std; #define N int(1e1+6) #define inf int(0x3f3f3f3f) #define mod int(1e9+7) typedef long long LL; int g ,mp ,f ,cnt,n,m; void flip(int x,int y) { cnt++; mp[x][y]=1; f[x][y]^=1; if(x<n-1)f[x+1][y]^=1; if(x>0)f[x-1][y]^=1; if(y<m-1)f[x][y+1]^=1; if(y>0)f[x][y-1]^=1; } bool ok(int x) { cnt=0; memcpy(f,g,sizeof(f)); for(int i=0;i<m;i++) { if(x&(1<<(m-i-1))) { flip(0,i); } } for(int i=1;i<n;i++) { for(int j=0;j<m;j++) { if(f[i-1][j]) flip(i,j); } } for(int i=0;i<m;i++) { if(f[n-1][i])return false; } return true; } int main() { #ifdef CDZSC freopen("i.txt", "r", stdin); //freopen("o.txt","w",stdout); int _time_jc = clock(); #endif while(~scanf("%d%d",&n,&m)) { int ans=inf,way=-1; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { scanf("%d",&g[i][j]); } } for(int i=0;i<(1<<m);i++) { if(ok(i)&&ans>cnt) { ans=cnt; way=i; } } memset(mp,0,sizeof(mp)); if(ans!=inf) { ok(way); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(j)printf(" "); printf("%d",mp[i][j]); } puts(""); } } else { puts("IMPOSSIBLE"); } } return 0; }
相关文章推荐
- openCv 导入 android studio
- 安装了PC蓝牙驱动程序后,使用串口助手,出现蓝屏现象!
- 项目2:太乐了
- 第五章—面向对象(上)
- 线程小游戏制作的心得与体会
- 趣文:C++程序员离职前丧心病狂的报复
- 160323、理解Java虚拟机体系结构
- Qt5.6+MinGW4.9.2+OpenCV3.1+Win10开发环境搭建
- R语言画图大全(实战3,6,11)
- 入职第一天
- 八大排序算法冒泡排序法
- 世界是数字的观后感
- 马太效应
- 数据结构(19)栈典型问题之C++实现表达式求值
- android手势滑动关闭当前activity
- Quartz学习笔记(五) quartz扩展druid连接池
- CSS自定义弹出框
- [angularjs] angularjs系列笔记(五)Service
- 透过__thread学线程局部存储(TLS)
- C 语言--字符串操作函数--笔记(1)