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简单的路径和绘图环所 ...
随机推荐
- 【转】IT大牛博客
原文:http://blog.csdn.net/qq1175421841/article/details/49384841 首届中国最受欢迎50大技术博客获奖名单如下: 第一名:李会军 http:/ ...
- Android 8 设置蓝牙名称 流程
记录android 8设置蓝牙名称的流程. packages/apps/Settings/src/com/android/settings/bluetooth/BluetoothDeviceRenam ...
- Linux 公网IP和内网IP,Dubbo提供者注册到了内网IP上怎么处理!
1.将Linux的localhost 改为公网IP: 改完以后重启网卡,并刷新Hosts: 2.将提供者和消息者的Dubbo 都改为公网IP: 3.开放端口:
- xxl-job安装教程
xxl-job是一个开源的分布式调度框架,其他类似的框架还有airflow,oozie等等,需要进行对比 https://github.com/xuxueli/xxl-job 1.首先git clon ...
- java基础---->java8中的函数式接口
这里面简单的讲一下java8中的函数式接口,Function.Consumer.Predicate和Supplier. 函数式接口例子 一.Function:接受参数,有返回参数 package co ...
- C++ Addon Async 异步机制
线程队列: libuv,window 可在libuv官网下载相应版本 opencv: 编译的时候opencv的位数要和 node的bit 一致 兼容electron : node-gyp rebu ...
- js中的 substr方法与substring方法 不同
一个参数时: 二者同为 从 第参数个 开始截取,一直到str 末尾,并返回. 二个参数时: substr(a,b): 从第a个字符开始, 截取b个. substring(a,b): 从第a个字符 ...
- 【python】——购物车
作业需求: 用户名和密码存放于文件中,格式为:egon|egon123 启动程序后,先登录,登录成功则让用户输入工资,然后打印商品列表,失败则重新登录,超过三次则退出程序 允许用户根据商品编号购买商品 ...
- background-color:transparent
background-color没有none值 在工作中发现, 这样是没反应的, 要写这个样式才能去掉背景颜色() background-color属性详细链接: http://www.w3sch ...
- django cookies与session
1. cookiies # cookies def login(request): print('COOKIES',request.COOKIES) print('SESSION',request.s ...