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 ...
随机推荐
- raid概述与CentOS7.4中raid5的搭建与测试
前言 一.raid的定义与作用 raid(独立冗余磁盘阵列).raid技术通过把多个硬盘设备组合成一个容量更大的,安全性更好的磁盘阵列.把数据切割成许多区段后分别放在不同的物理磁盘上,然后利用分散读写 ...
- CentOS下删除MySql
1.查找以前是否装有mysql rpm -qa | grep -i mysql 显示之前安装了: MySQL-client-5.5.49-1.linux2.6.i386 MySQL-server-5. ...
- JS 红包随机
微信随机红包,指定金额指定用户,随机发送红包 var moneys = new Array(); var moneyTotal = 0; function rand(obj){ if(obj.size ...
- HBase学习(三):数据模型
和传统的关系型数据库类似,HBase以表(Table)的方式组织数据.HBase的表由行(Row)和列(Column)共同构成,与关系型数据库不同的是HBase有一个列族(ColumnFamily)的 ...
- Linux编译移植Qt5的环境_OMAPL138平台
Linux编译移植Qt5_OMAPL138 [导语]:昨天编译Qt5各种失败,各种离奇的错误在Google上面也搜索不到,真是让人"蛋疼菊紧",今天把所有的环境全部清理干净,也重新 ...
- ansible自动化运维入门
1.ansible的安装 1)使用源码安装Python3.5 安装支持包 yum -y install lrzsz vim net-tools gcc gcc-c++ ncurses ncurses- ...
- QOS-QOS(服务质量)概述
QOS-QOS(服务质量)概述 2018年7月7日 20:29 概述及背景: 1. 引入: 传统IP网络仅提供“尽力而为”的传输服务,网络有可用资源就转发,资源不足时就丢弃 新一代IP网络承载了 ...
- 001---Python简介
编程语言: 机器语言 最底层,更容易被计算机识别,执行速度最快 复杂,开发效率低 汇编语言 比较底层,执行速度较快 同样复杂 高级语言 编译型语言:先编译,后执行.生成独立的可执行文件.是计算机可以理 ...
- 解决 Python2 和 Python3 的共存问题
首先安装两种版本的Python 进入系统属性更改环境变量 将两个版本的安装路径找出. 添加至PATH中,变量之间用分号隔开. D:\Python36\Scripts\;D:\Python36\;D:\ ...
- Xshell 清除历史记录方法
使用电脑久了,就会清理电脑,将一些历史记录清除,使得电脑可以运行的更快,Xshell也是同样的道理.本集小编就教大家如何清除xshell的历史记录. 如何清除历史记录: 1.打开xshell,然后点击 ...