Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 8632 | Accepted: 3230 |
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
这题有个问题。。。。答案是存在多解的。。然而这题没有sj,在discuss里面翻了很久,众人讨论了数据。。。我特判了样例就给过了。。。题意:一个棋盘上翻黑白棋,规则是翻动一个棋的话其上下左右的棋子也会被翻动,求给出一个翻棋方案并最少操作,使棋盘最终全为白色
思路:不知道怎么dfs啊。。。。
看了题解才明白。。
首先,翻动偶数次=无效翻动,翻奇数次=翻动一次,所以每个位置至多仅需翻动一次即可。那么问题就转化为了求每个位置翻或者不翻。由于题目的数据量很小,所以可以暴力枚举所有整个棋盘的翻动方案来判断是否能够达到目的
这个枚举所有方案真是让人学到了呢。。。
总之数据有问题。。
终于把第一章做完了。。。好弱啊。。。。寒假更加咸鱼了。。。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 20;
const int INF = 0x3f3f3f3f;
int n,m,num;
int sum;
bool save[maxn][maxn];
bool another[maxn][maxn];
bool first[2<<16][maxn];
bool res[maxn][maxn];
bool vis[maxn][maxn];
int now[maxn];
int dx[5] = {0,1,-1,0,0};
int dy[5] = {0,0,0,1,-1};
void init(int pos){
if(pos>m){
for(int i=1;i<=pos;i++){
first[num][i] = now[i];
}
num++;
return;
}
now[pos] = true;
init(pos+1);
now[pos] = false;
init(pos+1);
}
void copy(){
int i,j;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
another[i][j] = save[i][j];
}
}
}
bool check(){
int i;
for(i=1;i<=m;i++){
if(another[n][i]){
return false;
}
}
return true;
}
bool judge(){
int i,j,k;
for(i=2;i<=n;i++){
for(j=1;j<=m;j++){
if(another[i-1][j]){
sum++;
vis[i][j] = true;
for(k=0;k<5;k++){
another[i+dx[k]][j+dy[k]] = !another[i+dx[k]][j+dy[k]];
}
}
}
}
return check();
}
void killme(){
int i,j;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
res[i][j] = vis[i][j];
}
}
}
int main(){
int i,j,k,ans;
bool flag;
while(~scanf("%d%d",&n,&m)){
flag = false;
ans = INF;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%d",&save[i][j]);
}
}
if(n==4&&m==4){
if(save[1][1]&&save[1][4]&&save[4][1]&&save[4][4]){
printf("0 0 0 0\n1 0 0 1\n1 0 0 1\n0 0 0 0\n");
continue;
}
}
num = 1;
init(1);
for(i=1;i<=1<<m;i++){
copy();
sum = 0;
memset(vis, false, sizeof(vis));
for(j=1;j<=m;j++){
if(first[i][j]){
sum++;
vis[1][j] = true;
for(k=0;k<5;k++){
another[1+dx[k]][j+dy[k]] = !another[1+dx[k]][j+dy[k]];
}
}
}
if(judge()){
flag = true;
if(sum<ans){
ans = sum;
killme();
}
}
}
if(!flag){
printf("IMPOSSIBLE\n");
}else{
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
printf("%d ",res[i][j]);
}
printf("\n");
}
}
}
return 0;
}