POJ 1222 EXTENDED LIGHTS OUT(翻转+二维开关问题)

时间:2023-12-16 15:46:02

POJ 1222 EXTENDED LIGHTS OUT

    今天真是完美的一天,这是我在poj上的100A,留个纪念,马上就要期中考试了,可能后面几周刷题就没这么快了,不管怎样,为下一个200A奋斗,

    这个题是大白上的牛翻转颜色的题(P153)的弱化版,典型的开关问题;

/*
* Created: 2016年04月05日 22时28分26秒 星期二
* Author: Akrusher
*
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define in(n) scanf("%d",&(n))
#define in2(x1,x2) scanf("%d%d",&(x1),&(x2))
#define in3(x1,x2,x3) scanf("%d%d%d",&(x1),&(x2),&(x3))
#define inll(n) scanf("%I64d",&(n))
#define inll2(x1,x2) scanf("%I64d%I64d",&(x1),&(x2))
#define inlld(n) scanf("%lld",&(n))
#define inlld2(x1,x2) scanf("%lld%lld",&(x1),&(x2))
#define inf(n) scanf("%f",&(n))
#define inf2(x1,x2) scanf("%f%f",&(x1),&(x2))
#define inlf(n) scanf("%lf",&(n))
#define inlf2(x1,x2) scanf("%lf%lf",&(x1),&(x2))
#define inc(str) scanf("%c",&(str))
#define ins(str) scanf("%s",(str))
#define out(x) printf("%d\n",(x))
#define out2(x1,x2) printf("%d %d\n",(x1),(x2))
#define outf(x) printf("%f\n",(x))
#define outlf(x) printf("%lf\n",(x))
#define outlf2(x1,x2) printf("%lf %lf\n",(x1),(x2));
#define outll(x) printf("%I64d\n",(x))
#define outlld(x) printf("%lld\n",(x))
#define outc(str) printf("%c\n",(str))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define mem(X,Y) memset(X,Y,sizeof(X));
typedef vector<int> vec;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={-,,,,},dy[]={,-,,,};
const int INF=0x3f3f3f3f;
const ll mod=1e9+;
ll powmod(ll a,ll b) {ll res=;a%=mod;for(;b;b>>=){if(b&)res=res*a%mod;a=a*a%mod;}return res;}
const bool AC=true; const int M=;
const int N=;
int tile[][];
int opt[][];
int flip[][];
int k;
int get(int x,int y){
int c=tile[x][y];
rep(i,,){
int nx=x+dx[i],ny=y+dy[i];
if(nx>=&&ny>=&&nx<M&&ny<N){
c+=flip[nx][ny];
}
}
return c%;
}
int calc(){
rep(i,,M){
rep(j,,N){
if(get(i-,j)) flip[i][j]=;
}
}
rep(j,,N){
if(get(M-,j)){
return -;
}
}
int res=;
rep(i,,M)
rep(j,,N)
if(flip[i][j]) res++;
return res;
}
void solve(){
int res=-;
rep(i,,<<N){
mem(flip,);
rep(j,,N){
flip[][N-j-]=(i>>j)&;
}
int num=calc();
if(num>=&&(res<||res>num)){ //此处可能为0
res=num;
memcpy(opt,flip,sizeof(flip));
}
}
printf("PUZZLE #%d\n",k);
rep(i,,M){
rep(j,,N){
printf("%d ",opt[i][j]);
}
printf("\n");
}
}
int main()
{
int t;
in(t);
k=;
while(t--){
k++;
rep(i,,M)
rep(j,,N){
in(tile[i][j]);
}
solve();
}
return ;
}

      POJ 1222 EXTENDED LIGHTS OUT(翻转+二维开关问题)