Write a program that, given a starting location in a 2D grid of characters, will count how many contiguously connected @ symbols there are. Cells have to be direct neighbors either horizontally or vertically to count as contiguous. However, the right/left and top/bottom edges ARE considered to be connected – you can “wrap around” from one side of the matrix to the opposite one.
The grid of chars will always be 10x10. Your program should load the grid of chars from a file called “grid.txt”. A sample grid.txt is included.
Your program MUST make use of a recursive function for the core problem.
You may use loops to read in the data from the file.
Do NOT use a global variable to keep track of the count.
Sample Runs – User input in BOLD – all row/col are zero-indexed
See the image below for an example of how the characters are counted.
Run 1:
Enter row and column:
0 0
5 cells
Run 2:
Enter row and column:
1 7
5 cells
Run 3:
Enter row and column:
3 1
11 cells
Run 4:
Enter row and column:
2 0
0 cells
Sample Grid:
@-@--@-@@-
@@@-@@-@--
---@------
@@-@@@-@@@
-@-@-@-@--
@@-@@@-@@-
----------
-@@@-@----
-@@@@@-@-@
-------@-@
this is what i have so far, I have spent hours trying to figure out where to go from here and I am lost. I am currently working on the check neighbors function
The grid of chars will always be 10x10. Your program should load the grid of chars from a file called “grid.txt”. A sample grid.txt is included.
Your program MUST make use of a recursive function for the core problem.
You may use loops to read in the data from the file.
Do NOT use a global variable to keep track of the count.
Sample Runs – User input in BOLD – all row/col are zero-indexed
See the image below for an example of how the characters are counted.
Run 1:
Enter row and column:
0 0
5 cells
Run 2:
Enter row and column:
1 7
5 cells
Run 3:
Enter row and column:
3 1
11 cells
Run 4:
Enter row and column:
2 0
0 cells
Sample Grid:
@-@--@-@@-
@@@-@@-@--
---@------
@@-@@@-@@@
-@-@-@-@--
@@-@@@-@@-
----------
-@@@-@----
-@@@@@-@-@
-------@-@
this is what i have so far, I have spent hours trying to figure out where to go from here and I am lost. I am currently working on the check neighbors function
def import_grid(filename): #takes file and puts in into lists for grid with open(filename, "r") as temp_file: full_grid = [] temp_grid = [] for gridLines in temp_file.readlines(): temp_grid.append(gridLines.rstrip().split("[]")) for line in temp_grid: for i in line: full_grid.append(i) return full_grid def get_input(): user_input_list = [] user_input = input("Enter row and column:") for char in user_input: user_input_list.append(char) return user_input_list def check_neighbors(grid, row, col): row_size = 10 col_size = 10 start = grid[row-1][col-1] #this is the starting point print(start) def main(): file = "grid.txt" location = get_input() grid = import_grid(file) row = int(location[0]) col = int(location[2]) number_connected = check_neighbors(grid, row, col) if __name__ == '__main__': main()