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会爆栈,判断过于复杂也会导致超时,我们采用离线反向操作,先全部涂好,然后把黑格子逐步涂白,我们把每个空白区域当作一个并 ...
随机推荐
- Uva - 816 - Abbott's Revenge
这个迷宫问题还是挺好玩的,多加了一个转向的问题,有些路口不同的进入方式会有不同的转向限制,这个会比较麻烦一点,所以定义结点结构体的时候需要加一个朝向dir.总体来说是一道BFS求最短路的问题.最后打印 ...
- Linux下配置Tomcat
***安装*** 安装JDK chmod a+x jdk-1_5_0_06-linux-i586-rpm.bin ./jdk-1_5_0_06-linux-i586-rpm.bin 敲几次空格就O ...
- Android开机键失灵启动手机的解决办法
问题描述 Android手机的关机键损坏,无法开机. 解决方法 将手机通过USB线链接电脑,进入命令行,找到adb命令所在目录,运行如下命令: adb reboot 注意:用这种方法的前提是,如果你当 ...
- leetcode之 Palindrome Partitioning I&II
1 Palindrome Partitioning 问题来源:Palindrome Partitioning 该问题简单来说就是给定一个字符串,将字符串分成多个部分,满足每一部分都是回文串,请输出所有 ...
- 总账:日记账导入流程(文档 ID 1591640.1)
文档内容 概要 历史记录 详细信息 GL_INTERFACE_CONTROL GL_INTERFACE_HISTORY GL_IMPORT_REFERENCES 摘要 ...
- 时间序列分解-STL分解法
时间序列分解-STL分解法 [转载时请注明来源]:http://www.cnblogs.com/runner-ljt/ Ljt 作为一个初学者,水平有限,欢迎交流指正. STL(’Seasonal a ...
- 2、Libgdx配置你的开发环境(Eclipse,Intellij IDEA,NetBeans)
Libgdx 项目使用 Gradle管理依赖,构建过程和IDE整合.这使得你可以使用你喜欢的开发环境开发你的应用.不要提交跟IDE的特定文件到你的源码控制系统中. 配置Eclipse 要想通过Ecli ...
- STL算法设计理念 - 函数适配器
1)函数适配器的理论知识 2)常用函数函数适配器 标准库提供一组函数适配器,用来特殊化或者扩展一元和二元函数对象.常用适配器是: 1.绑定器(binder): binder通过把二元函数对象的一个实参 ...
- 【56】java本地文件File类详解
1.java类的介绍 public class File extends Object implements Serializable, Comparable<File> 文件和目录路径名 ...
- 安卓笔记--intent传值不更新问题
今天在学习安卓的过程中,遇到一个问题,就是用intent进行多次传值的话, 他永远是第一次的值 后来发现,intent接收数据被写到了onCreat();方法中,这时候finish();到上一个Act ...