Flip Game (高斯消元 || dfs)
- 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
Output
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
#include<stdio.h>
#include<string.h>
int c[][]= {,,,,-,,,-,,},min1,t;
char Map[][]; void turn(int x,int y)//改变状态;
{
for(int i=; i<; i++)
{
int dx=x+c[i][];
int dy=y+c[i][];
if(dx<||dy<||dx>=||dy>=)continue;
if(Map[dx][dy]=='b')
Map[dx][dy]='w';
else Map[dx][dy]='b';
}
} int check()//检查是否达到目标状态;
{
int i,j;
for(i=; i<; i++)
for(j=; j<; j++)
if(Map[i][j]!=Map[][])
return ;
return ;
} void dfs(int x,int y,int s)
{
if(check())//检查是否达到目标状态;
{
if(s<min1)//更新结果;
min1=s;
return ;
}
if(x==)return ;
turn(x,y);//改变状态/回溯
//翻转;
if(y==)dfs(x+,,s+);//换行;
else dfs(x,y+,s+);
//不翻转;
turn(x,y);//改变状态/回溯
if(y==)dfs(x+,,s);
else dfs(x,y+,s);
} int main()
{
int i;
min1=;
for(i=; i<; i++)
scanf("%s",Map[i]);
dfs(,,);
if(min1<=)
printf("%d\n",min1);
else printf("Impossible\n");
}
// 高斯消元
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define INF 0x3f3f3f3f
int Map[][] , a[] , min1 , x[] , freex[] ;
char str[][] ;
void getMap(int k)
{
int i , j ;
memset(Map,,sizeof(Map)) ;
for(i = ; i < ; i++)
{
for(j = ; j < ; j++)
{
a[i*+j] = (str[i][j] - '') ^ k ;
Map[i*+j][i*+j] = ;
if( i > )
Map[i*+j][i*+j-] = ;
if( i < )
Map[i*+j][i*+j+] = ;
if( j > )
Map[i*+j][i*+j-] = ;
if( j < )
Map[i*+j][i*+j+] = ;
}
}
return ;
}
void swap1(int p,int q)
{
int j , temp ;
temp = a[p] ;
a[p] = a[q] ;
a[q] = temp ;
for(j = ; j < ; j++)
{
temp = Map[p][j] ;
Map[p][j] = Map[q][j] ;
Map[q][j] = temp ;
}
return ;
}
int solve()
{
int i , j , k , t = , num1 = ;
for(i = ; i < && t < ; t++ , i++)
{
for(j = i ; j < ; j++)
if( Map[j][t] )
break ;
if( j == )
{
freex[num1++] = t ;
i-- ;
continue ;
}
if( i != j )
swap1(i,j) ;
for(j = i+ ; j < ; j++)
{
if( Map[j][t] )
{
a[j] = a[j]^a[i] ;
for(k = t ; k < ; k++)
Map[j][k] = Map[j][k] ^ Map[i][k] ;
}
}
}
for( ; i < ; i++)
if( a[i] )
return - ;
if( num1 ) return num1 ;
for(i = ; i >= ; i--)
{
x[i] = a[i] ;
for(j = i+ ; j < ; j++)
x[i] = x[i]^(Map[i][j]*x[j]) ;
}
return num1 ;
}
int f(int s)
{
int i , j , k , key , ans , min1 = INF ;
getMap(s) ;
key = solve() ;
ans = ;
if( key == )
{
ans = ;
for(i = ; i < ; i++)
ans += x[i] ;
min1 = min(min1,ans) ;
}
else if( key > )
{
int temp = <<key ;
for(int t = ; t < temp ; t++)
{
memset(x,,sizeof(x)) ;
ans = ;
for(j = ; j < key ; j++)
if( t & (<<j) )
{
x[ freex[j] ] = ;
ans++ ;
}
for(i = ; i >= ; i--)
{
for(k = ; k < ; k++)
if( Map[i][k] )
break ;
x[k] = a[i] ;
for(j = k+ ; j < ; j++)
x[k] ^= (Map[i][j]*x[j]) ;
ans += x[k] ;
}
min1 = min(min1,ans) ;
}
}
return min1 ;
}
int main()
{
int i , j , min1 = INF , k , ans , temp ;
for(i = ; i < ; i++)
{
scanf("%s", str[i]) ;
for(j = ; j < ; j++)
{
if( str[i][j] == 'b' )
str[i][j] = '' ;
else
str[i][j] = '' ;
}
}
ans = f() ;
min1 = min(min1,ans) ;
ans = f() ;
min1 = min(min1,ans) ;
if( min1 == INF )
printf("Impossible\n") ;
else
printf("%d\n", min1) ;
return ;
}
Flip Game (高斯消元 || dfs)的更多相关文章
- BZOJ1770:[USACO]lights 燈(高斯消元,DFS)
Description 貝希和她的閨密們在她們的牛棚中玩遊戲.但是天不從人願,突然,牛棚的電源跳閘了,所有的燈都被關閉了.貝希是一個很膽小的女生,在伸手不見拇指的無盡的黑暗中,她感到驚恐,痛苦與絕望. ...
- [luoguP2962] [USACO09NOV]灯Lights(高斯消元 + dfs)
传送门 先进行高斯消元 因为要求最少的开关次数,那么: 对于关键元,我们可以通过带入消元求出, 对于自由元,我们暴力枚举,进行dfs,因为只有开关两种状态,0或1 #include <cmath ...
- bzoj 1770: [Usaco2009 Nov]lights 燈【高斯消元+dfs】
参考:https://blog.csdn.net/qq_34564984/article/details/53843777 可能背了假的板子-- 对于每个灯建立方程:与它相邻的灯的开关次数的异或和为1 ...
- Codeforces 1163E 高斯消元 + dfs
题意:给你一个集合,让你构造一个长度尽量长的排列,使得排列中任意相邻两个位置的数XOR后是集合中的数. 思路:我们考虑枚举i, 然后判断集合中所有小于1 << i的数是否可以构成一组异或空 ...
- POJ1288 Sly Number(高斯消元 dfs枚举)
由于解集只为{0, 1, 2}故消元后需dfs枚举求解 #include<cstdio> #include<iostream> #include<cstdlib> ...
- NEFU 504 new Flip Game (高斯消元)
题目链接 题解:和 poj1753Filp game 差不多,区别在于t组数据并且翻转的时候多了一个左上角. #include <iostream> #include <cstdio ...
- POJ 1753 Flip game ( 高斯消元枚举自由变量)
题目链接 题意:给定一个4*4的矩阵,有两种颜色,每次反转一个颜色会反转他自身以及上下左右的颜色,问把他们全变成一种颜色的最少步数. 题解:4*4的矩阵打表可知一共有四个自由变元,枚举变元求最小解即可 ...
- poj 1753 Flip Game 高斯消元
题目链接 4*4的格子, 初始为0或1, 每次翻转一个会使它四周的也翻转, 求翻转成全0或全1最少的步数. #include <iostream> #include <vector& ...
- BZOJ 2115 Wc2011 Xor DFS+高斯消元
标题效果:鉴于无向图.右侧的每个边缘,求一个1至n路径,右上路径值XOR和最大 首先,一个XOR并能为一个路径1至n简单的路径和一些简单的XOR和环 我们开始DFS获得随机的1至n简单的路径和绘图环所 ...
随机推荐
- WordPress 安装插件导致 HTTP 500 内部服务器错误的问题
春节这几天忙着过节,一直没有看网站,今天登陆上来看到插件有更新,点开更新后,悲剧发生了.页面就无法加载,出现错误无法加载了,着实让我慌了慌(想到重来就郁闷) Chrome:该网页无法正常工作www ...
- Kubernetes集群的监控报警策略最佳实践
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/M2l0ZgSsVc7r69eFdTj/article/details/79652064 本文为Kub ...
- 4e00~9fa5
<script> function init (){ var begin = 0x4e00; var end = 0x9fa5; var str = ''; for(var a=begin ...
- [原]Docker-issue(2) http: server gave HTTP response to HTTPS client
系统环境 查看 文章末尾 附录 问题点:新建local registry后,push新的image到local registry 未能成功,并报错误: The push refers to repo ...
- 浅析 <路印协议--Loopring> 及整体分析 Relay 源码
作者:林冠宏 / 指尖下的幽灵 前序: 路印协议功能非常之多及强大,本文只做入门级别的分析. 理论部分请细看其白皮书,https://github.com/Loopring/whitepaper 实际 ...
- 禅道docker化(Centos7.2)
操作步骤 确认服务器禅道版本及容器禅道版本 服务器禅道版本:9.6.2 容器禅道版本:9.6.3 版本sql比对 下载官方9.6.3源码包url:http://dl.cnezsoft.com/zent ...
- zhenya moves from parents
Zhenya moved from his parents' home to study in other city. He didn't take any cash with him, he onl ...
- git 处理
[添加]cd 路径(进入文件夹) git clone url git status (查看状态) cd firstprojected 进入这个文件夹 查看 git status 有 index.php ...
- java获取当前网站的IP地址
package ip; import java.net.InetAddress; import java.net.UnknownHostException; /** * * @author * */ ...
- WdatePicker日历添加事件,在任意月改变时处理日期事件
原由 在做系统时根据要求有时候需要屏蔽掉某些特殊的日期,像周日或者法定假日,以及一些调班的日期:使用WdatePicker可以屏蔽掉周日和大多数法定假日,但像清明或者调班的日期则不好处理. 想法 1: ...