Editorial for Tiles


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.

Submitting an official solution before solving the problem yourself is a bannable offence.
Analiz ve Çözüm Yolu

Bu problemi recursive ve brute force kullanarak çözmeyi deneyelim .Önce ilk satırdan son satıra, daha sonra da ilk sütundan son sütuna olmak üzere tüm kare bölgeler üzerinde gezeceğiz. Rastladığımız her boş bölge için hangi fayans boyutunu kullanacağımıza karar vereceğiz ve tüm olası seçenekleri gözden geçireceğiz. Her uygun bölge için fayansı sol üste köşesinden kaplanmış olarak yerleştireceğiz.

Bu algoritma küçük test durumları için uygun ama daha büyük kapsamlı durumlarda zaman limitini aşacaktır. Bunu optimize etmek için bir "hatırlama" tekniği kullanılabilir. Bu yöntemde ana fikrimiz farklı kaplamalar aynı kaplanmış bölgeleri oluşturuyorsa, bir tanesini (en az fayansın kullanıldığı) saymanın yeterli olacağıdır. Hatırlama tekniğini uygulamak için dinamik programlama yaklaşımı kullanılabilir. Algoritmamızın durumu o anki bölge artı üstünden geçilen bölgelerin bir seti, algoritmanın değeri ise bu üstünden geçilen bölgeler setine ulaşmak için kullanılacak en az fayans sayısı. Tüm durumları anlık bölgelerini göz önüne alarak değerlendireceğiz. Tüm fayansların kare şeklinde olması ve oluşabilecek durumların nispeten az olması sayesinde zaman limitine sığabiliriz.


Analysis and Guide for Solution

Let's try to solve this problem using a brute force recursive approach. We will iterate over all square sections from the first to the last row then from the first to the last column. Every time we find an empty section we have to decide what tile size to use. We will consider all possible options. For each appropriate tile size we put the tile such that the current section is covered by its top left corner.

This algorithm works well for small test cases but does not fit into the time limit for larger ones. To optimize it a memorization technique can be used. The key idea is that if multiple different partial tilings lead to the same set of covered sections it's enough to consider just one of them (with the smallest number of tiles have been used). To implement memorization dynamic programming approach can be used. The state of our algorithm is a current section plus a set of covered sections and the value is the minimal number of tiles used to get this set of covered sections. We consider all the states in order of their current section. Due to the fact that all tiles are of square shape the total number of reachable states is relatively small and allows us to fit into the time limit.