Tugas 1 paal e agus budi raharjo 5109100164
-
Upload
budi-raharjo -
Category
Technology
-
view
84 -
download
4
Transcript of Tugas 1 paal e agus budi raharjo 5109100164
Perancangan dan Analisis Algoritme Lanjut
Agus Budi Raharjo
5109100164
Jurusan Teknik Informatika
Fakultas Teknologi Informasi Institut Teknologi Sepuluh Nopember
Latihan
• SPOJ Problem Set (classical) 140 – The Loner
• SPOJ Problem Set (tutorial) 67 – Cutting out • SPOJ Problem Set (tutorial) 404 – Scanner
SPOJ 140 The Loner
SPOJ Problem Set (classical)
140. The Loner
Problem code: LONER
The loner is a one-dimensional board game for a single player. The board is composed of squares arranged in a
single line, some of which initially have pawns on them. The player makes a move by jumping with a pawn over
a pawn on an adjacent field, to an empty square two fields to the right or left of its initial position. The pawn that
was jumped over is removed directly after the move, as illustrated below.
The game is considered won if exactly one pawn remains on the gaming board, and is lost if the player cannot
make a move.
Given the initial state of the gaming board, your task is to determine whether it is possible for the player to win
the game.
SPOJ 140 The Loner
Input
The input begins with the integer t, the number of test cases. Then t test cases follow. Each test cases begins with
the positive integer n <= 32000, denoting the size of the gaming board. The second and last line of the test case
description contains a sequence of n characters 0 or 1, without any white spaces. The i-th square of the board is
occupied by a pawn at the start of the game iff the i-th character of this sequence is 1.
Output
For each test case output the word yes if it is possible for the player to win the game for the presented starting
configuration, or the word no in the opposite case.
Example
Sample input:
2
7
0110011
6
111001
Sample output:
yes
no
SPOJ 140 The Loner
SPOJ 140 The Loner
SPOJ 140 The Loner
Langkah penyelesaian : 1. Hilangkan angka 0 di depan dan di belakang
permainan utama (0*L0*)
2. Untuk special case, definisikan
3. Cari persamaan antara array dengan teorema 1
SPOJ 140 The Loner
Dari teorema 1 didapat kemungkinan menang susunan permainan (L; dengan 0*L0* ):
• 1 • 011 • 110 • 11(01)*00(10)*11 • 11(01)*00(11)+(10)*11 • 11(01)*(11) +00(10)*11 • 11(01)*(11)*1011(10)*11 • 11(01)*1101(11)*(10)*11 • 11(01)*(11)*01 • 10 (11)*(10)*11
Ket:
tanda WX*Y artinya WY, WXY, WXXY, WX.....Y
tanda WX+Y artinya WXY, WXXY, WXXXY, WX.....Y
SPOJ 140 The Loner
untuk mengubahnya menjadi bentuk kondisional (menggunakan if), kita jabarkan dalam bentuk tree :
Ket : tanda garis bawah dan huruf “f” menandakan akhir array
SPOJ 140 The Loner
• Cont’d
Ket : tanda garis bawah dan huruf “f” menandakan akhir array
SPOJ 67 Cutting Out
SPOJ Problem Set (classical)
67. Cutting out
Problem code: CUTOUT
One has to cut out a number of rectangles from a paper square. The sides of each rectangle are to be parallel to the
sides of the square. Some rectangles can be already cut out. What is the largest area of a rectangle which can be cut
out from the remaining paper?
Illustration
Three rectangles have been cut out from the square 10x10 in the figure shown below. The area of the largest
rectangle that can be cut out from the remaining paper is 16. One of such rectangles is shown with a dashed line.
Task
Write a program that for each data set from a sequence of several data sets:
reads descriptions of a square and rectangles from the input,
computes the area of the largest rectangle which can be cut out from the remaining paper,
writes the result to output.
SPOJ 67 Cutting Out
Langkah penyelesaian : 1. Membuat Array persegi dengan sisi sesuai test
case dan membuat status “true”
2. Untuk setiap perpotongan, objek hasil perpotongan berganti status menjadi “false”
3. Untuk mencari luas persegi terbesar cek tiap cell, jika terdapat cell bernilai true, maka akan dicek luas perseginya dan dibandingkan dengan luas sebelumnya
SPOJ 67 Cutting Out
Alur pemeriksaan : 0 : posisi sekarang 1 : cek panjang (+1) 2 : cek lebar(sesuai dengan panjang) 3 : jika sudah membentuk persegi, index akan menuju ke cell 4 4 : cek panjang baru (+1) 5 : mencari lebar 6 : jika diketahui terdapat perpotongan, maka luas yang digunakan
adalah luas sebelumnya didapat dari index sekarang -1 dikuadratkan