原题:
Fliptile
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 16726 Accepted: 6121
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
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的格子,每个格子里面有一个灯,你可以其中任意的一个格子,那么包括该方格在内的上、下、左、右5个灯都会变成相反的状态,现在给你一个初始状态,问你是否能将所有的灯都关闭,如果可以输出最少步数的解,如果有多组解,输出字典序最小的一组,如果没有解,输出IMPOSSIBLE
代码:
//#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cstring>
#include<limits>
using namespace std;
typedef long long ll;
const int maxn=20;
int n , m;
int tile[maxn][maxn],flip[maxn][maxn],opt[maxn][maxn];
int dx[5] = {-1,0,0,0,1};
int dy[5] = {0,-1,0,1,0};
int get(int x,int y)//判断x,y是亮着的,四周如果被按了偶数次,那么状态肯定是亮着
{
int c = tile[x][y];
for(int i=0;i<5;i++)
{
int x2=x+dx[i];
int y2=y+dy[i];
if(0<=x2&&x2<m&&0<=y2&&y2<n)
{
c+=flip[x2][y2];
}
}
return c%2;
}
int calc()
{
for(int i=1;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(get(i-1,j)!=0)
flip[i][j]=1;
}
}
for(int j=0;j<n;j++)
{
if(get(m-1,j)!=0)
return INT_MAX;
}
int res=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
res+=flip[i][j];
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>m>>n)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
cin>>tile[i][j];
}
int res=INT_MAX;
for(int i=0;i<1<<n;i++)
{
memset(flip,0,sizeof(flip));
for(int j=0;j<n;j++)
flip[0][n-j-1]=i>>j&1;
int num = calc();
if(num<res)
{
res=num;
memcpy(opt,flip,sizeof(flip));
}
}
if(res==INT_MAX)
cout<<"IMPOSSIBLE"<<endl;
else
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(j==n-1)
cout<<opt[i][j]<<endl;
else
cout<<opt[i][j]<<" ";
}
}
}
}
return 0;
}
解答:
挑战程序设计竞赛上的例题,属于开关灯系列的题目,考虑第一行的状态,想要关闭第一行的第i列的灯,而且不还能影响i左侧和右侧的灯,那么只能点击第二行第i列的那个格子。也就是,如果第一行的状态确定,那么按照前面说的规律点击的方法就固定下来。所以只需要枚举第一行总共有多少种状态即可。
一般这种变化一个点,会固定的影响其它一些点的问题,如果有一种策略能够实现对其中一种状态进行处理,那么只要枚举出该点有多少种变化的可能,就可以判断出所有的情况。