51nod 1851 俄罗斯方块(思维题)

分析:假设n>=m,m为1,2单独讨论下,否则可以用第二行第一个把所有黑块搞到2x2的格子里,不断用凸出来的那个角一列一列把黑的变白就行了。然后只要黑色有偶数块都可以构造出来。复杂度O(nm)

#include <iostream>
#include <cstring>
#include <cstdio>using namespace std;
class Board{
public:char **a;int n, m;void init(int _n, int _m){n = _n, m = _m;a = new char*[n];for(int i = 0; i < n; i++)a[i] = new char[m];}Board(){}Board(int _n, int _m){init(_n, _m);}~Board(){for(int i = 0; i < n; i++)delete[] a[i];delete[] a;}void print(){for(int i = 0; i < n; i++){for(int j = 0; j < m; j++)cout<<a[i][j];cout<<endl;}}
};
void inline change(char &a){a = '1' + '0' - a;
}
inline void changex(char **a, int n, int m, int x, int y){if(y == 0){change(a[x][y]);change(a[x-1][y]);change(a[x-1][y+1]);change(a[x-2][y+1]);}else{change(a[x][y]);change(a[x-1][y]);change(a[x-1][y-1]);change(a[x-2][y-1]);}
}
inline void changey(char **a, int n, int m, int x, int y){if(x == 0){change(a[x][y]);change(a[x][y-1]);change(a[x+1][y-1]);change(a[x+1][y-2]);}else{change(a[x][y]);change(a[x][y-1]);change(a[x-1][y-1]);change(a[x-1][y-2]);}
}
bool solve(char **a, int n, int m){if(n == 2 && m == 2){if(a[0][1] != a[0][0] || a[1][0] != a[0][0] || a[1][1] != a[0][0])return false;return true;}else if(n <= 2 && m <= 2){for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)if(a[i][j] == '1')return false;return true;}else if(n == 1){while(m >= 4){if(a[0][m-1] == '1'){change(a[0][m-1]);change(a[0][m-2]);change(a[0][m-3]);change(a[0][m-4]);}m--;}if(a[0][0] == '1' || a[0][1] == '1' || a[0][2] == '1')return false;else return true;}else if(m == 1){while(n >= 4){if(a[n-1][0] == '1'){change(a[n-1][0]);change(a[n-2][0]);change(a[n-3][0]);change(a[n-4][0]);}n--;}if(a[0][0] == '1' || a[1][0] == '1' || a[2][0] == '1')return false;else return true;}while(m >= 3){for(int i = 0; i < n; i++)if(a[i][m-1] == '1')changey(a, n, m, i, m-1);m--;}while(n >= 3){for(int i = 0; i < m; i++)if(a[n-1][i] == '1')changex(a, n, m, n-1, i);n--;}int cnt = 0;for(int i = 0; i < n; i++)for(int j = 0; j < m; j++)cnt += a[i][j]=='1'?1:0;if(cnt % 2 == 0)return true;return false;
}
int main()
{
//    freopen("in", "r", stdin);int t;cin>>t;while(t--){int n, m;scanf("%d%d\n", &n, &m);Board *a = new Board(n, m);int i = 0, j = 0;while(i < n){a->a[i][j] = getchar();j++;if(j == m){getchar();i++;j = 0;}}if(solve(a->a, a->n, a->m))cout<<"Yes"<<endl;else cout<<"No"<<endl;delete a;}return 0;
}

 

转载于:https://www.cnblogs.com/7391-KID/p/10610710.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/394540.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python发邮件详解_python实现发送邮件详解

[Python]代码#_*_encoding:utf-8_*_#script for python3.2#-------------------------------------------------------------------------------# Name: 发送邮件# Purpose:## Author: QiuChangJie## Created: 10/09/2012# Copyright: (c) cj.qiu 2012# Licence: #------------…

gprof, Valgrind and gperftools - an evaluation of some tools for application level CPU profiling on

2019独角兽企业重金招聘Python工程师标准>>> In this post I give an overview of my evaluation of three different CPU profiling tools: gperftools, Valgrind and gprof. I evaluated the three tools on usage, functionality, accuracy and runtime overhead…

xp计算机属性打不开,xp系统我的电脑右键属性打不开怎么办

在使用xp系统过程中,我们经常需要打开“我的电脑”右键属性,查看系统信息以及进行虚拟内存、性能方面的设置,不过有深度技术ghost xp sp3纯净版用户右键点击我的电脑,发现右键菜单中的“属性”打不开,出现这个问题通常是注册表禁用了这个属性,下面小编跟大家介绍xp系统我的电脑…

状态机学习(二)解析INI文件

题目来自<系统程序员成长计划> 作者:李先静. 状态变化如下 #include <string> #include <iostream> using namespace std;string s "[GRP]\n\ name def \n\ data 2016.11.29 \r\n\ ; this is a comment \r\n\ str this is a test \n\ [zhangshan]…

接口之用例编写、验证

一、用Excel编写用例&#xff08;xlsx格式&#xff09; 截图仅供参考&#xff0c;实际用例编写需要根据实际情况来。 二、用例加载、验证 1、数据的加载 import xlrd,xlwt #python操作excel主要用到xlrd和xlwt这两个库&#xff0c;即xlrd是读excel&#xff0c;xlwt是写excel的库…

计算机二级word真题书娟,计算机二级word试题.docx

PAGEPAGE # / 80Word试题在考生文件夹下打开文档 word.docx &#xff0c;按照要求完成下列操作并以该文件名( word.docx )保存文档。某高校为了使学生更好地进行职场定位和职业准备&#xff0c;提高就业能力&#xff0c;该校学工处将于2013 年 4月 29 日(星期五) 19:30-21:30 在…

农场js_通过销售农场商品来解释Web API

农场jsby Kevin Kononenko凯文科诺年科(Kevin Kononenko) 通过销售农场商品来解释Web API (Web APIs explained by selling goods from your farm) If you have been to a farmer’s market or farm stand, then you can understand the concept of an application programmin…

python安装pyqt4_windows下安装PyQt4

第一步&#xff1a;确认自己电脑上的Python版本。然后下载对应的.whl文件下载第二步&#xff1a;https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyqt4上下载对应版本版本的.whl文件。例如cp-27-cp27m就代表是python2.7的版本。如果要下载python3.6且电脑是64位的则需要下载PyQt…

repcached配置与简单測试

安装libevent-devel进行configure。安装在文件夹/usr/local/repcached下编译安装完毕启动11211节点启动11212节点编写文件验证复制&#xff08;分别向1、2节点存入数据&#xff0c;验证复制&#xff09;ruby执行结果

为Activity设置特定权限才能启动

1.在AndroidManifest文件中&#xff0c;声明一个权限&#xff0c;并在activity中添加属性 <!--声明权限&#xff0c;权限名一般为包名permission类名 --><permission android:name"com.jikexueyuan.notepad.specpermission.permission.MyAty"/> <acti…

nashPay项目遇到的问题

浏览器提示错误代码&#xff1a; Failed to load resource: net::ERR_CONNECTION_REFUSED 出现这个问题是core服务异常&#xff0c;重启core服务可解决 layUi 下拉框赋值 var loadZhongduan function (data) { admin.req({ url: baseUrl "shoukuanZhongduan/getList&quo…

使用Express在Node.js中实现非常基本的路由

by Pau Pavn通过保罗帕文(PauPavn) 使用Express在Node.js中实现非常基本的路由 (Really, really basic routing in Node.js with Express) The goal of this story is to briefly explain how routing works in Express while building a simple — very simple — Node app.这…

计算机抄作通用模块,通用命令行模块的设计及实现

摘要&#xff1a;自从上个世纪八十年代以来,图形用户界面得到快速发展,计算机逐渐进入各类企业,家庭,其应用得到广泛的推广.对比起命令行界面来说,图形界面在交互性上有着不可比拟的优势.但在一些需要执行大量重复性工作的方面,例如在系统管理上,命令行界面提供的脚本功能,能够…

python读写磁盘扇区数据_C++-如何直接读取Windows磁盘扇区的数据?

1.通过CreateFile系列来完成读写扇区可以通过CreateFile打开磁盘逻辑分区&#xff0c;还要通过SetFilePointer以文件操作的方式把指针移到要操作的磁盘扇区开始处&#xff0c;在定位到要访问的扇区开始位置后就可以通过ReadFile或WriteFile函数实施相应的读写访问了&#xff0c…

公司 邮件 翻译 培训 长难句 结课

今天结课啦。。。。。。 明天培训总结&#xff0c;讲翻译技巧总结。 1new forms of thoughts as well as new subjects for thought must arise in the future as they have in the past, giving rise to new standards of elegance. 2if the small hot spots look as expected…

元祖(转载)

一.基本数据类型  整数&#xff1a;int  字符串&#xff1a;str(注&#xff1a;\t等于一个tab键)  布尔值&#xff1a; bool  列表&#xff1a;list   列表用[]  元祖&#xff1a;tuple  元祖用&#xff08;&#xff09;  字典&#xff1a;dict注&#xff1a;所…

leetcood学习笔记-226- 翻转二叉树

题目描述&#xff1a; 第一次提交&#xff1a; class Solution(object):def invertTree(self, root):""":type root: TreeNode:rtype: TreeNode"""if not root:return Nonetemp root.leftroot.left root.rightroot.right temp# root.left,…

现代JavaScript中的精美图案:制冰厂

I’ve been working with JavaScript on and off since the late nineties. I didn’t really like it at first, but after the introduction of ES2015 (aka ES6), I began to appreciate JavaScript as an outstanding, dynamic programming language with enormous, expres…

惠普omen测试软件,双GTX1080奢华魔方PC 惠普OMEN X评测

惠普最近一段时间在游戏PC领域着力发力&#xff0c;桌面的暗影精灵家族热卖&#xff0c;如火如荼的势头终于传导到了台式机领域。而今&#xff0c;惠普也终于有了自己正统意义上的重型武器——桌面游戏台式机OMEN 900暗影精灵II 系列。今天我们就要为大家评测这款三万元的台式机…

python 清华镜像_Anaconda3清华镜像 V5.3.1 最新免费版

相关软件软件大小版本说明下载地址Anaconda3清华镜像是一款功能强大的python管理工具&#xff0c;此软件集成了Conda和Python等大量科学计算分析的包&#xff0c;可以帮助用户快速实现项目环境的配置&#xff0c;有需要的赶快来试试吧&#xff01;【功能特点】1、省时省心&…