Tuesday, January 7, 2014

single plane maze router with obstacle

TCL code for a single place maze router which considers obstacle while routing to destination .

set heap_sort ""
proc add_val {val} {
global heap_sort;
global MAX_X;
global MAX_Y;
## PUSH only valid values
if { [lindex $val 0 1 0] < 0 || [lindex $val 0 1 0] > $MAX_X ||
    [lindex $val 0 1 1] < 0 || [lindex $val 0 1 1] > $MAX_Y } {
return ;
}
set heap_sort "$heap_sort $val "
set heap_sort [lsort -index 0 -integer $heap_sort]
puts "$heap_sort "
}
proc get_val {} {
global heap_sort;
set val ""
if { [llength $heap_sort] < 1 } {
puts $val
return $val
}
set val [lindex $heap_sort 0]
if { [llength $heap_sort] > 1 } {
puts "deleting 0th element"
set heap_sort [lrange $heap_sort 1 end]
} else {
puts "deleting 0th element"
set heap_sort ""
}
puts $val
return $val
}

proc find_paths {file} {
global MAX_X;
global MAX_Y;
set total_paths 0
set f [open $file r]
#Just File Processing to simplify the input to be given
while { ! [eof $f] } {
gets $f str
if { [regexp {^SIZE\s*(\d+)\,(\d+)} $str mv MAX_X MAX_Y] } {
} elseif {[regexp {^START\s*(\d+)\,(\d+)} $str mv C_X C_Y ] } {
} elseif {[regexp {^S\s*(\d+)\,(\d+)} $str mv tx ty] } {
# This is a 2D array in TCL, remembering the solidier co-ordinates
set soldier($tx,$ty) 1
}
}
# File Processing Done
set num 0
#Assuming Castle moves Down first
set dir DOWN
set cnt 0
#C_X and C_Y are the initial castle co-ordinates
#add_val is a proc like a function in C
#add_val arguments are {weight {Castlelocationx castlelocationy CastleDirection} {ourPath}}
add_val "{0 {$C_X $C_Y DOWN} {}}"
# while to make sure we dont go to infinite loop
while { $num < 10000  } {
#get_val is a proc like a function in c, this function returns the first element of the list and changes the list to remove the first element
set c_node [get_val]
if { $c_node == "" } {
puts "Heap Empty , processing DONE "
break ;
}
#puts "Expanding $c_node"
set c_wave_path [lindex $c_node 2]
set dir [lindex $c_node 1 2 ]
set c_x [lindex $c_node 1 0 ]
set c_y [lindex $c_node 1 1 ]
set n_c_y $c_y
set n_c_x $c_x
if {$dir == "DOWN" } {
set n_c_y [expr $c_y + 1]
set n_dir "RIGHT"
} elseif { $dir == "UP" } {
set n_c_y [expr $c_y - 1]
set n_dir "LEFT"
} elseif { $dir == "LEFT" } {
set n_c_x [expr $c_x - 1]
set n_dir  "DOWN"
} elseif { $dir == "RIGHT" } {
set n_c_x [expr $c_x + 1]
set n_dir  "UP"
}
if { [info exists soldier($n_c_x,$n_c_y)] } {
add_val "{[expr [lindex $c_node 0 0] + 30 ] {$n_c_x $n_c_y $n_dir} {$c_wave_path {$c_x $c_y}}}"

}
if { $n_c_x == $C_X && $n_c_y == $C_Y } {
puts "Reached Castle"
set c_wave_path_t [lindex $c_node 2]
set c_wave_path_t "$c_wave_path_t {$n_c_x $n_c_y}"
puts "Path is \n[join $c_wave_path_t \"\n\"]"
incr total_paths
continue
}
add_val "{[expr [lindex $c_node 0 0] + 1 ] {$n_c_x $n_c_y $dir} {$c_wave_path {$c_x $c_y}}}"

incr num
}
puts "Total Number of Paths to Castle $total_paths"
}
#calling Function "find_paths" for which input is file called "abc"
find_paths abc

No comments:

Post a Comment