注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
- import copy
- def move(grid):
- def robotMove(grid, i, j):
- if i < 0 or i >= len(grid) or j < 0 or j >= len(grid[0]):
- return 0
- if grid[i][j] == 1:
- return 0
- if i == len(grid)-1 and j == len(grid[0]) - 1:
- return 1
- arr = copy.deepcopy(grid)
- arr[i][j] = 1
- return robotMove(arr,i-1,j) + robotMove(arr, i, j+1) + robotMove(arr, i,j-1) + robotMove(arr, i+1,j)
- return robotMove(grid, 0, 0)
复制代码
- def letterCombinations(self, digits):
- if not digits:
- return []
- phoneMap = { '2' : "abc", '3': "def", '4':"ghi",
- '5':"jkl", '6':"mno", '7':"pqrs", '8':"tuv",'9': "wxyz" }
- result = []
- self.dfs(digits, "", result,phoneMap)
- return result
- def dfs(self, digits, path, result,phoneMap):
- if not digits and path:
- result.append(path)
- return
- for letter in phoneMap[digits[0]]:
- self.dfs(digits[1:], path+letter, result,phoneMap)
复制代码
- import random
- import copy
- class MineSweeper():
- def __init__(self, row, col, mine_count):
- self.board = [["-" for _ in range(col)] for _ in range(row)]
- self.row = row
- self.col = col
- self.mines = self.place_mines(mine_count)
- self.gameOver = False
- self.visited = set()
- self.iteration = 0
- self.print_board()
- self.print_mine()
- def print_mine(self):
- print("Mines -------------")
- tmp = copy.deepcopy(self.board)
- for (row, col) in self.mines:
- tmp[row][col] = "X"
- for row in tmp:
- print(" ".join(row))
- def print_board(self):
- print("Board at " + str(self.iteration)+ "th iteration: -----------------")
- for row in self.board:
- print(" ".join(row))
- self.iteration += 1
- def place_mines(self, mine_count):
- cnt = 0
- locations = set()
- while cnt < mine_count:
- row, col = self.generate_random()
- if (row, col) not in locations:
- cnt += 1
- locations.add((row, col))
- return locations
- def click(self, row = None, col = None):
- if len(self.visited) == self.row*self.col:
- print("You win")
- self.gameOver = True
- return
- if row ==None and col ==None:
- row, col = self.generate_random()
- while (row, col) in self.visited:
- row, col = self.generate_random()
- print("Clicked at number {0} row, number {1} col".format(row, col))
- if (row, col) in self.mines:
- self.board[row][col] = "X"
- self.print_board()
- print("Game over!")
- self.gameOver = True
- return
- else:
- self.reveal(row, col)
- self.print_board()
- def reveal(self, row, col):
- mine_count = 0
- self.visited.add((row, col))
- for i in range(row-1, row+2):
- for j in range(col-1, col+2):
- if (i, j) in self.mines:
- mine_count += 1
- if mine_count == 0:
- self.board[row][col] = "0"
- else:
- self.board[row][col] = str(mine_count)
- return
- for i in range(row-1, row+2):
- for j in range(col-1, col+2):
- if 0 <= i < self.row and 0 <= j < self.col and (i, j) not in self.visited:
- self.reveal(i, j)
- def generate_random(self):
- return random.randint(0, self.row-1), random.randint(0, self.col-1)
- if __name__ == "__main__":
- while True:
- config = input("Enter the number of rows, columns of the board size and mines with a space in between")
- config = config.strip().split()
- if len(config) != 3:
- print("Invalid input.")
- continue
- row, col, mines = int(config[0]), int(config[1]), int(config[2])
- minesweeper = MineSweeper(row, col, mines)
- while not minesweeper.gameOver:
- click = input("Enter row number and col number you want to click.").strip().split()
- row, col = int(click[0]) , int(click[1])
- minesweeper.click(row, col)
复制代码
[/hide]
求大米呀,可怜的我只有15个大米
补充内容 (2018-11-26 23:14):
请求版主,删掉这个帖子的第一题分数到小数的code吗?或者删掉整个帖子也行。因为有朋友照抄了我的的code,刚好同一个面试官,被面试官怀疑,然后挂了。不想害地里的....好后悔发了这个。 |