POJ 1753 Flip Game
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).
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.
Input
The input consists of 4 lines with 4 characters "w" or "b" each that denote game field position.
Output
Write to the output file a single integer number - the minimum number of rounds needed to achieve the goal of the game from the given position. If the goal is initially achieved, then write 0. If it's impossible to achieve the goal, then write the word "Impossible" (without quotes).
Sample Input
bwwb bbwb bwwb bwww
Sample Output
4
Source
题目大意:
给定4x4的棋盘,每个位置都有棋子,棋子有2种状态“黑”或“白”。每次操作可以改变某一位置及其上下左右最多5个棋子的状态,即黑->白,白->黑。给定一个棋盘的状态,问最少需要多少步能达到全黑或者全白的状态(或者永远实现不了)
思路:
对于每个棋子,被改变奇数次等价于被改变1次,被改变偶数次等价于没变,而对于某一个位置,先变和后变并没有区别,因此无须考虑操作顺序。使用位运算压缩状态(共16位)或者直接dfs搜索哪些位置需要被操作即可,注意最后保留最小结果
代码:
#include<stdio.h>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<climits>
#include<ctype.h>
#include<algorithm>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-8
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define lb(x) (x&(-x))
#define clr(a,x) memset(a,x,sizeof(a))
#define mp(a,b) make_pair(a,b)
typedef long long LL;
const int maxn=10005;
char str[5][5];
int map[5][5];
int ans;
void operate(int n)
{
int x=n/4;
int y=n%4;
map[x][y]=!map[x][y];
if(x-1>=0&&x-1<4)
map[x-1][y]=!map[x-1][y];
if(x+1>=0&&x+1<4)
map[x+1][y]=!map[x+1][y];
if(y-1>=0&&y-1<4)
map[x][y-1]=!map[x][y-1];
if(y+1>=0&&y+1<4)
map[x][y+1]=!map[x][y+1];
}
bool judge()
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(map[i][j]!=map[0][0])
return 0;
return 1;
}
void dfs(int cur,int cnt)
{
for(int i=cur+1;i<16;i++)
{
operate(i);
if(judge())
ans=min(ans,cnt);
dfs(i,cnt+1);
operate(i);
}
}
int main(void)
{
while(scanf("%s",str[0])!=EOF)
{
for(int i=1;i<4;i++)
scanf("%s",str[i]);
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(str[i][j]=='w')
map[i][j]=0;
else
map[i][j]=1;
if(judge())
{
printf("0\n");
continue;
}
ans=INT_MAX;
dfs(-1,1);
if(ans==INT_MAX)
printf("Impossible\n");
else
printf("%d\n",ans);
}
return 0;
}
与其类似的是POJ2965
POJ 2965 The Pilots Brothers' refrigerator
这个题是每次操作会改变所在行列所有7块的状态,思路类似,注意此题需要记录路径,不再赘述
代码:
#include<stdio.h>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<climits>
#include<ctype.h>
#include<algorithm>
#include<vector>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-8
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define lb(x) (x&(-x))
#define clr(a,x) memset(a,x,sizeof(a))
#define mp(a,b) make_pair(a,b)
typedef long long LL;
const int maxn=10005;
char str[5][5];
int map[5][5];
int ans;
vector<pair<int,int> >v,va;
void operate(int x,int y)
{
map[x][y]=!map[x][y];
for(int i=0;i<4;i++)
map[i][y]=!map[i][y];
for(int i=0;i<4;i++)
map[x][i]=!map[x][i];
}
bool judge()
{
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(map[i][j]!=1)
return 0;
return 1;
}
void dfs(int cur)
{
for(int i=cur+1;i<16;i++)
{
int x=i/4,y=i%4;
operate(x,y);
v.push_back(mp(x,y));
if(judge()&&v.size()<ans)
{
va=v;
ans=(int)va.size();
}
dfs(i);
operate(x,y);
v.pop_back();
}
}
int main(void)
{
while(scanf("%s",str[0])!=EOF)
{
v.clear();
for(int i=1;i<4;i++)
scanf("%s",str[i]);
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
if(str[i][j]=='-')
map[i][j]=1;
else
map[i][j]=0;
if(judge())
{
printf("0\n");
continue;
}
ans=INT_MAX;
dfs(-1);
printf("%d\n",ans);
for(int i=0;i<va.size();i++)
printf("%d %d\n",va[i].first+1,va[i].second+1);
}
return 0;
}
碎碎念:
有时候会问自己,如果当年再努力一点,拿了银牌能继续跟队友一起进步,而不是变得懒散不思进取,会不会人生轨迹变得不一样了呢?上周也试了单人去参加acm校赛,感慨万分,自己怀念的并不是打acm的日子,而是那个努力的自己啊
不管怎么样,好好生活