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.
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 4 4 0101 0010 0100 1111 7 Q M 0 2 1 Q M 2 2 0 Q M 2 1 0 Q |
Case #1: 4 2 2 2 |
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(); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器