There is a game with an N*N matrix, every element is a character or a digit. The object is to delete the most elements. Removing an element: select an element which has a neighbor vertical or horizontal but not diagonal. The neighbor must be the same character as the selected element. You delete both the selected element and all of it's neighbors (mustn't delete neighbors' neighbors). After the removal, the elements above the deleted elements in a column goes down, and if a column is empty, the columns from the left move to the right to fill the blank column. It's required to write a program which deletes the maximum possible elements. Input: The input file contains more test. Every test has in the 1st line an integer N (2<=N<=5). The next N rows contains N elements. The file ends with a line containing 0. Output: The output file contains for every test a line, the number of elements remained in the matrix after deleting the maximum possible elements. |
3 aab aab bcc 3 aba bbb aba 3 abd bbb cbe 4 1aa2 b21b b12b 2bb1 4 aaaa bbbb bbcb bbbb 4 acbg aceg bdfh bdfh 4 edbd faae gaae dbcc 4 aacc aacc bbdd bbdd 5 adfhk adfhk bexil begil ccgjj 5 edbdf adbde caaea baaec ggccb 5 aaaaa abaca acaba abaca aaaaa 0 |
0 0 4 0 1 1 3 2 1 1 1 |
1 0 4 1 1 2 8 0 1 9 6 |
|
|
aab aab bcc |
a-- aab bab |
-a- -ab -bb |
--- --b -bb |
--- --- --- |
· You can delete a neighbor only if it's the same character as the selected character (whose neighbor it is) · You can only delete the neighbors, can't delete the neighbor's neighbors. So i have to rewrite this code. · I translated the exercise from Romanian (neither are my native language), so I've probably made some mistakes. |
aab aab bcc |
..b .ab bcc |
... .a. bcc |
... ... ... |
aaaaa abaca acaba abaca aaaaa |
aaaaa ..aaa .baca .aaca ..aba -> .baba .baca abaca aaaaa aaaaa |
aba a-a cbf c-f ada ada |
aab aab bcc |
aab a.. aab -> aab b.. bab |
... .ab bab |
... ... ..b -> ..b b.b .bb |
... ... ... |
|
|
----- aab -aab- aab -> -aab- bcc -bcc- ----- |
a
to 7x7