CF508A Pasha and Pixels 题解
Content
有一个 \(n\times m\) 的矩阵,一开始全部格子被染成白色。 接下来有 \(k\) 个操作,每一个操作表示把一个格子染成黑色。问第一次出现 \(2\times 2\) 的全部涂成黑色的矩阵是第几个操作,或者没有出现这样的矩阵。
数据范围:\(1\leqslant n,m\leqslant 1000,1\leqslant k\leqslant 10^5\)。
Solution
我们可以边涂黑边判断,假设现在将 \((x,y)\) 涂成黑色。那么,出现一个 \(2\times 2\) 的全部涂成黑色的矩阵只会有 \(4\) 种情况:
- \((x,y),(x-1,y),(x,y-1),(x-1,y-1)\) 全部涂成黑色。
- \((x,y),(x+1,y),(x,y-1),(x+1,y-1)\) 全部涂成黑色。
- \((x,y),(x-1,y),(x,y+1),(x-1,y+1)\) 全部涂成黑色。
- \((x,y),(x+1,y),(x,y+1),(x+1,y+1)\) 全部涂成黑色。
只要满足上面四种情况中的任意一种,直接输出答案结束。否则,操作全部执行完毕还是没有出现,输出 \(0\)。
Code
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
int n, m, k, a[1007][1007];
bool check1(int x, int y) {
return a[x][y] && a[x - 1][y] && a[x][y - 1] && a[x - 1][y - 1];
}
bool check2(int x, int y) {
return a[x][y] && a[x + 1][y] && a[x][y - 1] && a[x + 1][y - 1];
}
bool check3(int x, int y) {
return a[x][y] && a[x - 1][y] && a[x][y + 1] && a[x - 1][y + 1];
}
bool check4(int x, int y) {
return a[x][y] && a[x + 1][y] && a[x][y + 1] && a[x + 1][y + 1];
}
int main() {
scanf("%d%d%d", &n, &m, &k);
for(int i = 1; i <= k; ++i) {
int x, y;
scanf("%d%d", &x, &y);
a[x][y] = 1;
if(check1(x, y) || check2(x, y) || check3(x, y) || check4(x, y))
return printf("%d", i), 0;
}
return printf("0"), 0;
}
CF508A Pasha and Pixels 题解的更多相关文章
- 【codeforces 508A】Pasha and Pixels
[题目链接]:http://codeforces.com/contest/508/problem/A [题意] 让你在一个n*m的方格上给方格染色; 顺序给出染色的k个格子 如果在某一时刻 有一个2* ...
- 模拟 Codeforces Round #288 (Div. 2) A. Pasha and Pixels
题目传送门 /* 模拟水题:给定n*m的空白方格,k次涂色,将(x,y)处的涂成黑色,判断第几次能形成2*2的黑色方格,若不能,输出0 很挫的判断四个方向是否OK */ #include <cs ...
- Codeforces Round #288 (Div. 2)
A. Pasha and Pixels 题意就是给一个n*m的矩阵,k次操作,一开始矩阵全白,一次操作可以染黑一个格子,问第几次操作可以使得矩阵中存在一个2*2的黑色矩阵.直接模拟即可 代码: ...
- UVA - 297Quadtrees(四分图)
Quadtrees Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Statu ...
- LeetCode Smallest Rectangle Enclosing Black Pixels
原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...
- Codeforces Round #337 (Div. 2) A. Pasha and Stick 水题
A. Pasha and Stick Pasha has a wooden stick of some positive integer length n. He wants to perform ...
- 302. Smallest Rectangle Enclosing Black Pixels
题目: An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The b ...
- Codeforces Round #337 (Div. 2) A. Pasha and Stick 数学
A. Pasha and Stick 题目连接: http://www.codeforces.com/contest/610/problem/A Description Pasha has a woo ...
- Codeforces Round #311 (Div. 2)B. Pasha and Tea 水题
B. Pasha and Tea Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/prob ...
随机推荐
- vue的常用指令
https://www.bootcdn.cn/ 前端资源库 <!-- 常用内置指令 v:text : 更新元素的 textContent v-html : 更新元素的 innerHTML v-i ...
- FJD1T1
在考场上因为一些原因,系统编译不了. 于是在最后\(1h\)把\(T3\)得重打一遍,所以这题的暴力没有写完. 不过也确实很蠢,没想到做法. 考虑搜索原串中的字母的对应取值,然后计算出结果的柿子. 考 ...
- WC 2007 剪刀石头布
WC 2007 剪刀石头布 看到这个三元环的问题很容易可以考虑到求不合法的三元环的数量的最小值. 什么情况不合法?既然不合法,当且仅当三元环中有一个人赢了另外两个人.所以我们考虑对于一个人而言,如果她 ...
- Codeforces 1491G - Switch and Flip(构造题)
Codeforces 题目传送门 & 洛谷题目传送门 obviously,难度高一点的构造题对我来说都是不可做题 首先考虑将排列拆成一个个置换环,也就是 \(\forall i\) 连边 \( ...
- perl 获取目录信息
1 #!/usr/bin/perl -w 2 use strict; 3 use FindBin qw($Bin $Script); 4 5 my $rp=$Bin; 6 print "th ...
- jquery操作html中图片宽高自适应
在网站制作中如果后台上传的图片不做宽高限制,在前台显示的时候,经常会出现图片变形,实用下面方法可以让图片根据宽高自适应,不论是长图片或者高图片都可以完美显示. $("#myTab0_Cont ...
- 汇编LED实验
汇编语言点亮LED 拿到一款全新的芯片,第一个要做的事情的就是驱动其 GPIO,控制其 GPIO 输出高低电平. GPIO口是IO口的一个功能之一. 一.接下来的步骤离不开芯片手册: 1.使能所有时钟 ...
- 24. 解决Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
第一种: sudo vim /etc/resolv.conf 添加nameserver 8.8.8.8 第二种: /etc/apt/sources.list 的内容换成 deb http://old- ...
- Spark中的分区方法详解
转自:https://blog.csdn.net/dmy1115143060/article/details/82620715 一.Spark数据分区方式简要 在Spark中,RDD(Resilien ...
- 在 windows 系统上 安装与配置 PHP + Apache
参考:http://www.cnblogs.com/pharen/archive/2012/02/06/2340628.html 在大学时候上过一门PHP课时,因为课堂需要配置过一次PHP+Mysql ...