№1033
http://acm.timus.ru/problem.aspx?space=1&num=1033
ЖМИ ДАЛЕЕ>>>
РЕШЕНИЕ:
[PASCAL]
Var Map: Array[0..34,0..34] of LongInt;
N,I,J,R: LongInt;
Ch: Char;
Procedure Mark(X,Y:LongInt);
Begin
If Map[X,Y]=1 then begin
Map[X,Y]:=2;
If Map[X+1,Y]=1 then Mark(X+1,Y);
If Map[X-1,Y]=1 then Mark(X-1,Y);
If Map[X,Y+1]=1 then Mark(X,Y+1);
If Map[X,Y-1]=1 then Mark(X,Y-1);
End;
End;
Begin
FillChar(Map,SizeOf(Map),0);
Readln(N);
Map[0,0]:=2;
Map[1,0]:=2;
Map[0,1]:=2;
Map[N+1,N]:=2;
Map[N,N+1]:=2;
Map[N+1,N+1]:=2;
For I:=1 to N do begin
For J:=1 to N do begin
Read(Ch);
If Ch<>'#' then Map[I,J]:=1;
End;
Readln;
End;
Mark(1,1);
Mark(N,N);
R:=0;
For I:=1 to N do
For J:=1 to N do
If Map[I,J]=2 then begin
If Map[I+1,J]<>2 then Inc(R);
If Map[I-1,J]<>2 then Inc(R);
If Map[I,J+1]<>2 then Inc(R);
If Map[I,J-1]<>2 then Inc(R);
End;
Write(R*9);
End.
[C++]
#include <iostream>
#include <queue>
using namespace std;
queue <pair<int, int>> q;
int a[100][100], n, r;
char c;
pair<int, int> x;
int see (int k, int i, int j)
{
if (!((i == 0 && j == 1) || (i == 1 && j == 0) || (i == n && j == n+1) || (i == n+1 && j ==n)))
{
if (a[i][j] > 100000000)
r += 9;
else
if (a[i][j] == 0)
{
q.push(make_pair(i, j));
a[i][j] = k+1;
}
}
return 0;
}
int feel(int i, int j)
{
if (a[i][j] == 0)
{
a[i][j] = 1;
q.push(make_pair(i, j));
while (!q.empty())
{
x = q.front();
q.pop();
int k = a[x.first][x.second];
see(k, x.first+1, x.second);
see(k, x.first-1, x.second);
see(k, x.first, x.second+1);
see(k, x.first, x.second-1);
}
}
return 0;
}
int main ()
{
cin >> n;
memset(a, 127, sizeof(a));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
cin >> c;
if (c == '.')
a[i][j] = 0;
}
feel(1, 1);
feel(n, n);
cout << r;
return 0;
}
1033. Labyrinth
Ограничение времени: 1.0 секунды
Ограничение памяти: 16 МБ
Ограничение памяти: 16 МБ
Administration of the labyrinth has decided to start a new season with new wallpapers. For this purpose they need a program to calculate the square of the walls inside the labyrinth. This job is just for you!
The labyrinth is represented by a matrix N×N (3 ≤ N ≤ 33, you see, ‘3’ is a magic digit!). Some matrix cells contain a dot character (‘.’) that denotes an empty square. Other cells contain a diesis character (‘#’) that denotes a square filled by monolith block of stone wall. All squares are of the same size 3×3 meters.
The walls are constructed around the labyrinth (except for the upper left and lower right corners, which are used as entrances) and on the cells with a diesis character. No other walls are constructed. There always will be a dot character at the upper left and lower right corner cells of the input matrix.
Your task is to calculate the square of visible part of the walls inside the labyrinth. In other words, the square of the walls' surface visible to a visitor of the labyrinth. Note that there's no holes to look or to move through between any two adjacent blocks of the wall. The blocks are considered to be adjacent if they touch each other in any corner. See picture for an example: visible walls inside the labyrinth are drawn with bold lines. The height of all the walls is 3 meters.
Исходные данные
The first line of the input contains the single number N. The next N lines contain N characters each. Each line describes one row of the labyrinth matrix. In each line only dot and diesis characters will be used and each line will be finished with a new line character. There will be no spaces in the input.
Результат
Your program should print to the output a single integer — the exact value of the square of the wallpaper needed.
Пример
исходные данные | результат |
---|---|
5 ..... ...## ..#.. ..### ..... | 198 |
Автор задачи: Владимир Пинаев
Источник задачи: III командный студенческий чемпионат Урала по программированию. 1999 г.
Источник задачи: III командный студенческий чемпионат Урала по программированию. 1999 г.