+
),
'a'
std::cout << "Prime numbers in {2,...,999}:\n";
%
!"
(
)char
("
'
int
!
*
*
)
int 2unsigned
- 1
+
'
3)
(
"
1(
4
'a'/'b' #
6/
'
7896:
.
.
.
:
89;<
7
=:
.
.
.
:
9>><
*
.
/.
*
.
/.'\n'
*
(
0
1"
)char " ' 'a';
"
&
1"
char
)
)char
+
%? ?
("
→7
=:
.
.
.
:
89;<
%? ?
for (char c = 'a'; c <= 'z'; ++c)
(
- 1
char → int / unsigned int
1 (
"
5 * * (
-
%
) char
"
" *
+
!
char c = 'a'
&
#$
'
)char
std::cout << c;
*
abcdefghijklmnopqrstuvwxyz
→7
=:
.
.
.
:
89;<
'A','B',...,'Z'
→ @>:@@:
.
.
.
:
A=
'A','B',...,'Z'
→ @>:@@:
.
.
.
:
A=
'a','b',...,'z'
→ A;:A6:
.
.
.
:
899
'a','b',...,'z'
→ A;:A6:
.
.
.
:
899
%
),
"
*
"
)char
char text[] = {'b', 'o', 'o', 'l'}
(
"
&
, B:
!
)
),
"
-
%
(
char text[] = "bool"
%
E
1
!
!
&
,
D
"
%
0 - ""
- *
"
D
"
"
"
Gallia est omnis divisa in partes tres
)char
E
"
"
char text[] = {'b', 'o', 'o', 'l'}
-
E
1
&
(
>:
)
C
"C
1
&
,
!
"
"
*
'\0'
,
!
"
Gallia est omnis divisa in partes tres
visa
%
"
0 - ""
- *
"
D
"
"
!
0 - ""
- *
"
D
"
*
"
Gallia est omnis divisa in partes tres
"
E
1
0 - ""
- *
"
D
"
"
!
"
"
Gallia est omnis divisa in partes tres
(
visa
visa
visa
D
%
"
%
#include<iostream>
int main ()
{
// search string
char s[] = "bool";
"
%
#include<iostream>
E
( 1
!
"
"
+
" %- )
int main ()
{
// search string
char s[] = "bool";
"
#include<iostream>
F
E
:
,
s
G
int main ()
{
// search string
char s[] = "bool";
"
a
l
l
Gallia
i
// determine search string length m
unsigned int m = 0;
for (char* p = s; *p != '\0'; ++p) ++m;
// determine search string length m
unsigned int m = 0;
for (char* p = s; *p != '\0'; ++p) ++m;
// determine search string length m
unsigned int m = 0;
for (char* p = s; *p != '\0'; ++p) ++m;
// cyclic text window of size m
char* t = new char[m];
// cyclic text window of size m
char* t = new char[m];
// cyclic text window of size m
char* t = new char[m];
unsigned int w = 0; // number of characters read so far
unsigned int i = 0; // index where t logically starts
...
unsigned int w = 0; // number of characters read so far
unsigned int i = 0; // index where t logically starts
...
unsigned int w = 0; // number of characters read so far
unsigned int i = 0; // index where t logically starts
...
%
"
%
#include<iostream>
int main ()
{
// search string
char s[] = "bool";
%
"
#include<iostream>
i
a
i
l
l
Gallia
int main ()
{
// search string
char s[] = "bool";
i
a
l
i
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
l
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
Gallia
// determine search string length m
unsigned int m = 0;
for (char* p = s; *p != '\0'; ++p) ++m;
// determine search string length m
unsigned int m = 0;
for (char* p = s; *p != '\0'; ++p) ++m;
// cyclic text window of size m
char* t = new char[m];
// cyclic text window of size m
char* t = new char[m];
unsigned int w = 0; // number of characters read so far
unsigned int i = 0; // index where t logically starts
...
unsigned int w = 0; // number of characters read so far
unsigned int i = 0; // index where t logically starts
...
"
std::cout << "\n";
delete[] t;
return 0;
}
& *
" 4
%
"
%
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
i
v
i
s
a
j
i
s
a
j
v
i
s
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
a
j
std::cout << "\n";
delete[] t;
return 0;
w == 0
}
v
i
s
a
j
w == 0
%
"
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
i
i
std::cout << "\n";
delete[] t;
return 0;
w == 0
%
G
G
}
"
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
std::cout << "\n";
delete[] t;
return 0;
}
v
}
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
"
i
std::cout << "\n";
delete[] t;
return 0;
w == 0
%
(
"
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
}
C
%
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
std::cout << "\n";
delete[] t;
return 0;
1
bool
"
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
G
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
i
v
"
i
s
a
j
std::cout << "\n";
delete[] t;
return 0;
w == 1
}
G
i
v
i
s
a
j
w == 1
%
"
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
%
G
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
a
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
i
v
i
s
a
j
std::cout << "\n";
delete[] t;
return 0;
%
G
a
v
i
s
j
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
a
l
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
(i+j)%m
std::cout << "\n";
delete[] t;
return 0;
i
v
i
s
a
j
std::cout << "\n";
delete[] t;
return 0;
w == 4
}
l
l
i
s
a
i
v
j
w == 4
%
"
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
l
a
std::cout << "\n";
delete[] t;
return 0;
w == 3
%
G
G
}
"
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
a
}
%
"
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
l
i
std::cout << "\n";
delete[] t;
return 0;
w == 2
}
}
"
i
a
l
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
l
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
i
v
i
s
"
a
j
std::cout << "\n";
delete[] t;
return 0;
w == 5
}
i
s
a
v
i
v
i
s
a
j
w == 23
%
"
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
%
i
s
a
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
i
i
s
a
j
std::cout << "\n";
delete[] t;
return 0;
%
i
s
a
v
i
s
j
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
std::cout << "\n";
delete[] t;
return 0;
s
a
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
(i+j)%m
i
v
i
s
a
j
std::cout << "\n";
delete[] t;
return 0;
w == 23
}
a
v
(i+j)%m
i
v
i
s
a
j
w == 23
%
"
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
v
s
std::cout << "\n";
delete[] t;
return 0;
w == 23
%
i
i
}
"
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
a
}
%
"
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
v
i
std::cout << "\n";
delete[] t;
return 0;
w == 23
}
}
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
v
(i+j)%m
v
"
i
s
a
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
v
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
(i+j)%m
i
v
i
s
"
a
j
std::cout << "\n";
delete[] t;
return 0;
w == 23
}
i
s
a
v
(i+j)%m
i
v
i
s
a
j
w == 23
%
"
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
(
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
%
i
s
a
...
// find pattern in the text being read from std::cin
std::cin >> std::noskipws; // don't skip whitespaces!
v
for (unsigned int j = 0; j < m;)
// compare search string with window at j-th element
if (w < m || s[j] != t[(i+j)%m])
// input text still too short, or mismatch:
// advance window by replacing first character
if (std::cin >> t[i]) {
std::cout << t[i];
++w;
// one more character read
j = 0;
// restart with first characters
i = (i+1)%m; // of string and window
} else break;
// no more characters in the input
else ++j; // match: go to next character
(i+j)%m
i
v
std::cout << "\n";
delete[] t;
return 0;
i
s
a
j
std::cout << "\n";
delete[] t;
return 0;
w == 23
}
"
D+
%
i
s
a
((
v
"
"" *
.
/.
./string_matching < eratosthenes.C
(i+j)%m
i
C
1
+
v
i
s
""
:
H" -
C
a
j
9G
w == 23
*
}
%
((
"
"" *
.
/.
./string_matching < eratosthenes.C
// Program: eratosthenes.C
%
((
%
"
((
"" *
.
/.
./string_matching < eratosthenes.C >
match.out
// Calculate prime numbers in {2,...,999} using
// Eratosthenes' sieve.
#include <iostream>
int main()
{
// definition and initialization: provides us with
// Booleans crossed_out[0],..., crossed_out[999]
bool
/
"
+
H"
(
:
"
"" *
.
/.
./string_matching < eratosthenes.C >
match.out
+
1
:
E
-
""
"
""
"
I
"
E
E
"
E
1
*" %)
* :
.
.
.
"
E
1
1
1
:
int a[2][3]
(
" %)
a[0][0]
a[0][1]
int a[2][3]
a
*
1
" *
C"
a[0][2]
a[1][0]
"
E
1
a[
1
:
"
a[0]
E
"
a[1][1]
J
&
, G
)int
"
)(E
:
.
.
.
:
=
a[1][2]
9
=
a[0][0]
a[0][1]
a[0][2]
8
a[1][0]
a[1][1]
a[1][2]
a[1]
E
"
"
.
.
.
int a[2][3] =
)8]...[ )-]
int a[][3] =
+"
{
{
-
{2,4,6}, {1,3,5}
-
8
4-D
a
)8 C "
:
J
1
"
)9
C"
:1
J
"
)GC "
:
.
.
.
{2,4,6}, {1,3,5}
}
2
4
a[0]
}
6
1
3
a[1]
5
2
4
a[0]
6
1
3
a[1]
5
(
'
"
(
"
4
#
4
"
'
"
"
(
4
#
4
'
"
"
"
4
#
4
"
int a[2][3];
int a[2][3];
int a[2][3];
int (*p)[3] = a; // Zeiger auf erstes Element
int (*p)[3] = a; // Zeiger auf erstes Element
int (*p)[3] = a; // Zeiger auf erstes Element
p
p + 1
p + 2
") *
)
(
p
a[0][0]
a[0][1]
a[0][2]
a[1][0]
a[1][1]
*p
1"
( int[3]
3
) int[3],
""
1 G
p
( int
int *p [3]; // int* p[3]
a[1][2]
a[0]
(
'
"
"
4
#
int a[2][3];
4
"
"
)
%-
""
"
!K p
/
1 G
D
'
- ""
" 1
"
+"
'
"
#
8
"8
"
"
+"
=
int** a = new int*[n];
"
#
=
8
"8
a[0]
=
a[1]
8
(
( int
C"
8
1
( int
int *p% [3]; :// int* p[3]
%
- ""
" 1
a
=
!"
(
int (*p)[3] = a; // Zeiger auf erstes Element
3
1
1
*
-
8
) int*
a[n-1]
8
1
'
1
- ""
" 1
"
+"
"
'
#
1
- ""
" 1
a
for (int i = 0; i < n; ++i)
"8
8
=
=
a[1]
8
for (int i = 0; i < n; ++i)
L
=M
8
a[n-1]
a[1]
8
for (int i = 0; i < n; ++i)
L
=M
8
a[n-1]
"
'
#
=
8
"8
"
+"
=
a[1]
8
for (int i = 0; i < n; ++i)
'
"
C"
1 " intN
a[n-1]
i == 1
- ""
" 1
#
8
"8
'
a[0]
=
a[1]
8
8
(
8
"
+"
=
(
(
a[i][j]
a[i] = new int[m];
(
8
"
#
a
=
int** a = new int*[n];
a[i] = new int[m];
=
a[1]
i == 1
a
a[0]
"8
8
a[0]
1
- ""
" 1
a
int** a = new int*[n];
#
a[n-1]
i == 0
"
+"
"
(
C"
1 " intN
1
- ""
" 1
=
int** a = new int*[n];
=
(
C"
1 " intN
1
"
+"
a[i] = new int[m];
i == 0
L
8M
"8
8
a[0]
a[i] = new int[m];
for (int i = 0; i < n; ++i)
- ""
" 1
#
a
int** a = new int*[n];
a[0]
a[i] = new int[m];
'
'
"
a
=
int** a = new int*[n];
"
+"
8
J
"8
a[0]
=
a[1]
8
*(a[i]+j)
L 8M
(
C"
1 " intN
a[i]
a[n-1]
i == n-1
8
a[n-1]
a[i]+j
8
D
QP
5
SR
6
SR
7
SR
4
QP
3
5
SR
4
6
SR
5
6
QP
QP
i
22
9
YX
8
WV
7
WV
10
WV
11
9
UT
10
8
UT
9
YX
UT
WV
WV
WV
YX
-
R
'
98
&
,
' ,
)"
D
98
5
4
3
6
5
4
7
6
5
6
9
10
11
8
9
10
7
8
9
i
22
¥¤
¥¤
¥¤
¥¤
¥¤
£¢
£¢
T
¥¤
¥¤
¥¤
£¢
£¢
£¢
¡ ¡ ¡
©¨
©¨
©¨
©¨
©¨
©¨
©¨
©¨
©¨
§¦
§¦
§¦
§¦
§¦
§¦
§¦
§¦
§¦
32
32
32
32
32
32
32
32
32
54
54
54
54
54
10
10
54
10
10
10
10
76
76
76
/.
/.
/.
54
54
54
10
10
10
76
76
76
/.
/.
/.
76
76
76
/.
/.
/.
98
98
98
+*
+*
+*
98
98
98
+*
+*
+*
98
98
98
+*
+*
+*
)(
" '
?>
?>
?>
?>
=<
?>
=<
=<
=<
=<
=<
?>
;:
;:
;:
)(
)(
;:
;:
;:
)(
)(
)(
)(
)(
)(
?>
?>
?>
=<
=<
=<
;:
;:
;:
O
¥¤
£¢
£¢
£¢
¡ £¢
¡ ¡
¡ ¡ ¡
=
E '
4*
'
F
1 %
:
(
*D
'
&
'
&
'
&
'
&
'
&
'
&
!
!
!
'
&
'
&
'
&
CB
CB
CB
#"
#"
#"
%$
%$
CB
CB
CB
#"
#"
#"
%$
%$
%$
CB
CB
CB
#"
#"
#"
%$
%$
-,
-,
-,
%$
-,
-,
-,
%$
-,
-,
!
!
!
-,
!
!
!
A@
A@
A@
A@
A@
A@
&
)
F
S
R
'
98
)
F
%
19
18
0
17
18
17
16
15
%
14
18
13
19
17
18
16
17
15
16
14
D
"
~
"
19
~
~
*
}|
C
}|
~
)
F
}|
~
~
T
{z
~
}|
~
%
{z
}|
~
}|
(
{z
gf
A@
A@
A@
-O
yx
gf
P"Q
yx
{z
}|
on
20
'
yx
}|
}|
{z
on
4*
wv
)
21 20 19 20
F
4
*
22 21 20 21
&
,
23 22 21 22
wv
gf
gf
12
15
wv
k
gf
11
{z
k
gf
13
{z
k
j
k
12
yx
ml
j
k
j
k
12
yx
ml
ml
gf
ih
ih
11
yx
ml
ml
gf
gf
k
j
j
ih
ih
ih
qp
qp
qp
10
wv
on
ml
on
j
k
j
k
ih
ih
ih
qp
qp
qp
ih
10
wv
10
9
9
wv
on
11
8
{z
on
sr
ml
2
ml
3
ml
4
on
1
on
2
on
3
sr
0
j
1
j
2
7
{z
19
"
"
6
yx
18
(
'
R
"
J
5
'
yx
17
4*
yx
16
4
O
wv
15
3
qp
-O
wv
14
3
"
sr
13
+
sr
18
sr
19
17
ut
18
16
ut
17
15
&,
ut
16
14
qp
D
qp
"
sr
a`
a`
a`
_^
"
sr
18
a`
17
a`
_^
C
ut
a`
a`
_^
_^
4*
'
F
1 %
:
(
*D
ut
19
a`
a`
cb
*
_^
]\
_^
a[n-1]
)
F
ut
20
_^
]\
_^
cb
cb
%
sr
]\
_^
cb
cb
cb
a[1]
sr
[Z
]\
cb
cb
cb
ed
ed
ed
S
ut
[Z
]\
ed
ed
ed
ed
ed
a[0]
ut
[Z
]\
[Z
]\
a
ut
YX
[Z
]\
[Z
"O )
wv
YX
)
21 20 19 20
F
4
*
22 21 20 21
&
,
23 22 21 22
UT
YX
[Z
15
UT
[Z
[Z
YX
ed
12
]\
11
10
UT
ED
ED
ED
GF
13
YX
ED
12
KJ
11
YX
ED
GF
GF
12
KJ
I
H
ED
11
ML
I
H
ED
H
I
10
ML
I
H
H
I
10
WV
H
I
9
9
ML
KJ
KJ
8
WV
KJ
ON
7
'
WV
KJ
ML
ED
GF
ED
GF
H
I
4*
UT
ML
H
I
ON
%)
UT
QP
ML
QP
I
H
2
SR
KJ
3
KJ
4
KJ
1
ML
2
ML
3
ML
0
QP
1
QP
2
QP
GF
6
SR
GF
GF
ON
ON
ON
5
GF
4
SR
"
"
R
"
J
UT
&
,
3
ON
&,
SR
(
'
ON
3
"
ON
+
ON
1
4*
'
P"Q
(
T
)
F
"
,
,
=
-------X---(
S8
8
'
32
32
32
32
32
32
UT
UT
UT
UT
UT
UT
54
54
54
32
32
54
54
54
10
10
32
10
10
10
10
UT
UT
UT
WV
WV
WV
WV
WV
SR
SR
WV
SR
SR
SR
SR
76
76
76
/.
/.
/.
54
54
54
10
10
10
76
76
76
/.
/.
/.
YX
YX
YX
QP
QP
QP
WV
WV
WV
SR
SR
SR
YX
YX
YX
QP
QP
QP
YX
YX
YX
QP
QP
QP
76
76
76
/.
/.
/.
98
98
98
+*
+*
+*
98
98
98
+*
+*
+*
=<
=<
;:
=<
=<
;:
;:
;:
)(
98
98
98
+*
?>
?>
=<
?>
?>
?>
=<
?>
;:
;:
)(
)(
)(
+*
)(
)(
+*
ML
)(
KJ
?>
?>
?>
=<
=<
=<
;:
;:
;:
)(
)(
'
'
&
'
&
'
&
'
&
&
'
&
[Z
[Z
[Z
ML
ML
ML
[Z
[Z
[Z
ML
ML
ML
[Z
[Z
[Z
ML
ML
a`
a`
a`
a`
_^
_^
a`
_^
_^
a`
_^
]\
_^
]\
]\
KJ
KJ
KJ
]\
]\
]\
KJ
KJ
KJ
a`
a`
a`
_^
_^
_^
]\
]\
]\
KJ
KJ
I
H
I
H
H
I
H
I
I
H
I
H
I
H
I
H
I
H
'
&
'
CB
CB
CB
#"
CB
CB
CB
#"
#"
#"
#"
#"
&
'
%$
%$
%$
%$
%$
-,
-,
-,
%$
-,
-,
!
!
!
-,
!
!
!
&
ed
ed
ed
ED
ed
ed
ed
ED
ED
ED
ED
ED
GF
GF
GF
GF
GF
ON
ON
ON
GF
ON
ON
ON
ed
ed
ed
ED
ED
ED
GF
GF
GF
ON
ON
ON
cb
cb
cb
A@
A@
A@
CB
CB
CB
#"
#"
#"
%$
%$
%$
-,
-,
-,
!
!
!
cb
cb
cb
A@
A@
A@
cb
cb
cb
A@
A@
A@
,
E -
" '
G
4*
"
=
f
f
g
g
'
',
// (n+2) x (m+2) to hold the floor plus extra walls around
int** floor = new int*[n+2];
// dynamically allocate twodimensional array of dimensions
int m; std::cin >> m; // number of columns
8
int n; std::cin >> n; // number of rows
~
~
=
floor[r] = new int[m+2];
"
for (int r=0; r<n+2; ++r)
+
'
+"
*
,
""
~
(
4
'
|
}
~
~
f
f
f
g
h
i
C
3
|
}
~
~
f
f
f
h
4*
H" 5
|
}
g
g
f
h
+
,
2
T
'
1
3
z
{
~
~
g
g
g
i
g
i
""
8
0
2
z
{
|
}
|
}
k
j
k
j
"
T
1
3
z
|
}
|
}
j
k
k
j
k
k
j
k
H" 5
2
---X---XXX-
x
|
}
|
}
l
l
m
m
l
l
m
m
j
j
k
j
j
k
2
3
{
y
z
{
z
{
l
l
m
m
l
m
l
m
'
,
1
%
x
y
z
{
z
{
n
n
o
o
l
m
'
2
9
x
z
z
n
n
o
o
n
o
=
0
,
v
x
x
n
n
o
o
n
o
T
1
" '
y
w
{
{
y
y
r
r
s
s
n
o
2
9
v
x
y
x
y
r
r
s
s
r
s
h
i
h
i
p
q
p
q
p
q
%
v
x
y
x
y
v
w
v
w
r
r
s
s
t
t
u
u
r
s
h
i
i
h
p
8
,
w
w
v
w
v
w
v
w
t
t
u
u
t
t
u
r
s
q
p
q
q
h
i
i
h
p
'
---X---X-T-
,
---X---X----
,
"
---X---X---
" '
w
v
--SX-------u
t
-XXX--X----u
t
8 12
S
u
t
------X----q
p
("
p
4*
"
u
C
1
q
p
+
0
H" 5
8
E -
%
%
1
"
O
,
'
q
'
E "
,
G
T
9
""
/
" " S8
+
4*
C
'
""
O
+
4*
C
'
""
2
O
int tr = 0;
int tc = 0;
for (int r=1; r<n+1; ++r)
for (int r=1; r<n+1; ++r)
for (int c=1; c<m+1; ++c) {
for (int c=1; c<m+1; ++c) {
int tc = 0;
C
(
4
*.
/.
H" C
( +
shortest_path0.dat
for (int r=1; r<n+1; ++r)
for (int c=1; c<m+1; ++c) {
char entry = '-';
char entry = '-';
std::cin >> entry;
std::cin >> entry;
std::cin >> entry;
if
if
if
(entry == 'S') floor[r][c] = 0;
(entry == 'S') floor[r][c] = 0;
4*
C
'
""
O
(entry == 'S') floor[r][c] = 0;
else if (entry == 'X') floor[r][c] = -2;
else if (entry == '-') floor[r][c] = -1;
else if (entry == '-') floor[r][c] = -1;
}
+
}
+
4*
C
'
""
+
4*
C
O
&
,
'
&
,
int tr = 0;
int tc = 0;
int tc = 0;
int tc = 0;
for (int r=1; r<n+1; ++r)
for (int r=1; r<n+1; ++r)
for (int r=1; r<n+1; ++r)
char entry = '-';
}
&
,
-
int tr = 0;
for (int c=1; c<m+1; ++c) {
*
for (int c=1; c<m+1; ++c) {
char entry = '-';
char entry = '-';
std::cin >> entry;
std::cin >> entry;
std::cin >> entry;
if
if
if
(entry == 'S') floor[r][c] = 0;
""
O
&
,
int tr = 0;
for (int c=1; c<m+1; ++c) {
*
∈7
8:
.
.
.
:<
∈7
8:
.
.
.
:
"<
else if (entry == 'T') floor[tr = r][tc = c] = -1;
else if (entry == 'X') floor[r][c] = -2;
else if (entry == 'X') floor[r][c] = -2;
else if (entry == '-') floor[r][c] = -1;
}
C
char entry = '-';
else if (entry == 'T') floor[tr = r][tc = c] = -1;
else if (entry == 'T') floor[tr = r][tc = c] = -1;
""
&
,
int tr = 0;
int tc = 0;
'
&
,
int tr = 0;
-
4*
C
O
&
,
+
(entry == 'S') floor[r][c] = 0;
(entry == 'S') floor[r][c] = 0;
else if (entry == 'T') floor[tr = r][tc = c] = -1;
else if (entry == 'T') floor[tr = r][tc = c] = -1;
else if (entry == 'T') floor[tr = r][tc = c] = -1;
else if (entry == 'X') floor[r][c] = -2;
else if (entry == 'X') floor[r][c] = -2;
else if (entry == 'X') floor[r][c] = -2;
else if (entry == '-') floor[r][c] = -1;
else if (entry == '-') floor[r][c] = -1;
}
8&
,
-
else if (entry == '-') floor[r][c] = -1;
}
9&
,
1
O
4*
bool progress = false;
for (int r=1; r<n+1; ++r)
( (
"
T8:
9:
G.
.
.
4*
'
for (int i=1;; ++i) {
+
""
O
bool progress = false;
(
4
(
#
bool progress = false;
for (int r=1; r<n+1; ++r)
4
!
for (int c=1; c<m+1; ++c) {
if (floor[r][c] != -1) continue;
if (floor[r][c] != -1) continue;
if (floor[r][c] != -1) continue;
if (floor[r-1][c] == i-1 || floor[r+1][c] == i-1 ||
if (floor[r-1][c] == i-1 || floor[r+1][c] == i-1 ||
if (floor[r-1][c] == i-1 || floor[r+1][c] == i-1 ||
floor[r][c-1] == i-1 || floor[r][c+1] == i-1 ) {
floor[r][c-1] == i-1 || floor[r][c+1] == i-1 ) {
floor[r][c-1] == i-1 || floor[r][c+1] == i-1 ) {
floor[r][c] = i; // label cell with i
floor[r][c] = i; // label cell with i
floor[r][c] = i; // label cell with i
progress = true;
progress = true;
progress = true;
}
}
}
}
}
}
if (!progress) break;
if (!progress) break;
}
if (!progress) break;
}
""
for (int r=1; r<n+1; ++r)
for (int c=1; c<m+1; ++c) {
for (int i=1;; ++i) {
'
4*
−1 −1 −1 −1
−1 −1 −1 −1
−1 −1 −1 −1
−1 −1 −1 −1
−1 −1 −1 −1 −1 −1 −1
−1 −1 −1
−1 −1 −1
−1 −1 −1
−1 −1 −1
−1 −1
%$
%$
%$
%$
#"
%$
#"
%$
#"
!
#"
#"
#"
!
!
!
!
−1 −1 −1
!
−1 −1 −1
%$
%$
#"
%$
#"
!
#"
!
−1 −1 −1 −1 −1
!
−1 −1 −1
−1 −1 −1
)(
)(
)(
)(
)(
)(
−1 −1 −1 −1 −1
−1 −1 −1 −1 −1
'&
)(
'&
'&
'&
'&
'&
)(
'&
'&
)(
'&
−1 −1 −1
−1 −1
+
for (int c=1; c<m+1; ++c) {
}
""
O )
" ' ,
for (int i=1;; ++i) {
'
−1 −1 0
+
8&
,
(
else if (entry == '-') floor[r][c] = -1;
}
−1
else if (entry == 'X') floor[r][c] = -2;
(entry == 'S') floor[r][c] = 0;
else if (entry == 'T') floor[tr = r][tc = c] = -1;
std::cin >> entry;
if
S8
floor[0][c] = floor[n+1][c] = -2;
=
for (int c=0; c<m+2; ++c)
char entry = '-';
for (int c=1; c<m+1; ++c) {
%
floor[r][0] = floor[r][m+1] = -2;
for (int r=1; r<n+1; ++r)
""
−1 −1 −1 −1 −1
−1
'
" S8
=
for (int r=0; r<n+2; ++r)
int tc = 0;
int tr = 0;
&
,
O
C
4*
"
+
O * (
4
', !
""
'
4*
+
""
'
4*
+
+
4*
'
""
/
∈7
8:
.
.
.
:<
∈7
8:
.
.
.
:
"<
for (int i=1;; ++i) {
bool progress = false;
for (int r=1; r<n+1; ++r)
O
" -
for (int c=1; c<m+1; ++c) {
if (floor[r][c] != -1) continue;
+
4*
""
/
∈7
8:
.
.
.
:<
∈7
8:
.
.
.
:
"<
for (int i=1;; ++i) {
bool progress = false;
for (int r=1; r<n+1; ++r)
:
C 5
' ,
for (int c=1; c<m+1; ++c) {
if (floor[r][c] != -1) continue;
if (floor[r-1][c] == i-1 || floor[r+1][c] == i-1 ||
floor[r][c-1] == i-1 || floor[r][c+1] == i-1 ) {
for (int r=1; r<n+1; ++r)
if (floor[r][c] != -1) continue;
'
""
floor[r][c-1] == i-1 || floor[r][c+1] == i-1 ) {
progress = true;
bool progress = false;
for (int r=1; r<n+1; ++r)
+
',
+
!
B5
'
floor[r][c-1] == i-1 || floor[r][c+1] == i-1 ) {
floor[r][c] = i; // label cell with i
progress = true;
}
-
"
:
N
" (
" S8
}
8 9
"
if (!progress) break;
8
""
4*
'
( !1 "
* "%
*
+
4*
E F4 - ,
'
""
4*
'
( !1 "
* "%
int r = tr; int c = tc;
while (floor[r][c] > 0) {
int d = floor[r][c] - 1;
int d = floor[r][c] - 1;
floor[r][c] = -3;
floor[r][c] = -3;
if
if
(floor[r-1][c] == d) --r;
%
else if (floor[r][c-1] == d) --c;
else if (floor[r][c-1] == d) --c;
else
}
*
(floor[r-1][c] == d) --r;
else if (floor[r+1][c] == d) ++r;
++c; // (floor[r][c+1] == d)
%
.
.
.
else if (floor[r+1][c] == d) ++r;
else
}
E -
}
D
while (floor[r][c] > 0) {
if (floor[r-1][c] == i-1 || floor[r+1][c] == i-1 ||
if (!progress) break;
J
4*
if (floor[r][c] != -1) continue;
}
8 9
8
int r = tr; int c = tc;
for (int c=1; c<m+1; ++c) {
8 9 G
}
E F4 - ,
for (int i=1;; ++i) {
8
if (floor[r-1][c] == i-1 || floor[r+1][c] == i-1 ||
8 9 G
#
}
}
C 5
' ,
for (int c=1; c<m+1; ++c) {
8
if (!progress) break;
}
/
∈7
8:
.
.
.
:<
∈7
8:
.
.
.
:
"<
floor[r][c] = i; // label cell with i
progress = true;
}
""
bool progress = false;
}
if (!progress) break;
'
for (int i=1;; ++i) {
floor[r][c-1] == i-1 || floor[r][c+1] == i-1 ) {
progress = true;
4*
4*
floor[r][c] = i; // label cell with i
}
+
+
if (floor[r-1][c] == i-1 || floor[r+1][c] == i-1 ||
floor[r][c] = i; // label cell with i
}
'
++c; // (floor[r][c+1] == d)
+
4*
'
E F4 - ,
""
+
4*
'
( !1 "
* "%
4*
E F4 - ,
int r = tr; int c = tc;
dT "
' ,
int d = floor[r][c] - 1;
floor[r][c] = -3;
floor[r][c] = -3;
if
if
(floor[r-1][c] == d) --r;
else
4*
else if (floor[r+1][c] == d) ++r;
#"
#"
#"
#"
#"
)(
)(
else
)(
+
4*
& :
oXXX-oX-----
O )
"
d
.
.
.
'
""
( "
ooSX-oooooofor (int r=0; r<n+2; ++r)
std::cout << 'S';
---X---XXXo---X---X-oo-
std::cout << 'o';
else if (floor[r][c] == -2)
std::cout << 'X';
---X---X-o--
else
std::cout << '-';
---X---X-T--
)(
)(
)(
)(
%$
%$
%$
)(
)(
%$
%$
%$
)(
)(
)(
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
%$
*
,
++c; // (floor[r][c+1] == d)
ooooooX-----
else if (floor[r][c] == -3)
std::cout << "\n";
}
%$
%$
)(
)(
)(
)(
)(
)(
)(
)(
)(
)(
21
22
21
%$
%$
23
%$
%$
)(
)(
)(
)(
)(
)(
)(
)(
)(
)(
)(
)(
)(
−3 20
22
22
)(
20
%$
%$
)(
)(
)(
)(
)(
)(
)(
)(
)(
)(
)(
)(
)(
)(
−3 19
21
(floor[r][c] == 0)
else if (r == tr && c == tc) std::cout << 'T';
%$
%$
"'
for (int c=1; c<m+1; ++c)
%$
%$
( "4*
if
*
else if (floor[r][c-1] == d) --c;
""
for (int r=1; r<n+1; ++r) {
%$
%$
6
'
−3 −3 19
20
9
10
8
9
7
8
12
11
−3 18
)(
!
!
!
!
!
!
11
10
13
9
14
12
10
5
4
6
5
7
6
3
2
4
11
3
5
−3 −3 −3 −3 −3 −3 17
4
1
−3
2
4*
)(
)(
)(
)(
18
9
K
5
'
(floor[r-1][c] == d) --r;
}
#"
#"
#"
17
)(
16
)(
15
#"
#"
#"
19
)(
18
)(
)(
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
17
+
4*
'
( !1 "
* "%
floor[r][c] = -3;
if
++c; // (floor[r][c+1] == d)
!
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
!
!
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
16
10
3
−3 −3 0
%$
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
−3
%$
'&
15
%$
%$
'&
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
#"
'&
'&
'&
'&
'&
'&
'&
'&
'&
−3 −3 −3 −3 −3 −3
%$
%$
""
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'&
'
#"
#"
4*
""
int d = floor[r][c] - 1;
( "
'
else if (floor[r][c-1] == d) --c;
++c; // (floor[r][c+1] == d)
'
while (floor[r][c] > 0) {
G
}
+
'&
(floor[r-1][c] == d) --r;
"
else if (floor[r+1][c] == d) ++r;
else if (floor[r][c-1] == d) --c;
else
4*
int r = tr; int c = tc;
E * H
int d = floor[r][c] - 1;
}
'&
+
E F4 - ,
4*
'
( !1 "
* "%
while (floor[r][c] > 0) {
-
else if (floor[r+1][c] == d) ++r;
'&
""
int r = tr; int c = tc;
while (floor[r][c] > 0) {
%$
'
-------X----
delete[] floor[r];
(
4
"
delete[] floor;
1
S9
(
+
4*
+
:
0
5
'
"" (
4 J
(
""
"
(J
" E -
8
?
.
.
.
© Copyright 2026 Paperzz