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的更多相关文章

  1. 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 ...

  2. Gym 102346A Artwork dfs

    Artwork Gym - 102346A 题意:给n*m的地图,入口是(0,0),出口是(n,m),其中有k个监视器,坐标是(xi,yi),监视半径是r,问一个人能不能不被监视到,从起点到终点. 如 ...

  3. Artwork (Gym - 102346A)【DFS、连通块】

    Artwork (Gym - 102346A) 题目链接 算法 DFS,连通块 时间复杂度:O(k*n + k * k) 1.这道题就是让你判断从(0,0)到(m,n),避开中途所有的传感器(传感器的 ...

  4. UVa LA 4636 Cubist Artwork 难度: 0

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  5. UVALive - 4636 Cubist Artwork(贪心)

    题目链接 题意 给出正视图和侧视图,判断最少用几个立方体 分析 若存在高度相同的立方块,则以数目多的那面为准. #include <iostream> #include <cstdi ...

  6. Gym - 101550A Artwork (并查集在线做法)

    题目链接 题意:给你一个n*m的网格图,初始时格点全白,每次可以将一段连续的格点涂黑.求出每次操作之后白色连通块的数量. 看了看网上的题解,基本全是离线的做法.其实这道题是有在线的做法的,利用了对偶图 ...

  7. UVa 1445 - Cubist Artwork

    统计正面看高度为i的竖条个数为cnt1[i], 统计侧面看高度为i的竖条个数为cnt2[i]: ans = sum( i * max( cnt1[i], cnt2[i] ) ); ( 1 <= ...

  8. Artwork 18年中南多校第一场A

    一.题意 对于一个矩阵,若干道命令,每道命令将会把某一段格子涂黑,请问每次涂黑之后矩阵中未被涂黑的块的数量? 二.思路 保存每道命令,并且忠实的执行他,到最后一步开始搜索联通块的数量,并将其保存. 之 ...

  9. Artwork Gym - 101550A 离线并查集

    题目:题目链接 思路:每个空白区域当作一个并查集,因为正着使用并查集分割的话dfs会爆栈,判断过于复杂也会导致超时,我们采用离线反向操作,先全部涂好,然后把黑格子逐步涂白,我们把每个空白区域当作一个并 ...

随机推荐

  1. Unity UGUI基础之Slider、Scrollbar

    Slider(滑动条):是一个主要用于形象的拖动以改变目标值的控件,他的最恰当应用是用来改变一个数值,最大值和最小值自定义,拖动滑块可在此之间改变,例如改变声音大小. Scrollbar(滚动条):是 ...

  2. Cocos2D旋转炮塔到指定角度(二)

    增加如下代码到ccTouchesEnded方法中,就在你在导弹精灵上调用runAction之前: // Determine angle to face float angleRadians = ata ...

  3. AngularJS 入门教程 $http is not defined 解决方案

    采用从git下载的教程, www.angularjs.cn 版本的 入门教程,在第5步的时候 签出文件: git checkout -f step-5 运行将会提示: $http is not def ...

  4. Red Hat Enterprise Linux 5 64-bit chinese language support config steps

    Red Hat Enterprise Linux 5 64-bit 系统下安装中文语言支持方法 测试环境:Windows2012+Vmvare9.0+Red Hat Enterprise Linux ...

  5. iOS中 SDWebImage手动清除缓存的方法 技术分享

    1.找到SDImageCache类 2.添加如下方法: - (float)checkTmpSize { float totalSize = 0; NSDirectoryEnumerator *file ...

  6. Chipmunk僵尸物理对象的出现和解决(三)

    首先是触摸移动反弹棒的代码: -(void)touchMoved:(CCTouch *)touch withEvent:(CCTouchEvent *)event{ CGPoint location ...

  7. (四十三)UITabBarController和AppDelegate的一些细节

    假设一个UITabBar是V1,V2,V3三个视图的根控制器. 一进入程序的时候,只会加载一个视图V1,也就是说UITabBar是延迟加载的. Tip:通过代码创建的UITabBar会一次性加载所有视 ...

  8. 识别你的ADFS是什么版本的(Which version of ADFS is running)

    各版本的ADFS版本识别见如下链接: http://jorgequestforknowledge.wordpress.com/2014/02/23/gathering-architectural-de ...

  9. 【一天一道LeetCode】#51. N-Queens

    一天一道LeetCode系列 (一)题目 The n-queens puzzle is the problem of placing n queens on an n×n chessboard suc ...

  10. 【Linux学习笔记】关于ubuntu开机菜单栏和任务栏不见了的有效解决方法

    (一) 问题描述 ubuntu开机只有桌面,没有菜单栏和任务栏,如下图: (二) 问题解决 刚学习ubuntu,总有些像我这样不折腾就不舒服的人,今天改了一下主题,图标什么的,重启开机后就发现!咦!我 ...