Artwork
A template for an artwork is a white grid of n × m squares. The artwork will be created by painting q horizontal and vertical black strokes. A stroke starts from square (x1, y1), ends at square (x2, y2) (x1 = x2 or y1 = y2) and changes the color of all squares (x, y) to black where x1 ≤x≤x2 andy1 ≤y≤y2.
The beauty of an artwork is the number of regions in the grid. Each region consists of one ormore white squares that are connected to each other using a path of white squares in the grid,walking horizontally or vertically but not diagonally. The initial beauty of the artwork is 1. Yourtask is to calculate the beauty after each new stroke. Figure A.1 illustrates how the beauty of theartwork varies in Sample Input 1.
Input
The first line of input contains three integers n, m and q (1 ≤ n,m ≤ 1000, 1 ≤ q ≤ 104).Then follow q lines that describe the strokes. Each line consists of four integers x1, y1, x2
andy2 (1≤x1 ≤x2 ≤n,1≤y1 ≤y2 ≤m).Either x1 =x2 or y1 =y2 (or both).
Output
For each of the q strokes, output a line containing the beauty of the artwork after the stroke.
样例输入
4 6 5
2 2 2 6
1 3 4 3
2 5 3 5
4 6 4 6
1 6 4 6
样例输出
1
3
3
4
3 思路:使用广度搜索。模拟题意为,通过输入的数据染对应方块的颜色为黑色,然后设定一个起点,上下左右递归遍历,如果存在白色块则把它染成别的颜色(不能是黑色了)。递归完成之后记得把颜色恢复成白色。
我的代码:
import java.util.Scanner; public class Main {
static Scanner scanner = new Scanner(System.in);
static int m = 0, n = 0, q = 0;
static int beauty = 0; public static void main(String[] args) {
long t1=System.currentTimeMillis();
int x1, y1, x2, y2;
n = scanner.nextInt();//列
m = scanner.nextInt();//行
q = scanner.nextInt();
int[][] arr = new int[m + 2][n + 2];
for (int i = 0; i < n + 2; i++) {
arr[0][i] = 9;
arr[m + 1][i] = 9;
}
for (int i = 0; i < m + 2; i++) {
arr[i][0] = 9;
arr[i][n + 1] = 9;
}
for (int i = 0; i < q; i++) {
x1 = scanner.nextInt();
y1 = scanner.nextInt();
x2 = scanner.nextInt();
y2 = scanner.nextInt();
if (x1 == x2 && y1 == y2) {
arr[y1][x1] = 1;
} else if (x1 == x2) {
for (int j = y1; j <= y2; j++) {
arr[j][x1] = 1;
}
} else if (y1 == y2) {
for (int j = x1; j <= x2; j++) {
arr[y1][j] = 1;
}
}
System.out.println(cal(1, 1, arr));
for (int j = 0; j < m + 2; j++) {
for (int k = 0; k < n + 2; k++) {
if (arr[j][k] == -1) {
arr[j][k] = 0;
}
}
}
beauty = 0;
}
long t2=System.currentTimeMillis();
System.out.println(t2-t1+"ms");;
} public static int cal(int x, int y, int[][] arr) { //找到白块
for (int i = x; i < m + 1; i++) {
for (int j = y; j < n + 1; j++) {
if (arr[i][j] == 0) {//找到白块
//开始染色,把与白块相邻的都染色
arr[x][y]=-1;
stroke(i, j, arr);
beauty++;
}
}
}
return beauty;
} public static void stroke(int x, int y, int[][] arr) {
//上右下左顺序 if (arr[x][y - 1] == 0) {
arr[x][y - 1] = -1;
stroke(x, y - 1, arr);
}
if (arr[x + 1][y] == 0) {
arr[x + 1][y] = -1;
stroke(x + 1, y, arr);
}
if (arr[x][y + 1] == 0) {
arr[x][y + 1] = -1;
stroke(x, y + 1, arr);
}
if (arr[x - 1][y] == 0) {
arr[x - 1][y] = -1;
stroke(x - 1, y, arr);
} }
}
但是有个问题,就是如果数据稍微多一点的时候,栈就溢出了。这个问题以后再想办法优化一下吧。
Artwork的更多相关文章
- NCPC 2016 October 8,2016 Artwork
Problem A Artwork Problem ID: artwork Time limit: 4 seconds A template for an artwork is a white gri ...
- Gym 102346A Artwork dfs
Artwork Gym - 102346A 题意:给n*m的地图,入口是(0,0),出口是(n,m),其中有k个监视器,坐标是(xi,yi),监视半径是r,问一个人能不能不被监视到,从起点到终点. 如 ...
- Artwork (Gym - 102346A)【DFS、连通块】
Artwork (Gym - 102346A) 题目链接 算法 DFS,连通块 时间复杂度:O(k*n + k * k) 1.这道题就是让你判断从(0,0)到(m,n),避开中途所有的传感器(传感器的 ...
- UVa LA 4636 Cubist Artwork 难度: 0
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- UVALive - 4636 Cubist Artwork(贪心)
题目链接 题意 给出正视图和侧视图,判断最少用几个立方体 分析 若存在高度相同的立方块,则以数目多的那面为准. #include <iostream> #include <cstdi ...
- Gym - 101550A Artwork (并查集在线做法)
题目链接 题意:给你一个n*m的网格图,初始时格点全白,每次可以将一段连续的格点涂黑.求出每次操作之后白色连通块的数量. 看了看网上的题解,基本全是离线的做法.其实这道题是有在线的做法的,利用了对偶图 ...
- UVa 1445 - Cubist Artwork
统计正面看高度为i的竖条个数为cnt1[i], 统计侧面看高度为i的竖条个数为cnt2[i]: ans = sum( i * max( cnt1[i], cnt2[i] ) ); ( 1 <= ...
- Artwork 18年中南多校第一场A
一.题意 对于一个矩阵,若干道命令,每道命令将会把某一段格子涂黑,请问每次涂黑之后矩阵中未被涂黑的块的数量? 二.思路 保存每道命令,并且忠实的执行他,到最后一步开始搜索联通块的数量,并将其保存. 之 ...
- Artwork Gym - 101550A 离线并查集
题目:题目链接 思路:每个空白区域当作一个并查集,因为正着使用并查集分割的话dfs会爆栈,判断过于复杂也会导致超时,我们采用离线反向操作,先全部涂好,然后把黑格子逐步涂白,我们把每个空白区域当作一个并 ...
随机推荐
- java设计模式---职责链模式
职责链的本质:分离职责,动态组合 样例: /** * 定义职责对象的接口 * */ public abstract class Handler { protected Handler successo ...
- 我眼中的Linux设备树(四 中断)
四 中断中断一般包括中断产生设备和中断处理设备.中断控制器负责处理中断,每一个中断都有对应的中断号及触发条件.中断产生设备可能有多个中断源,有时多个中断源对应中断控制器中的一个中断,这种情况中断产生设 ...
- 如何让你的web具备权限认证
大多数Web系统都有权限需求,前面已经了解了它的整个认证过程的原理,这节将讲述如何在Tomcat中配置web资源的权限.先以Tomcat默认的认证模式Basic和默认的域UserDatabaseRea ...
- Leetcode_13_Roman to Integer
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41486885 通过本文你可能学到的知识如下: (1)理解本 ...
- 关于MySQL insert into ... select 的锁情况
摘要: 一直以为"insert into tb select * from tbx" 这样的导入操作是会把tbx表给锁住的,在锁期间是不允许任何操作(保证一致性).看完 ...
- 从驱动层分析android的Binder机制-android学习之旅(83)
前言及知识准备 Binder驱动程序 Service组件的注册和启动 Clinet应用获取服务 本次主要介绍Android平台下Binder进程间机制.从机制的组成出发,将按照Binder驱动程序.B ...
- Cocos2D的随机数生成函数
有很多种方法生成随机数.但是只有arc4random函数生成的最接近于"真随机(truly random)"数.(而且不需要种子) 其变体函数arc4random_uniform生 ...
- SQL-Teradata基础
1.创建一个和表 pnr_1 结构一样的表 Create table pnr_2 as pnr_1 with no data 不含数据 Create table pnr_2 as pnr_1 wit ...
- 关于SMALI语法
dalvik字节码有两种类型,原始类型和引用类型.对象和数组是引用类型,其它都是原始类型.V void,只能用于返回值类型Z booleanB byteS shortC charI int ...
- 2015/12/24:嵌入式C语言的位操作随笔
今晚是平安夜,首先祝大家平安夜快乐,明天是圣诞,祝大家圣诞快乐!! 好了,这周都特别有空,上班也非常轻松,基本就是看看内核驱动,学学安卓,没什么正事的开发活干.今晚,我们来总结一例在现实 ...