题目描述

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. 构建ExtJS 6.x程序

    构建ExtJS 6.x程序 ExtJS也有自己的打包工具 SenchaCmd,它用来生成构建ExtJS前端组织架构,最后打包发布生产,操控着前端整个开发生命周期,SenchaCmd依赖于JDK,所以要 ...

  2. PHP-入门指引1

    PHP("PHP: Hypertext Preprocessor",超文本预处理器的字母缩写)是一种被广泛应用的开放源代码的多用途脚本语言,它可嵌入到 HTML中,尤其适合 web ...

  3. Scrapy框架的基本使用

    安装 pip install scrapy 基础使用 1. 创建一个工程:scrapy startproject 2. 在工程目录下创建一个爬虫文件 cd 工程 scrapy genspider 爬虫 ...

  4. 多线程编程之Apue3rd_Chapter11之互斥锁_读写锁_自旋锁

    学习了apue3rd的第11章,主要讲的是多线程编程.因为线程共享进程的资源比如堆和全局变量,多线程编程最重要的是,使用各种锁进行线程同步. 线程编程首先要学习的三个函数如下: #include &l ...

  5. linux网络服务实验

    1.设置window IP地址为192.168.3.XX,掩码24位. 2.设置Linux IP地址为192.168.3.YY,掩码24位.window与Linux互相ping通. 3.在linux中 ...

  6. linux实验-基本指令1

    1.root帐号登录,查看/tmp目录,如果/tmp目录下没有子目录myshare,则建立该目录. 2.创建帐号testuser. 3.把myshare目录及其目录下的所有文件和子目录的拥有者该为te ...

  7. sudo mount -o loop pm.img /mnt/floppy

    sudo mount -o loop pm.img /mnt/floppy 最近在学<一个操作系统的实现>,由于这本书比较老了,所以有一些对于软盘的操作指令现在用会出现一些错误,当我进行虚 ...

  8. jsp中的input

    Input表示Form表单中的一种输入对象,其又随Type类型的不同而分文本输入框,密码输入框,单选/复选框,提交/重置按钮等,下面一一介绍. 1,type=text 输入类型是text,这是我们见的 ...

  9. 为WPF中DropShadowBitmapEffect提供轻量级的替代品

    原文:为WPF中DropShadowBitmapEffect提供轻量级的替代品 为WPF中DropShadowBitmapEffect提供轻量级的替代品                         ...

  10. 13 ThreadLocal

    ThreadLocal 在多线程环境下,每个线程都有自己的数据.一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁. 1. 使用函数 ...