题目描述

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.

输入输出样例

输入样例#1:

000000000
001000100
000000000
000110000
000111000
000000000
000000000
000000000
000000000
输出样例#1:

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的更多相关文章

  1. [USACO11NOV]二进制数独Binary Sudoku

    传送门 这道题是很好的一道IDA*练习题. 首先我们先确定搜索的框架,我们要求的是用最少的修改次数使得所有的行,列,宫之内都有偶数个1,最直观的想法显然是先预处理出有奇数个1的行,列,宫,之后枚举每一 ...

  2. 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 ...

  3. MySQL 二进制日志(Binary Log)

    同大多数关系型数据库一样,日志文件是MySQL数据库的重要组成部分. MySQL有几种不同的日志文件.通常包括错误日志文件,二进制日志,通用日志,慢查询日志,等等.这些日志能够帮助我们定位mysqld ...

  4. [逆向工程] 二进制拆弹Binary Bombs 快乐拆弹 详解

    二进制拆弹 binary bombs 教你最快速解题,成功拆弹 最近计算机基础课,的实验lab2,二进制拆弹,可以说是拆的我很快乐了(sub n, %hair) 此处头发减n 我刚开始做的时候很是懵逼 ...

  5. Leetcode之回溯法专题-37. 解数独(Sudoku Solver)

    Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...

  6. 二进制日志BINARY LOG清理

    mysql> show master logs; +------------------+-----------+ | Log_name | File_size | +------------- ...

  7. Arduino 串口篇 Arduino发送二进制 send binary via RS232-to-USB to PC

    有用的链接在这里:http://hi.baidu.com/mizuda/item/06b7fdc1d0e45a0ec710b2dd 更加详细的内容请查阅Arduino官方:http://arduino ...

  8. 求解数独难题, Sudoku问题(回溯)

    Introduction : 标准的数独游戏是在一个 9 X 9 的棋盘上填写 1 – 9 这 9 个数字,规则是这样的: 棋盘分成上图所示的 9 个区域(不同颜色做背景标出,每个区域是 3 X 3 ...

  9. [Swift]LeetCode36. 有效的数独 | Valid Sudoku

    Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to th ...

随机推荐

  1. ASP.NET成员资格和角色管理

    一.成员资格管理 1.成员资格管理模型 ASP.NET提供的成员资格管理功能,其核心是利用内置的成员库表(SQL Server).成员资格管理API(Membership.MembershipUser ...

  2. 第一次使用Git上传本地项目到github

    看了好多帖子,终于在混乱中找到自己适合的方法......自我感觉这个比较简单. 先安装本地git,官方下载地址:http://git-scm.com/download/  根据你自己的系统 下载对应版 ...

  3. 使用NPOI快速导出导入Excel

    这两天做项目需要导入导出EXCEL,是基于NPOI的封装,设计思路是使用DataTable,然后导出一个和DataTable一模一样的Excel表出来 github地址:https://github. ...

  4. mysql 5.7 配置初始化及修改 ROOT 用户密码

    1.修改配置文件 my.ini 放在 mysql\bin [mysqld] basedir=C:\Mysql datadir=C:\Mysql\data port=3306 # server_id = ...

  5. 开源版本 hadoop-2.7.5 + apache-hive-2.1.1 + spark-2.3.0-bin-hadoop2.7整合使用

    一,开源软件版本: hadoop版本 : hadoop-2.7.5 hive版本 :apache-hive-2.1.1 spark版本: spark-2.3.0-bin-hadoop2.7 各个版本到 ...

  6. Hibernate-ORM:16.Hibernate中的二级缓存Ehcache的配置

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述Hibernate中的二级缓存的配置,作者将使用的是ehcache缓存 一,目录 1.二级缓存的具 ...

  7. os模块3

    os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.curd ...

  8. CCS实例,网页栏目

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  9. ExtJs工具篇(3)——Aptana Studio3乱码的问题

            在Aptana Studio里面使用,发现输入的中文是乱码,在浏览器中浏览也是乱码,想着肯定是编码的问题,但是一直没有找到在那个地方设置.以为汉化后就可以了,没想到汉化后竟然还是乱码, ...

  10. XenServer设置master,摧毁故障主机

    XenServer pool 移除server 设置master 这分为Pool Master是正常还是异常2种情况: 正常情况下可能要对Pool Master做一些停机维护,比如换内存条啥的,此时在 ...