-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem118B.java
122 lines (111 loc) · 2.54 KB
/
Problem118B.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package CodeForces;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Problem118B
{
public static void main(String[] args) throws IOException
{
class Coordinate
{
public int x;
public int y;
public int value;
public Coordinate(int x, int y, int value)
{
this.x = x;
this.y = y;
this.value = value;
}
}
fastinput.init(System.in);
int n = fastinput.nextInt();
char[][] handkerchief = new char[(2*n)+1][(2*n)+1];
Queue<Coordinate> q1 = new LinkedList<Coordinate>();
Coordinate start = new Coordinate(n, n, n);
q1.add(start);
for (int i = 0; i < (2*n)+1; i++)
{
for (int j = 0; j < (2*n) +1; j++)
{
handkerchief[i][j] = ' ';
}
}
while (!q1.isEmpty())
{
Coordinate current = q1.poll();
handkerchief[current.y][current.x] = (char) (48 + current.value);
if (current.value > 0)
{
if (current.y - 1 >= 0 && handkerchief[current.y-1][current.x]== ' ')
{
q1.add(new Coordinate(current.y-1, current.x, current.value-1));
}
if (current.y + 1 < (2*n)+1 && handkerchief[current.y+1][current.x]== ' ')
{
q1.add(new Coordinate(current.y+1, current.x, current.value-1));
}
if (current.x - 1 >= 0 && handkerchief[current.y][current.x-1]== ' ')
{
q1.add(new Coordinate(current.y, current.x-1, current.value-1));
}
if (current.x + 1 < (2*n)+1 && handkerchief[current.y][current.x+1]== ' ')
{
q1.add(new Coordinate(current.y, current.x+1, current.value-1));
}
}
}
int limit;
int antilimit = 1;
for (int i = 0; i < (2*n)+1; i++)
{
if (i <= n)
{
limit = n+i;
}
else
{
limit = 2*n - antilimit++;
}
for (int j = 0; j <= limit; j++)
{
System.out.print(handkerchief[i][j]);
if (j +1 <= limit)
{
System.out.print(" ");
}
}
System.out.println("");
}
}
public static class fastinput
{
static BufferedReader bf;
static StringTokenizer st;
static void init(InputStream input)
{
bf = new BufferedReader(new InputStreamReader(input));
st = new StringTokenizer("");
}
static String nextline() throws IOException
{
return bf.readLine();
}
static String next() throws IOException
{
while (!st.hasMoreTokens())
{
st = new StringTokenizer(bf.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException
{
return Integer.parseInt(next());
}
}
}