You are on page 1of 11

Chng 1

BIU DIN BI TON


TRONG KHNG GIAN TRNG THI
1. t vn .
Khi gii quyt bi ton bng phng php tm kim, trc ht ta phi xc
nh khng gian tm kim bao gm tt c cc i tng trn thc hin vic
tm kim.
Mt phng php biu din vn ph hp l s dng cc khi nim trng
thi (state) v ton t (operator).
Phng php gii quyt vn da trn khi nim trng thi v ton t c
gi l cch tip cn gii quyt vn nh khng gian trng thi.
2. M t trng thi
Gii bi ton trong khng gian trng thi, trc ht phi xc nh dng m t
trng thi bi ton sao cho bi ton tr nn n gin hn, ph hp bn cht vt
l ca bi ton (C th s dng cc xu k hiu, vct, mng hai chiu, cy,
danh sch).
Mi trng thi chnh l mi hnh trng ca bi ton, cc tnh trng ban u v
tnh trng cui ca bi ton gi l trng thi u v trng thi cui.
V d 1. Bi ton ong nc
Cho 2 bnh c dung tch ln lt l m v n (lit). Vi ngun nc khng
hn ch, dng 2 bnh trn ong k lit nc. Khng mt tnh tng qut c th
gi thit k <= min(m,n).
Ti mi thi im xc nh, lng nc hin c trong mi bnh phn nh
bn cht hnh trng ca bi ton thi im .
- Gi x l lng nc hin c trong bnh dung tch m v y l lng nc
hin c trong bnh dung tch n. Nh vy b c th t (x,y) c th xem l trng
thi ca bi ton. Vi cch m t nh vy, cc trng thi c bit ca bi ton s l:
- Trng thi u: (0,0)
-

Trng thi cui: (x,k) hoc (k,y), 0 x m , 0 y n


12

V d 2. Bi ton tr chi 8 s
Trong bng vung 3 hng, 3 ct , mi cha mt s nm trong phm vi
t 1 n 8 sao cho khng c 2 c cng gi tr, c mt trong bng b trng
(khng cha gi tr no c. Xut pht t mt sp xp no cc s trong bng,
hy dch chuyn trng sang phi, sang tri, ln trn hoc xung di (nu c
th c) a v bng ban u v bng qui c trc. Chng hn Hnh 1.
di y l bng xut pht v Hnh 2. l bng m ta phi thc hin cc bc di
chuyn trng t c.
2 8 3
1 6 4
7
5
Hnh 1.
1 2 3
8
4
7 6 5
Hnh 2.
Gi tr cc phn t trong bng xc nh trng thi bi ton. V vy c th
m t trng thi ca bi ton bng mt ma trn A3*3= (aij) , aij{0..8}, aij < > akl,
i<>k, j<> l
- Trng thi u ca bi ton l ma trn
2 8 3

1 6 4
7 0 5

- Trng thi cui l ma trn


1 2 3

8 0 4
7 6 5

C th pht biu dng tng qut ca bi ton ny (Tr chi n2-1 s)


V d 3. Bi ton thp H Ni
13

Cho ba cc 1, 2, 3. cc 1 ban u c n a sp xp theo th t to dn t di


ln trn. Hy dch chuyn n a sang cc th 3 sao cho:
-

Mi ln ch chuyn mt a.

Trong mi cc khng cho php a to nm trn a nh hn.

Bi ton xc nh khi bit c tng a ang nm cc no. Hay ni cch


khc, c hai cch xc nh:
1- Cc 1 hin ang cha nhng a no? Cc 2 hin ang cha nhng a
no? V cc 3 ang cha nhng a no.
2- a ln th i hin ang nm cc no? ( i = 1 .. n )
Nh vy cch m t trng thi bi ton khng duy nht, vn l chn cch m
t no t c mc ch d dng nht.
Theo trn, vi cch th nht ta phi dng 3 danh sch ng v s a trn mi
cc l khc nhau trong tng thi im khc nhau.
Cch th hai, nhn qua th kh m t nhng da vo khi nim v b c th t
trong ton hc, cch ny m t bi ton hiu qu hn. Tht vy, nu gi x i l cc
cha a ln th i, trong xi{1, 2, 3}, i{1 ..n}. Khi b c th t (x1,
x2, . . ,xn) c th dng lm dng m t trng thi ang xt ca bi ton. Vi cch
m t ny,
Trng thi u l (1,1,. . .,1)
Trng thi cui l (3,3,. . .,3)
3. Ton t chuyn trng thi.
Ton t chuyn trng thi thc cht l cc php bin i a t trng thi ny
sang trng thi khc. C hai cch dng biu din cc ton t:
-

Biu din nh mt hm xc nh trn tp cc trng thi v nhn gi

tr cng trong tp ny.


-

Biu din di dng cc quy tc sn xut S? A c ngha l nu c

trng thi S th c th a n trng thi A.

14

V d 1. Bi ton ong nc
Cc thao tc s dng chuyn trng thi ny sang trng thi khc gm:
y mt bnh, ht nc trong mt bnh ra ngoi, nc t bnh ny sang
bnh khc. Nh vy, nu trng thi ang xt l (x,y) th cc trng thi k tip c
th chuyn n s l:
(m,y)
(x,n)
(0,y)
(x,0)
(x,y)

(0, x+ y) nu x+y < = n


(x+y -n,n) nu x+y > n
(x+ y,0) nu x+y < = m
(m, x+y-m) nu x+y > m

V d 2. Tr chi 8 s
Cc thao tc chuyn trng thi tng ng vi vic chuyn trng sang
phi, sang tri, ln, xung nu c th c.
-

Biu din theo quy tc sn xut:

1342587
6

1325487
6
1342587
6
1342568
7

- Biu din theo mt hm


Gi hm fu l hm biu din cho ton t chuyn trng ln trn; gi B (B=
(bij)) l trng thi sau khi di chuyn trng trng thi A (A= (aij)) ln trn,
ngha l: B= fu(A), gi s trng ang v tr (i0, j0) (hay ni cch khc ai0 j0 = 0)
th hm f c xc nh nh sau:

15

fu(aij) =

aij

(i, j)

aij

nu (i, j) (i0-1, j0) v (i, j) (i0, j0) v i0 >1

ai0-1, j0

nu (i, j) = (i0, j0), i0 >1

ai0, j0

nu (i, j) = (i0-1, j0), i0 >1

nu i0 = 1

Tng t, c th xc nh cc php chuyn trng xung di fd, qua tri fl, qua
phi fr nh sau:

fd(aij) =

fl(aij) =

fr(aij) =

aij

(i, j)

aij

nu (i, j) (i0+1, j0) v (i, j) (i0, j0) v i0 <3

ai0-1, j0

nu (i, j) = (i0, j0), i0 <3

ai0, j0

nu (i, j) = (i0+1, j0), i0 <3

aij

(i, j)

aij

nu (i, j) (i0, j0-1) v (i, j) (i0, j0) v j0 > 1

ai0-1, j0

nu (i, j) = (i0, j0), j0 > 1

ai0, j0

nu (i, j) = (i0, j0-1), j0 > 1

aij

(i, j)

aij

nu (i, j) (i0, j0+1) v (i, j) (i0, j0) v j0 < 3

ai0-1, j0

nu (i, j) = (i0, j0), j0 < 3

ai0, j0

nu (i, j) = (i0, j0+1), j0 < 3

nu i0 = 3

nu j0 = 1

nu j0 = 3

V d 3. Bi ton Thp H Ni vi n=3.


Mi trng thi l mt b ba (i, j, k). C cc trng hp nh sau:
- Ba a cng nm trn mt cc: (i, i, i)
- Hai a cng nm trn mt cc: (i, i, j), (i, j, i), (j, i, i)
- Ba a nm trn ba cc phn bit: (i, j, k)

16

(i, i, i)

(i, i, j)
(i, i, k)

(i, i, j)

(i, i, k)
(i, k, j)
(i, i, i)

(i, j, i)

(i, j, k)
(i, j, j)
(i, k, i)

(j, i, i)

(j, i, j)
(j, i, k)
(k, i, i)

(i, j, k)

(i, i, k)
(i, j, j)
(i, j, i)

4. Khng gian trng thi ca bi ton.


4 Kkhng gian trng thi l tp tt c cc trng thi c th c v tp cc ton t

ca bi ton.
Khng gian trng thi l mt b bn, K hiu: K= (T, S, G, F). Trong ,
T: tp tt c cc trng thi c th c ca bi ton
S: trng thi u
G: tp cc trng thi ch
F: tp cc ton t
V d 1. Khng gian trng thi ca bi ton ong nc l b bn T, S, G, F xc
inh nh sau:
4 T = { (x,y) / 0 <= x <= m; 0 <= y <= n }
5 S = (0,0)
6 G = { (x,k) hoc (k,y) / 0 <= x <= m; 0 <= y <= n}
7 F = Tp cc thao tc ong y, ra hoc sang bnh khc thc hin trn
mt bnh.

17

V d 2. Khng gian trng thi ca bi ton Thp H ni vi n = 3:


T = { (x1, x2, x3)/ xi {1, 2, 3} }
S = (1, 1, 1)
G = {(3, 3, 3)}
F = Tp cc kh nng c th chuyn a xc nh trong phn
trc.
V d 3. Khng gian trng thi ca bi ton tr chi 8 s:
T = { (aij)3x3 / 0<= aij <= 8 v aij <> akl vi i<> j hoc k <> l}
S = Ma trn xut pht ca bi ton,
G = Ma trn cui cng ca bi ton (cc s nm theo v tr yu
cu)
F = {fl, fr, fu, fd}
Tm kim li gii trong khng gian trng thi l qu trnh tm kim xut pht t
trng thi ban u, da vo ton t chuyn trng thi xc nh cc trng thi
tip theo cho n khi gp c trng thi ch.
5. Biu din khng gian trng thi di dng th
5.1. Cc khi nim
th G = (V,E) trong V:tp nh, E: tp cung (EV*V)

Ch
-

G l th v hng th (i, j) l mt cnh cng nh l (j, i) (tc l:(i, j)E th


(j,i)E)

- Nu G l th c hng th cung (i, j) hon ton khc vi cung (j, i).


V d xt d th v hng G1 v th c hng G2
1
2

1
4

G1

G2

Tp nh k:
18

nV, T(n)={mV/ (n,m) E}c gi l tp cc nh k ca n


ng i:
p = (n1,...,nk) c gi l ng i t nh n1 nk nu ni V, i=1,k ;
(ni, ni+1)E i=1, k -1

Cy l th c nh gc n0V tho:

Mt th G=(V, E) gi l cy nu tn ti mt nh n0V c nhng tnh cht


sau:
-

nV, nT(n0), trong T(n0): tp cc nh thuc dng di ca n0 (n0 l t


tin ca n)

nV, !mV sao cho nT(m); m c gi l cha ca n.

5.2. Biu din khng gian trng thi bng th


Theo ngn ng th, khng gian trng thi tng ng vi mt th
nh hng trong : Cc trng thi tng ng vi cc nh trong th, nu tn
ti ton t chuyn trng thi th c cung (s, t)
thy r mi tng quan, ta c bng sau
KGTT
Trng thi

th
nh

Ton t

Cung

Dy cc trng thi lin tip ng i


5.3. Biu din th
Cho th G = (V,E) , gi s V={1, 2,....,n}. C hai cch thng dng
biu din th G lu tr trong my tnh.
i) Biu din bng ma trn k
th G c biu din bi ma trn k A=(aij)nxn, vi n l s nh ca th,
trong :
aij=

nu (i, j) E

trong trng hp ngc li

19

Nu G l th v hng th ma trn k A l ma trn i xng.


V d Vi th v hng G1 v th c hng G2 trn ta c cc ma trn k
sau:
G1:

0
1
1
1

1
0
1
1

1
1
0
0

1
1
0
0

0
1
0
0

G2:

1
0
0
0

0
1
0
1

1
1
0
0

ii) Biu din bng danh sch k.


Vi mi nh i ca th, ta c mt danh sch tt c cc nh k vi i, ta
k hiu l List(i). th hin List(i) ta c th dng mng, kiu tp hp hay kiu
con tr. V d vi th G1, ta c List(1)= [2, 3, 4]
V d 1. Bi ton ong nc m=3, n=2, k=1
(0,0)
(3,0)

(0,2)

(1,2)

(3,2)

(1,0)

(0,1)

(2,0)
(2,2)
(3,1)

V d 2. Thp H Ni vi n = 3
111

(112)
112

(111)
(113)

(132)
(133)

(123)
(131)

(121)

(122)

(233)
(231)
(221)
(222)
(223)

(322)
(232)

(323)
(212)

(213)

(321)

(313)
(211)

(311)

(331)
(312)

(332)

(333)

20

6. BI TP
Xy dng khng gian trng thi i vi cc bi ton sau:
6.1. Cho n thnh ph nh s t 1 n n. Giao thng ng b gia hai thnh
ph i v j c cho bi gi tr aij nh sau: aij = -1 c ngha l khng c ng b
i t thnh ph i sang thnh ph j v a ij = 1 nu c ng i trc tip t thnh
ph i sang thnh ph j. Tm ng i t thnh ph i0 sang thnh ph j0.
6.2. Cho k v n l 2 s nguyn dng. C 2 k vin si, c phn b trong n
ng, ng th nht c a1 vin, ng th 2 c a2 vin, , ng th n c an vin
v tt nhin a1+ a2 + + an = 2k. Ngi ta cn san s lng si t cc ng
dn si tr v 1 ng. Quy tc san si nh sau: mi ln san p dng cho 2 ng
si, gi s 1 ng c a vin v ng kia c b vin (khng gim tng qut, c th
gi thit a b) th san si t ng c a vin sang ng c b vin thnh mt
ng c a-b vin v ng kia 2*b vin.
Hng dn: Trng thi ca bi ton phi xc nh c s si hin c trong mi
ng.
6.3. Mt dy cc s nguyn dng a1, a2, , an c gi l hp l nu tho mn
hai iu kin:
-

an l s nguyn t.

ai+1 = ai +1 hoc 2*ai

Cho trc s a1, hy tm dy hp l a1, a2, , an.


6.4 Bi ton ngi a hng.
Ngi a hng cn phi xc nh c hnh trnh ngn nht sao cho mi
thnh ph i n ng mt ln v quay tr li thnh ph xut pht. Gi s thnh
ph xut pht l thnh ph 1, c tt c n thnh ph nh s t 1 n n.

21

Hng dn:
- Mi trng thi cho bi danh sch cc thnh ph i qua cho n thi im
hin ti, trong khng cho php mt thnh ph no c xut hin nhiu
hn mt ln tr thnh ph 1 sau khi lit k tt c cc thnh ph cn li.
- Cc ton t tng ng vi cc hnh ng
1- i ti thnh ph 1
2- i ti thnh ph 2
3- i ti thnh ph 3.
...
6.5. Bi ton phn tch c php.
Vn phm G l b bn G = (N, T, P, S), N l tp k hiu khng kt thc,
T l tp k hiu kt thc, S N l k hiu u v P l tp sn xut c dng
, y , (NUT). Ngn ng sinh ra bi vn phm G c nh ngha bi:\
L(G) = { T | S }, S c ngha l 1, , n (NUT) sao
cho i i+1, 1 = S v n = .
Bi ton phn tch c php c pht biu : ch trc vn phm G vi xu
T cho hy xc nh xem L(G) hay khng ?

22