From fe8ca301ff2bba036ead7273168357879adc1dfd Mon Sep 17 00:00:00 2001 From: Yuval Adam Date: Sat, 7 May 2011 17:32:30 +0300 Subject: i think i got it --- 2011/qual/d/d.py | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) (limited to '2011/qual/d/d.py') diff --git a/2011/qual/d/d.py b/2011/qual/d/d.py index 0bdf8dd..d978a3c 100644 --- a/2011/qual/d/d.py +++ b/2011/qual/d/d.py @@ -6,31 +6,28 @@ def solve(a): # stop if solved while a != sol: print "%s is not %s" % (a, sol) - + flipped = False # first flip all pairs for i in range(len(a)): if a[i] != i+1 and a[a[i]-1] == i+1: print 'flipping %d and %d' % (a[a[i]-1], a[i]) - t = a[i] - a[i] = a[a[i]-1] - print 'flipping %d and %d' % (a[a[i]-1], a[i]) - a[a[i]-1] = t - print 'flipping %d and %d' % (a[a[i]-1], a[i]) - break + t = a[a[i]-1] + a[a[i]-1] = a[i] + a[i] = t steps += 2 - - # did that suffice? - if a == sol: - print "%s is not %s" % (a, sol) - break + flipped = True + + if flipped: + continue # now find the first mixed index to fix for i in range(len(a)): if a[i] != i+1: print 'flipping %d and %d' % (a[a[i]-1], a[i]) - a[i], a[a[i]-1] = a[a[i]-1], a[i] + t = a[a[i]-1] + a[a[i]-1] = a[i] + a[i] = t steps += 2 - sleep(1) return steps -- cgit v1.3.1