Python Forum
Problem in a path finding algorithm (counter is not working)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem in a path finding algorithm (counter is not working)
#1
Ok, so here´s the drill: I´m doing a problem that receives a matrix of size NxN (the problem called it a "mine", hence the variable name used) containing 0´s and 1´s, and then finds the path that goes throw the least number of 1´s and then prints out the number of 1´s that the path had to go throw. So this code i made works by receiving the input variable N, and then a matrix of size NxN containing 0´s and 1´s. After that, i generate a tuple that contains all the possible paths (the letters "R" and "D" represent the way the path is going, either right or down, these are the only moves allowed in the problem´s statement). After that, i go through each possible path, and then i have a variable that counts how many 1´s did it have to go throw. The variable "min" then receives the value of the counter for each path if that counter is smaller than the previous minimum path counter. In this way, when i print the variable "min", i should get the minimum number of 1´s you have to go throw in order to complete the path of the matrix (go from [0][0] to [N-1][N-1]), but in the simples test case of a 2x2 matrix [[0, 0], [1, 0]] which is supposed to be 0, obviously, i´m getting 1. Can someone help figure this out please? (The code is down below)

from itertools import permutations

N = int(input())
Mine = [input().split() for x in range(N)]
Mine = [[int(i) for i in x] for x in Mine]

sample_path = "D"*(N-1)+"R"*(N-1)
paths = set(permutations(sample_path, 2*(N-1)))
Min = N**2

for i in list(paths):
  counter = 0
  pos = [0, 0]
  for j in i:
    if j == 'D':
      if Mine[pos[0]+1][pos[1]] == 1:
        counter += 1
        pos[0] += 1
    elif j == 'R':
      if Mine[pos[0]][pos[1]+1] == 1:
        counter += 1
        pos[1] += 1
  if counter < Min: Min = counter
    
print(Min)
Reply


Messages In This Thread
Problem in a path finding algorithm (counter is not working) - by Kowalski - Feb-05-2018, 11:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Appending sys.path not working with spyder Larz60+ 2 731 Apr-11-2025, 08:50 AM
Last Post: Larz60+
  working directory if using windows path-variable chitarup 2 1,627 Nov-28-2023, 11:36 PM
Last Post: chitarup
  Floor approximation problem in algorithm gradlon93 3 1,905 Dec-14-2022, 07:48 PM
Last Post: Gribouillis
Big Grin question about simple algorithm to my problem jamie_01 1 2,350 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 3,357 Sep-09-2021, 01:25 PM
Last Post: Yoriz
Exclamation Path sacn problem lucky511 10 5,872 Jun-24-2021, 12:09 PM
Last Post: Axel_Erfurt
  Subprocess.Popen() not working when reading file path from csv file herwin 13 25,720 May-07-2021, 03:26 PM
Last Post: herwin
  problem about maze path finder Simurg 2 2,840 Aug-16-2020, 01:10 PM
Last Post: Simurg
  a weired problem after restructing algorithm homepoeple 0 2,017 Jan-15-2020, 06:25 PM
Last Post: homepoeple
  Finding a Hamiltonian Path Efficiently - ADVANCED stauffski 3 7,859 Dec-31-2019, 10:25 PM
Last Post: stauffski

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020