Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

DenisKP avatar DenisKP 2 Точки

Java Advanced - Задача - 12. The Matrix

Здравейте,

Имам малък проблем със задача 12. The Matrix. В Judge ми дава 55/100. Всички тестове ми излизат. Опитах и с мои тестове, но резултата е същият. Погледнах примерни решиния на колеги(бяха с рекурсия и не успях да ги разбера много много). Ще съм благодарен ако ударите едно рамо.

Моето решение: https://pastebin.com/iX1vdjbg

Условие:

You are given a matrix (2D array) of lowercase alphanumeric characters (a-z, 0-9), a starting position – defined by a start row startRow and a start column startCol – and a filling symbol fillChar. Let’s call the symbol originally at startRow and startCol the startChar. Write a program, which, starting from the symbol at startRow and startCol, changes to fillChar every symbol in the matrix which:

  • is equal to startChar AND
  • can be reached from startChar by going up (row – 1), down (row + 1), left (col – 1) and right (col + 1) and “stepping” ONLY on symbols equal startChar

So, you basically start from startRow and startCol and can move either by changing the row OR column (not both at once, i.e. you can’t go diagonally) by 1, and can only go to positions which have the startChar written on them. Once you find all those positions, you change them to fillChar.

In other words, you need to implement something like the Fill tool in MS Paint, but for a 2D char array instead of a bitmap.

Input

On the first line, two integers will be entered – the number R of rows and number C of columns.

On each of the next R lines, C characters separated by single spaces will be entered – the symbols of the Rth row of the matrix, starting from the 0th column and ending at the C-1 column.

On the next line, a single character – the fillChar – will be entered.

On the last line, two integers – startRow and startCol – separated by a single space, will be entered.

Output

The output should consist of R lines, each consisting of exactly C characters, NOT SEPARATED by spaces, representing the matrix after the fill operation has been finished.

Constraints

0 < R, C < 20
0 <= startRow < R
0 <= startCol < C

All symbols in the input matrix will be lowercase alphanumerics (a-z, 0-9). The fillChar will also be alphanumeric and lowercase.

The total running time of your program should be no more than 0.1s

The total memory allowed for use by your program is 5MB

Examples

Input

Output

5 3

a a a

a a a

a b a

a b a

a b a

x

0 0

xxx

xxx

xbx

xbx

xbx

5 3

a a a

a a a

a b a

a b a

a b a

x

2 1

aaa

aaa

axa

axa

axa

5 6

o o 1 1 o o

o 1 o o 1 o

1 o o o o 1

o 1 o o 1 o

o o 1 1 o o

3

2 1

oo11oo

o1331o

133331

o1331o

oo11oo

5 6

o o o o o o

o o o 1 o o

o o 1 o 1 1

o 1 1 w 1 o

1 o o o o o

z

4 1

oooooo

ooo1oo

oo1o11

o11w1z

1zzzzz

5 6

o 1 o o 1 o

o 1 o o 1 o

o 1 1 1 1 o

o 1 o w 1 o

o o o o o o

z

4 0

z1oo1z

z1oo1z

z1111z

z1zw1z

zzzzzz

 

Тагове:
0
Java Advanced
Balkan_Stylish avatar Balkan_Stylish 1 Точки

Здравей Денис,

Видях кода ти.

За такъв тип задачи 2 от начините за решение е BFS(търсене в ширина) или DFS(търсене в дълбочина) алгоритми,

от които BFS е за предпочитане(по-оптимално).

Написал съм и двете решения, написал съм коментари, погледни ги когато ти е удобно.

https://pastebin.com/ZU3yULpt

0
Crystal54 avatar Crystal54 0 Точки

A Matrix is a set of numbers, generally, for JEE Advanced, matrices may be referred to as 2D arrays. Some aspirants find square matrices easier to solve for.

 

FirstCallOnline

-1
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.