您的位置:首页 > 其它

POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

2016-03-01 16:21 260 查看
Flip Game

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 37427Accepted: 16288
Description

Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the other one is black and each piece is lying either it's black or white side up. Each round you flip 3 to 5 pieces, thus changing the color of their upper side from black to white and vice versa. The pieces to be flipped are chosen every round according to the following rules:

Choose any one of the 16 pieces.

Flip the chosen piece and also all adjacent pieces to the left, to the right, to the top, and to the bottom of the chosen piece (if there are any).

#include <stdio.h>

int field;
int state[]={19,39,78,140,305,626,1252,2248,4880,10016,20032,35968,12544,29184,58368,51200};
int bit[]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};

void read() {
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
field<<=1;
if(getchar()=='b')
field|=1;
}
getchar();
}
}

bool check() {
return field==0x0000||field==0xFFFF;
}

int minn=0xFF;
void work() {
for(int flip=0; flip<=0xFFFF; flip++) {
int temp=field, cnt=0;
for(int i=0; i<16; i++)
if(flip&bit[i]) {// flip&(1<<i)
field^=state[i];
++cnt;
}
if(check()&&minn>cnt) minn=cnt;
field=temp;
}
}

void print() {
if(minn==0xFF) puts("Impossible");
else printf("%d\n", minn);
}

int main() {
read();
work();
print();
return 0;
}


POJ 1753 枚举
三、高斯消元法

  1、基本想法是,令a=棋盘状态矩阵,b=最终各棋子的状态,ax=b解出x=要翻转的棋子,数一下x里面1的数量就是翻转的棋子数了。因为最终状态可以是全黑或全白,因此需要对b取两次值,做两次消元。

  2、但是你会发现,这题会经常出现无穷多解的情况,也就是存在自由变元。因此需要枚举or搜索这些自由变元的值。

  (代码目前没交,待更新)

——原创by BlackStorm,转载请注明出处。

本文地址:/article/7078210.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: