Problem A. Dynamic Grid
Problem
We have a grid with R rows and C columns in which every entry is either 0 or 1. We are going to perform N operations on the grid, each of which is one of the following:
- Operation M: Change a number in one cell of the grid to 0 or 1
- Operation Q: Determine the number of different connected regions of 1s. A connected region of 1s is a subset of cells that are all 1, in which any cell in the region can be reached from any other cell in the region by traveling between cells along edges (not corners).
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with one line with two integers, R and C, which represent the number of rows and columns in the grid. Then, there are R lines of C characters each, in which every character is either 0
or 1
. These lines represent the initial state of the grid.
The next line has one integer, N, the number of operations to perform on the grid. N more lines follow; each has one operation. All operation Ms will be of the form M x y z
, meaning that the cell at row x and column y should be changed to the value z. All operation Qs will be of the form Q
.
Output
For each test case, output one line containing "Case #x:", where x is the test case number (starting from 1). Then, for every operation Q in the test case, in order, output one line containing the number of connected regions of 1s.
Limits
1 ≤ T ≤ 10.
1 ≤ R, C ≤ 100.
0 ≤ x < R.
0 ≤ y < C.
0 ≤ z ≤ 1.
Small dataset
1 ≤ N ≤ 10.
Large dataset
1 ≤ N ≤ 1000.
Sample
Input |
Output |
1 |
Case #1: |
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.Scanner; public class Q1 { /*
*
1
4 4
0101
0010
0100
1111
7
Q
M 0 2 1
Q
M 2 2 0
Q
M 2 1 0
Q 1
4 4
0101
1010
0101
1111
1
Q
*/ static int a[][];
static int mark[][];
static void markA(int x, int y){
if(x>=a.length || x<0 || y<0 || y>=a[0].length || a[x][y]==0 ||mark[x][y]==1){
return;
} if(a[x][y]==1){
mark[x][y]=1;
}
//You
if(y<a[0].length-1 ){
markA(x, y+1);
}
//zuo
if(y>0){
markA(x, y-1);
}
//xia
if(x<a.length-1){
markA(x+1, y);
} if(x>0){
markA(x-1, y);
}
}
static int query(){
int count=0;
for(int i=0; i<a.length; i++){
for(int j=0; j<a[0].length; j++){ if(mark[i][j]==0 && a[i][j]==1){
count++;
// System.out.println(i +":: "+j); // System.out.println(count);
markA(i, j);
// System.out.println(Arrays.toString(mark[0]));
// System.out.println(Arrays.toString(mark[1]));
// System.out.println(Arrays.toString(mark[2]));
// System.out.println(Arrays.toString(mark[3]));
}
}
}
return count;
}
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
PrintWriter writer = new PrintWriter("out.txt");
int t=scanner.nextInt();
int c,r;
int m;
//scanner.nextLine();
for(int ttt=0; ttt<t;++ttt){
c=scanner.nextInt();
r=scanner.nextInt();
scanner.nextLine(); a=new int[c][r];
mark= new int[c][r];
for(int i=0; i<c; i++){ String line =scanner.nextLine(); for(int j=0; j<r; j++){
a[i][j]=line.charAt(j)-'0';
}
}
writer.println("Case #"+(ttt+1)+":");
m=scanner.nextInt();
for(int i=0; i<m;i++){
String x=scanner.next();
int x1,y1,value;
if(x.equals("Q")){
writer.println(query());
mark=new int[c][r];
}else if(x.equals("M")){
x1=scanner.nextInt();
y1=scanner.nextInt();
value = scanner.nextInt();
a[x1][y1]=value;
mark=new int[c][r];
}
}
} //int result=query();
//System.out.println(result);
scanner.close();
writer.close();
} }
Problem A. Dynamic Grid的更多相关文章
- [Algorithm] Meeting hour optimization (Kanpsack problem) and Dynamic programming
For example we have array of meeting objects: const data = [ { name: }, { name: }, { name: }, { name ...
- HDU - 6321 Problem C. Dynamic Graph Matching (状压dp)
题意:给定一个N个点的零图,M次操作,添加或删除一条边,每一次操作以后,打印用1,2,...N/2条边构成的匹配数. 分析:因为N的范围很小,所以可以把点的枚举状态用二进制表示集合.用一维数组dp[S ...
- Codeforces 1503C Travelling Salesman Problem(Dynamic Programming)
题意 大家都是优秀生,这点英文还是看得懂的:点此看题 题解 由于旅行路线成一个环,所以从哪里出发不重要,我们把景点按照 a i a_i ai 排序,不妨就从左边最小的出发.基础的旅行费用 c i c ...
- hdu 4223 Dynamic Programming?
Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 6321 Dynamic Graph Matching
HDU 6321 Dynamic Graph Matching (状压DP) Problem C. Dynamic Graph Matching Time Limit: 8000/4000 MS (J ...
- hdu多校第3场C. Dynamic Graph Matching
Problem C. Dynamic Graph Matching Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Tot ...
- Working with the Dynamic Type in C#
Working with the Dynamic Type in C# https://www.red-gate.com/simple-talk/dotnet/c-programming/workin ...
- RAC的QA
RAC: Frequently Asked Questions [ID 220970.1] 修改时间 13-JAN-2011 类型 FAQ 状态 PUBLISHED Appli ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
随机推荐
- hiho一下第131周 后缀自动机二·重复旋律8(循环相似子串)
后缀自动机五·重复旋律8 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一段音乐旋律可以被表示为一段数构成的数列. 小Hi ...
- 【strork】HAOI2017旅游记
乘着大巴踏上归程,看着赛后落寞的一些人,赛后轻松的一些人,感触颇多. [爪机被lsj坑了,照片等周末再放...] 4月22: 早上起床不用跑操,到了教室我就看见LSJ跑过来,不用想,果然:“早上nin ...
- 7.26机房报零赛——无尽的矩阵【kmp+hash】
恩,其实大家都没有报零,反正我是蒟蒻 为了纪念我第一次打过哈希,特此写一篇题解 题目描述 从前有一个的小矩阵,矩阵的每个元素是一个字母(区分大小写),突然有一天它发生了 变异,覆盖了整个二维空间,即不 ...
- [POI2006]Periods of Words
题目大意: 给定一个长度为$n(n\leq10^6)$的字符串$S$,定义一个串$S$的最大周期为一个不为$S$的字符串$Q$,满足$Q$为$S$的前缀且$S$为$QQ$的前缀.求字符串$S$的每一个 ...
- layout layout_alignLeft跟layout_toLeftOf
今天调布局的时候 想把界面做成横屏竖屏都可以的 突然发现之前理解的android:布局参数都是有问题的 今天贴出来 下次自己也记得 以下大部为用在RelativeLayout中的一些参数: andro ...
- Ubuntu 16.04使用timedatectl进行管理时间(UTC/CST)(服务器/桌面)
说明:16.04开始,systemd接管了系统之后就不再使用/etc/default/rcS和ntpdate.dpkg-reconfigure tzdata进行时间的管理,所以在这些地方设置是无效的, ...
- DoTween 部分中文文档
前言 DOTween现在还处于 alpha,所以还有一些缺失的功能(如路径插件,附加回调和其它的tween选项),这个文档在不久的将来可能会改变. 一.术语 Tweener 一个tween控制valu ...
- SpringBoot 框架整合webservice
spring boot集成web service框架 题记: 本篇博客讲的spring boot如何集成 spring web service,如果您想用Apache CXF集成,那么可能不适合您.为 ...
- ubuntu 安装 regex模块时 fatal error: Python.h: No such file or directory
原因是 python-dev包没有安装 根据Py2还是py3 sudo apt-get install python-dev 或者 sudo apt-get install python3-dev 安 ...
- JS中的柯里化及精巧的自动柯里化实现
一.什么是柯里化? 在计算机科学中,柯里化(Currying)是把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数且返回结果的新函数的技术.这个技术由 C ...