我不得不吐槽:
整理下进来的ccf准备经历:
首先说下java做题的局限:花费时间多,空间多,代码偏长。另一个问题是程序的问题,就是某些题目会明确注明测试点中有30%的数据范围是0-100,50%的数据是0-10000,而100%的数据是100000.这时候,就需要复杂度为O(1)的算法。好好思考一下啊~
另附上我的程序:
import java.util.Scanner; public class Main{ static int m, n; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); m = scanner.nextInt(); n = scanner.nextInt(); int q = scanner.nextInt(); scanner.nextLine(); char[][] init = new char[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { init[i][j] = '.'; } } String action = null; for (int i = 0; i < q; i++) { action = scanner.nextLine(); String[] actionArray = action.split(" "); if (actionArray.length == 4) { int x = Integer.parseInt(actionArray[1]); int y = Integer.parseInt(actionArray[2]); char filling = actionArray[3].charAt(0); fill(init, x, y, filling); } else { int x1 = Integer.parseInt(actionArray[1]), y1 = Integer.parseInt(actionArray[2]), x2 = Integer.parseInt(actionArray[3]), y2 = Integer.parseInt(actionArray[4]); if (x1 != x2) { if (x1 > x2) { int tmp = x1; x1 = x2; x2 = tmp; } for (int j = x1; j <= x2; j++) { if (init[y1][j] == '|' || init[y1][j] == '+') init[y1][j] = '+'; else init[y1][j] = '-'; } } else { if (y1 > y2) { int tmp = y1; y1 = y2; y2 = tmp; } for (int j = y1; j <= y2; j++) { if (init[j][x1] == '-' || init[j][x1] == '+') init[j][x1] = '+'; else init[j][x1] = '|'; } } } } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < m; j++) { System.out.print(init[i][j]); } System.out.println(); } } static void fill(char[][] currentGraph, int x, int y, char filling) { currentGraph[y][x] = filling; if (notLine(x, y + 1, currentGraph, filling)){ fill(currentGraph, x, y + 1, filling); } if (notLine(x + 1, y, currentGraph, filling)) fill(currentGraph, x + 1, y, filling); if (notLine(x, y - 1, currentGraph, filling)) fill(currentGraph, x, y - 1, filling); if (notLine(x - 1, y, currentGraph, filling)) fill(currentGraph, x - 1, y, filling); } static boolean notLine(int x, int y, char[][] currentGraph, char filling) { return x >= 0 && x <= (m - 1) && y >= 0 && y <= (n - 1) && currentGraph[y][x] != '-' && currentGraph[y][x] != '|' && currentGraph[y][x] != '+' && currentGraph[y][x] != filling; } }
问题描述
用 ASCII 字符来画图是一件有趣的事情,并形成了一门被称为 ASCII Art 的艺术。例如,下图是用 ASCII 字符画出来的 CSPRO 字样。
.._._.._.._…_..
././.||...|..../._..
|.|…___.|.|).|.|).|.|.|.|
|.|.).|.._/|...<|.|_|.|
.____|_/||…||._\/.
本题要求编程实现一个用 ASCII 字符来画图的程序,支持以下两种操作:
Ÿ 画线:给出两个端点的坐标,画一条连接这两个端点的线段。简便起见题目保证要画的每条线段都是水平或者竖直的。水平线段用字符 - 来画,竖直线段用字符 | 来画。如果一条水平线段和一条竖直线段在某个位置相交,则相交位置用字符 + 代替。
Ÿ 填充:给出填充的起始位置坐标和需要填充的字符,从起始位置开始,用该字符填充相邻位置,直到遇到画布边缘或已经画好的线段。注意这里的相邻位置只需要考虑上下左右 4 个方向,如下图所示,字符 @ 只和 4 个字符 * 相邻。
.*.
@
.*.
输入格式
第1行有三个整数m, n和q。m和n分别表示画布的宽度和高度,以字符为单位。q表示画图操作的个数。
第2行至第q + 1行,每行是以下两种形式之一:
Ÿ 0 x1 y1 x2 y2:表示画线段的操作,(x1, y1)和(x2, y2)分别是线段的两端,满足要么x1 = x2 且y1 ≠ y2,要么 y1 = y2 且 x1 ≠ x2。
Ÿ 1 x y c:表示填充操作,(x, y)是起始位置,保证不会落在任何已有的线段上;c 为填充字符,是大小写字母。
画布的左下角是坐标为 (0, 0) 的位置,向右为x坐标增大的方向,向上为y坐标增大的方向。这q个操作按照数据给出的顺序依次执行。画布最初时所有位置都是字符 .(小数点)。
输出格式
输出有n行,每行m个字符,表示依次执行这q个操作后得到的画图结果。
样例输入
4 2 3
1 0 0 B
0 1 0 2 0
1 0 0 A
样例输出
AAAA
A–A
样例输入
16 13 9
0 3 1 12 1
0 12 1 12 3
0 12 3 6 3
0 6 3 6 9
0 6 9 12 9
0 12 9 12 11
0 12 11 3 11
0 3 11 3 1
1 4 2 C
样例输出
…………….
…+——–+…
…|CCCCCCCC|…
…|CC+—–+…
…|CC|………
…|CC|………
…|CC|………
…|CC|………
…|CC|………
…|CC+—–+…
…|CCCCCCCC|…
…+——–+…
…………….
评测用例规模与约定
所有的评测用例满足:2 ≤ m, n ≤ 100,0 ≤ q ≤ 100,0 ≤ x < m(x表示输入数据中所有位置的x坐标),0 ≤ y < n(y表示输入数据中所有位置的y坐标)。
仍然是模拟。
需要注意的是,题中的坐标体系和用二维数组表示的坐标体系是不同的,这需要一个映射过程。
<code class="language-c++ hljs cpp has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <iostream></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <cstdio></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <string></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <fstream></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <cstring></span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#include <algorithm></span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">using</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">namespace</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">std</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> maxn = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">105</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> pic[maxn][maxn]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> dir[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>][<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>] = { {-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>}, {<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, -<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>}, {<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>}, {<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>} }; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> n, m, q; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> print() { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < n; ++i){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> j = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; j < m; ++j) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << pic[i][j]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << endl; } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> Map(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>& x, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span>& y) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> t = x; x = n - <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span> - y; y = t; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> dfs(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> r, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> c, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> ch) { pic[r][c] = ch; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>; ++i) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> nr = r + dir[i][<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>], nc = c + dir[i][<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(nr < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> || nr >= n || nc < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> || nc >= m || pic[nr][nc] == <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'-'</span> || pic[nr][nc] == ch || pic[nr][nc] == <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'|'</span> || pic[nr][nc] == <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'+'</span>) <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">continue</span>; dfs(nr, nc, ch); } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> main() { <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#ifdef LOCAL</span> fstream <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cin</span>(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"data.in"</span>); <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#endif <span class="hljs-comment" style="box-sizing: border-box;">// LOCAL</span></span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cin</span> >> m >> n >> q; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < n; ++i){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> j = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; j < m; ++j) pic[i][j] = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'.'</span>; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span>(q--){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> op, x1, y1, x2, y2; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">char</span> ch; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cin</span> >> op; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(op == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cin</span> >> x1 >> y1 >> x2 >> y2; Map(x1, y1); Map(x2, y2); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(x1 == x2){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(y1 > y2) swap(y1, y2); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = y1; i <= y2; ++i){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(pic[x1][i] == <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'|'</span>) pic[x1][i] = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'+'</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(pic[x1][i] != <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'+'</span>) pic[x1][i] = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'-'</span>; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//'+'不要被覆盖</span> } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(x1 > x2) swap(x1, x2); <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = x1; i <= x2; ++i){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(pic[i][y1] == <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'-'</span>) pic[i][y1] = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'+'</span>; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span>(pic[i][y1] != <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'+'</span>) pic[i][y1] = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'|'</span>; } } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cin</span> >> x1 >> y1 >> ch; Map(x1, y1); dfs(x1, y1, ch); } } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < n; ++i){ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> j = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; j < m; ++j) <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << pic[i][j]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">cout</span> << endl; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; }</code>