POJ 1753 Flip Game (当蒟蒻忘记scanf要读回车时,他自闭了
题面
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).
Consider the following position as an example:
bwbw
wwww
bbwb
bwwb
Here “b” denotes pieces lying their black side up and “w” denotes pieces lying their white side up. If we choose to flip the 1st piece from the 3rd row (this choice is shown at the picture), then the field will become:
bwbw
bwww
wwwb
wwwb
The goal of the game is to flip either all pieces white side up or all pieces black side up. You are to write a program that will search for the minimum number of rounds needed to achieve this goal.
瞎搞
搜索模板题,以前做过,但是读完题我还是懵逼
滑稽
2^16 每个都搜索枚举一遍就行了重要的事情说三遍
scanf要读回车,getchar去吃一下
scanf要读回车,getchar去吃一下
scanf要读回车,getchar去吃一下
代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<map>
#include<iomanip>
#define INF 0x7ffffff
using namespace std;
int dx[5]={0,1,0,-1,0};
int dy[5]={0,0,1,0,-1};
int mp[10][10],ans=INF;
int Fan(int x,int y)
{
int num=0;
for(int i=0;i<=4;i++)
{
int px=x+dx[i],py=y+dy[i];
if(px>4||px<1||py>4||py<1)continue;
if(mp[px][py]==1)num--;
else num++;
mp[px][py]=!mp[px][py];
}
return num;
}
void DFS(int x,int y,int d,int sum)
{
if(sum==0||sum==16)
{
ans=min(ans,d);
return;
}
if(x>4||y>4)return;
int mp1[5][5];
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
mp1[i][j]=mp[i][j];
int pp=Fan(x,y);
sum+=pp;
if(y<4)DFS(x,y+1,d+1,sum);
else DFS(x+1,1,d+1,sum);
sum-=pp;
for(int i=1;i<=4;i++)
for(int j=1;j<=4;j++)
mp[i][j]=mp1[i][j];
if(y<4)DFS(x,y+1,d,sum);
else DFS(x+1,1,d,sum);
}
int main()
{
char x;
int tmp=0;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
scanf("%c",&x);
if(x=='b')mp[i][j]=1;
else mp[i][j]=0;
tmp+=mp[i][j];
}
x=getchar();
}
DFS(1,1,0,tmp);
if(ans==INF)printf("Impossible");
else printf("%d\n",ans);
return 0;
}
上一篇: [模板题]第K个数