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, TT 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. 1
  2. 4 4
  3. 0101
  4. 0010
  5. 0100
  6. 1111
  7. 7
  8. Q
  9. M 0 2 1
  10. Q
  11. M 2 2 0
  12. Q
  13. M 2 1 0
  14. Q
  1. Case #1:
  2. 4
  3. 2
  4. 2
  5. 2
  1. import java.io.FileNotFoundException;
  2. import java.io.PrintWriter;
  3. import java.io.Writer;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class Q1 {
  8.  
  9. /*
  10. *
  11. 1
  12. 4 4
  13. 0101
  14. 0010
  15. 0100
  16. 1111
  17. 7
  18. Q
  19. M 0 2 1
  20. Q
  21. M 2 2 0
  22. Q
  23. M 2 1 0
  24. Q
  25.  
  26. 1
  27. 4 4
  28. 0101
  29. 1010
  30. 0101
  31. 1111
  32. 1
  33. Q
  34. */
  35.  
  36. static int a[][];
  37. static int mark[][];
  38. static void markA(int x, int y){
  39. if(x>=a.length || x<0 || y<0 || y>=a[0].length || a[x][y]==0 ||mark[x][y]==1){
  40. return;
  41. }
  42.  
  43. if(a[x][y]==1){
  44. mark[x][y]=1;
  45. }
  46. //You
  47. if(y<a[0].length-1 ){
  48. markA(x, y+1);
  49. }
  50. //zuo
  51. if(y>0){
  52. markA(x, y-1);
  53. }
  54. //xia
  55. if(x<a.length-1){
  56. markA(x+1, y);
  57. }
  58.  
  59. if(x>0){
  60. markA(x-1, y);
  61. }
  62. }
  63. static int query(){
  64. int count=0;
  65. for(int i=0; i<a.length; i++){
  66. for(int j=0; j<a[0].length; j++){
  67.  
  68. if(mark[i][j]==0 && a[i][j]==1){
  69. count++;
  70. // System.out.println(i +":: "+j);
  71.  
  72. // System.out.println(count);
  73. markA(i, j);
  74. // System.out.println(Arrays.toString(mark[0]));
  75. // System.out.println(Arrays.toString(mark[1]));
  76. // System.out.println(Arrays.toString(mark[2]));
  77. // System.out.println(Arrays.toString(mark[3]));
  78. }
  79. }
  80. }
  81. return count;
  82. }
  83. public static void main(String[] args) throws FileNotFoundException {
  84. // TODO Auto-generated method stub
  85. Scanner scanner = new Scanner(System.in);
  86. PrintWriter writer = new PrintWriter("out.txt");
  87. int t=scanner.nextInt();
  88. int c,r;
  89. int m;
  90. //scanner.nextLine();
  91. for(int ttt=0; ttt<t;++ttt){
  92. c=scanner.nextInt();
  93. r=scanner.nextInt();
  94. scanner.nextLine();
  95.  
  96. a=new int[c][r];
  97. mark= new int[c][r];
  98. for(int i=0; i<c; i++){
  99.  
  100. String line =scanner.nextLine();
  101.  
  102. for(int j=0; j<r; j++){
  103. a[i][j]=line.charAt(j)-'0';
  104. }
  105. }
  106. writer.println("Case #"+(ttt+1)+":");
  107. m=scanner.nextInt();
  108. for(int i=0; i<m;i++){
  109. String x=scanner.next();
  110. int x1,y1,value;
  111. if(x.equals("Q")){
  112. writer.println(query());
  113. mark=new int[c][r];
  114. }else if(x.equals("M")){
  115. x1=scanner.nextInt();
  116. y1=scanner.nextInt();
  117. value = scanner.nextInt();
  118. a[x1][y1]=value;
  119. mark=new int[c][r];
  120. }
  121. }
  122. }
  123.  
  124. //int result=query();
  125. //System.out.println(result);
  126. scanner.close();
  127. writer.close();
  128. }
  129.  
  130. }

Problem A. Dynamic Grid的更多相关文章

  1. [Algorithm] Meeting hour optimization (Kanpsack problem) and Dynamic programming

    For example we have array of meeting objects: const data = [ { name: }, { name: }, { name: }, { name ...

  2. HDU - 6321 Problem C. Dynamic Graph Matching (状压dp)

    题意:给定一个N个点的零图,M次操作,添加或删除一条边,每一次操作以后,打印用1,2,...N/2条边构成的匹配数. 分析:因为N的范围很小,所以可以把点的枚举状态用二进制表示集合.用一维数组dp[S ...

  3. Codeforces 1503C Travelling Salesman Problem(Dynamic Programming)

    题意 大家都是优秀生,这点英文还是看得懂的:点此看题 题解 由于旅行路线成一个环,所以从哪里出发不重要,我们把景点按照 a i a_i ai​ 排序,不妨就从左边最小的出发.基础的旅行费用 c i c ...

  4. hdu 4223 Dynamic Programming?

    Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  5. HDU 6321 Dynamic Graph Matching

    HDU 6321 Dynamic Graph Matching (状压DP) Problem C. Dynamic Graph Matching Time Limit: 8000/4000 MS (J ...

  6. hdu多校第3场C. Dynamic Graph Matching

    Problem C. Dynamic Graph Matching Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Tot ...

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

  8. RAC的QA

    RAC: Frequently Asked Questions [ID 220970.1]   修改时间 13-JAN-2011     类型 FAQ     状态 PUBLISHED   Appli ...

  9. Leetcode: climbing stairs

    July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...

随机推荐

  1. [Beijing2010组队]次小生成树Tree

    小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正当小C洋洋得意之时,小P又来泼小C冷水了.小P说,让小C求出一个无向图的次小生成树,而且这个次小生成树还得是严格次小 ...

  2. [POI2014]Around the world

    题目大意: 一个环上有$n(n\le10^6)$个点,每个点之间的距离为$l_i(l_i\le10^9)$.有$m(m\le100)$架飞机,每架飞机单次最大航行距离为$d_i$.飞机只能在点上起飞. ...

  3. Java 继承问题 -- 子类是否继承父类的私有属性

    理解一: 子类会继承父类的所有属性和方法,至于能不能直接访问,那就是访问权限的问题了. 例如:父类有个private String name; 属性.子类会继承下来,但子类访问不了,因为是privat ...

  4. C#的多线程——使用async和await来完成异步编程(Asynchronous Programming with async and await)

    https://msdn.microsoft.com/zh-cn/library/mt674882.aspx 侵删 更新于:2015年6月20日 欲获得最新的Visual Studio 2017 RC ...

  5. C++中的类所占内存空间+继承中的成员访问控制

    C++学习之继承中的成员访问控制 C++中的类所占内存空间总结

  6. 【Hadoop】Hadoop MR 自定义排序

    1.概念 2.代码示例 FlowSort package com.ares.hadoop.mr.flowsort; import java.io.IOException; import org.apa ...

  7. 2017.7.7 postgreSQL在插入造成重复时执行更新

    参考来自:https://stackoverflow.com/questions/1109061/insert-on-duplicate-update-in-postgresql/1109198#11 ...

  8. Nodejs扩展,实现消息弹窗

    參考https://github.com/olalonde/node-notify的实现 模块的C++代码 node_gtknotify.cc #include <v8.h> #inclu ...

  9. Angular 学习笔记——sublime

    div.row>div.col-md-12*10 就等于 <div class="row">    <div class="col-md-12&q ...

  10. VB,Visual Basic如何修改代码文本大小和字体

    工具-选项-编辑器格式   修改之后效果如图所示