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会爆栈,判断过于复杂也会导致超时,我们采用离线反向操作,先全部涂好,然后把黑格子逐步涂白,我们把每个空白区域当作一个并 ...
随机推荐
- UNIX环境高级编程——存储映射I/O(mmap函数)
共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式,因为进程可以直接读写内存,而不需要任何数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的数据拷贝,而共 ...
- 在go中使用json作为主要的配置格式
最近在用go重构,在先前的代码中,我们使用的ini文件进行配置,但是因为很多历史遗留问题,导致配置混乱,维护困难,自然也需要考虑重构了. 通用配置格式 通用的配置格式有很多,常用的就有ini,json ...
- javascript之BOM地址栏对象(Location)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- mixer: sql词法分析器设计
介绍 mixer希望在proxy这层就提供自定义路由,sql黑名单,防止sql注入攻击等功能,而这些的基石就在于将用户发上来的sql语句进行解析.也就是我最头大的词法分析和语法分析. 到现在为止,我只 ...
- 使用GDAL工具对FY3系列卫星数据进行校正
本文档主要对如何使用GDAL提供的工具对FY3系列卫星数据进行校正处理.FY3系列卫星提供的数据一般是以HDF5格式下发,一个典型的FY3A和FY3B的数据文件名如下: FY3A_MERSI_GBAL ...
- 【一天一道LeetCode】#19. Remove Nth Node From End of List
一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...
- NDK 与 JNI 的关系
简介 JNI是java语言提供的Java和C/C++相互沟通的机制,Java可以通过JNI调用本地的C/C++代码,本地的C/C++的代码也可以调用java代码.JNI 是本地编程接口,Java和C/ ...
- 【freeradius2.x】 安装和学习
虚拟机中centos 安装和学习 radius2 版本是2.2.x 的使用等知识 安装 为了测试方面,yum安装 yum -y install freeradius* 配置文件的位置是 /etc/ra ...
- AngularJS进阶(十七)在AngularJS应用中实现微信认证授权遇到的坑
在AngularJS应用中集成微信认证授权遇到的坑 注:请点击此处进行充电! 前言 项目开发过程中,移动端新近增加了一个功能"微信授权登录",由于自己不是负责移动端开发的,但最后他 ...
- Linux下的 .o、.a、.so文件
http://blog.sina.com.cn/s/blog_656681710100qzmy.html 工程里很多函数只是有声明,找不到实现的代码.因为那些实现代码已经编译成库所以看不见,我所看见的 ...