P3032 [USACO11NOV]二进制数独Binary Sudoku
题目描述
Farmer John's cows like to play an interesting variant of the popular game of "Sudoku". Their version involves a 9 x 9 grid of 3 x 3 subgrids, just like regular Sudoku. The cows' version, however, uses only binary digits:
000 000 000
001 000 100
000 000 000
000 110 000
000 111 000
000 000 000
000 000 000
000 000 000
000 000 000
The goal of binary Sudoku is to toggle as few bits as possible so that each of the nine rows, each of the nine columns, and each of the nine 3 x 3 subgrids has even parity (i.e., contains an even number of 1s). For the example above, a set of 3 toggles gives a valid solution:
000 000 000
001 000 100
001 000 100
000 110 000
000 110 000
000 000 000
000 000 000
000 000 000
000 000 000
Given the initial state of a binary Sudoku board, please help the cows determine the minimum number of toggles required to solve it.
给出一个9*9的01矩阵,问最少修改几个数能使每行、每列以及每个九宫格中1的个数均为偶数。
输入输出格式
输入格式:
* Lines 1..9: Each line contains a 9-digit binary string corresponding to one row of the initial game board.
输出格式:
* Line 1: The minimum number of toggles required to make every row, column, and subgrid have even parity.
输入输出样例
000000000
001000100
000000000
000110000
000111000
000000000
000000000
000000000
000000000
3
说明
The Sudoku board in the sample input is the same as in the problem text above.
Three toggles suffice to solve the puzzle.
Solution:
本题贼有意思。
很容易想到整个棋盘最多只要$81$次翻转(即把每个棋子都翻转),但是显然实际上不用这么多次。
那么我们直接$IDA*$,每个位置的棋子要么翻转要么不翻转,然后分别记录一下每行、每列、每个九宫格内的棋子个数,估价函数就是取三者中不满足偶数条件个数最多的个数(至少需要这么多次翻转才能使棋子变为偶数),然后剪枝就好了。
代码:
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=;
int mp[N][N],dep,tmp[N][N],squ[N],line[N],lit[N];
bool vis[N][N]; il void change(int x,int y){
if(x<=){
if(y<=)squ[]^=;
else if(y<=)squ[]^=;
else squ[]^=;
return;
}
else if(x<=){
if(y<=)squ[]^=;
else if(y<=)squ[]^=;
else squ[]^=;
return;
}
else {
if(y<=)squ[]^=;
else if(y<=)squ[]^=;
else squ[]^=;
return;
}
} il void dfs(int tot,int lsx,int lsy){
if(tot>dep||lsx==)return;
int px=,py=,ps=;
For(i,,) {
if(line[i]&)px++;
if(lit[i]&)py++;
if(squ[i]&)ps++;
}
if((!px)&&(!py)&&(!ps))cout<<tot-,exit();
if(tot+max(px,max(py,ps))>dep)return;
lsy++;
if(lsy>)lsy=,lsx++;
tmp[lsx][lsy]^=,line[lsx]^=,lit[lsy]^=,change(lsx,lsy);
dfs(tot+,lsx,lsy);
tmp[lsx][lsy]^=,line[lsx]^=,lit[lsy]^=,change(lsx,lsy);
dfs(tot,lsx,lsy);
} int main(){
For(i,,) {
For(j,,){
scanf("%1d",&tmp[i][j]);
if(tmp[i][j])line[i]^=,lit[j]^=,change(i,j);
}
}
bool f=;
For(i,,) if((line[i]&)||(lit[i]&)||(squ[i]&)){f=;break;}
if(!f)cout<<;
else while()dep++,dfs(,,);
return ;
}
P3032 [USACO11NOV]二进制数独Binary Sudoku的更多相关文章
- [USACO11NOV]二进制数独Binary Sudoku
传送门 这道题是很好的一道IDA*练习题. 首先我们先确定搜索的框架,我们要求的是用最少的修改次数使得所有的行,列,宫之内都有偶数个1,最直观的想法显然是先预处理出有奇数个1的行,列,宫,之后枚举每一 ...
- 2016年11月3日JS脚本简介数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6.布尔型数据:bool 7.对象类型:object 8.二进制:binary 语言类型: 1.强类型语言:c++ c c# java 2.弱类型语
数据类型: 1.整型:int 2.小数类型: float(单精度) double(双精度) decimal () 3.字符类型: chr 4.字符串类型:sting 5.日期时间:datetime 6 ...
- MySQL 二进制日志(Binary Log)
同大多数关系型数据库一样,日志文件是MySQL数据库的重要组成部分. MySQL有几种不同的日志文件.通常包括错误日志文件,二进制日志,通用日志,慢查询日志,等等.这些日志能够帮助我们定位mysqld ...
- [逆向工程] 二进制拆弹Binary Bombs 快乐拆弹 详解
二进制拆弹 binary bombs 教你最快速解题,成功拆弹 最近计算机基础课,的实验lab2,二进制拆弹,可以说是拆的我很快乐了(sub n, %hair) 此处头发减n 我刚开始做的时候很是懵逼 ...
- Leetcode之回溯法专题-37. 解数独(Sudoku Solver)
Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...
- 二进制日志BINARY LOG清理
mysql> show master logs; +------------------+-----------+ | Log_name | File_size | +------------- ...
- Arduino 串口篇 Arduino发送二进制 send binary via RS232-to-USB to PC
有用的链接在这里:http://hi.baidu.com/mizuda/item/06b7fdc1d0e45a0ec710b2dd 更加详细的内容请查阅Arduino官方:http://arduino ...
- 求解数独难题, Sudoku问题(回溯)
Introduction : 标准的数独游戏是在一个 9 X 9 的棋盘上填写 1 – 9 这 9 个数字,规则是这样的: 棋盘分成上图所示的 9 个区域(不同颜色做背景标出,每个区域是 3 X 3 ...
- [Swift]LeetCode36. 有效的数独 | Valid Sudoku
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...
随机推荐
- jQuery(四)--HTTP请求
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- YII2.0 获取当前访问地址/IP信息
假设我们当前页面的访问地址是:http://localhost/CMS/public/index.php?r=news&id=1 一. 1.获取当前域名:echo Yii::app()-> ...
- 网络编程之socket的运用
一,socket用法 socket是什么 ? Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐 ...
- vue---day01
1.let和const var 全局作用域和函数作用域 存在变量提升 其实是个bug 可以重复声明 let 块级作用域 不存在变量提升 不能重复声明 const 常量 和let一样还有另外两个 定义的 ...
- 初步学习pg_control文件之二
接前文:初步认识pg_control文件 继续学习,pg_control文件在何处形成的?是在initdb的时候,运用的函数如下: /* * This func must be called ONCE ...
- 我的阿里之路+Java面经考点
我的阿里之路+Java面经考点 时间:2018-03-19 23:03 来源:未知 作者:admin 点击:87次 我的2017是忙碌的一年,从年初备战实习春招,年三十都在死磕JDK源码,三 ...
- 「日常训练」Single-use Stones (CFR476D2D)
题意(Codeforces 965D) $w$表示河的宽度,$l$表示青蛙所能跳的最远的距离,第二行的$w-1$个元素表示离河岸为$i$的地方有$a[i]$个石头,一个石头被踩两次,问最多有多少只青蛙 ...
- Qt 实现在隐藏标题栏情况下,窗口的缩放(未成功)
呃,这是一个悲剧的版本,在这版本中,我按照网上大神的说法,试了一下,但是没有效果,不知道出错在了那里,和昨天一样,也是,没有理想的效果,这里贴上代码,记录一下 资料连接:放评论 需要包含头文件 #in ...
- Qt 隐藏标题栏可移动升级版
在最出的时候,在Qt程序隐藏标题栏的情况下,实现界面可拖拽移动,是鼠标在在程序界面的任意位置都可以,现在这个版本是需要鼠标在程序界面的特定位置开可以 上代码 static QPoint last(0, ...
- Ruby中数组的&操作
最近在忙一个项目,好久没有写日志了,项目终于接近尾声,可以适当放松一下,所以记一下在这个项目中发现的有趣事情: 数组的 与 操作 一直以为两个数组A和B相与,谁前谁后都一样,不过这次在项目中突然想试一 ...