Dynamic Programming(will be upgrading always)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15642411/article/details/85114442

1、回朔解决迷宫问题

r,c=4,4
def isSafe(maze,x,y):
    if x>=0 and x<c and y>=0 and y<r and maze[y][x]==1:
        return True
    return False 
def solvemaze(maze,x,y,sol):
    if x==c-1 and y==r-1:
        sol[y][x]=1
        return True
    if isSafe(maze,x,y)==True:
        sol[y][x]=1
        if solvemaze(maze,x,y+1,sol)==True:
            return True
        if solvemaze(maze,x+1,y,sol)==True:
            return True
        sol[y][x]=0
        return False
def solve(maze):
    sol=[[0 for i in range(c)] for i in range(r)]
#    sol=[[0]*c]*r #用这个方式结果就出错
    if solvemaze(maze,0,0,sol)==False:#sol 是可变类型,传引用
        print("can not output path")
        return False 
    printReslut(sol)
    return sol
def printReslut(sol):
    for r in sol:
        for e in r:
            print(str(e)+" ",end=" ")
        print(" ")
maze = [ [1, 0, 0, 0], [1, 1, 0, 1], 
    [0, 1, 0, 0], [1, 1, 1, 1] ]
print("maze")
printReslut(maze)
print("*********result********")
solve(maze)
result:
maze
1  0  0  0   
1  1  0  1   
0  1  0  0   
1  1  1  1   
*********result********
1  0  0  0   
1  1  0  0   
0  1  0  0   
0  1  1  1   
*************************************

2、最小编辑距离,将str1转为str2需要几步操作

def editDistance(str1,str2,m,n):
    if m==0:
        return n
    if n==0:
        return m
    if str1[m-1]==str2[n-1]:
        return editDistance(str1,str2,m-1,n-1)
    return 1+min(editDistance(str1,str2,m-1,n-1),editDistance(str1,str2,m-1,n),editDistance(str1,str2,m,n-1))
if __name__=="__main__":
    str1="greek"
    str2="greesak"
    print(editDistance(str1,str2,len(str1),len(str2)))
result :2 插入s,插入a
***************************************

3、数字游戏,给定一个数,只能由3,5,10这三个数的加法组合得到,3,5,10每个数字的使用次数不限,输出几种组合法

def count(n): 
    table = [0 for i in range(n+1)] 
    table[0] = 1 #当n=3,n=5时候只能有一种组合
    for i in range(3, n+1): 
        table[i] += table[i-3] 
    for i in range(5, n+1): 
        table[i] += table[i-5] 
    for i in range(10, n+1): 
        table[i] += table[i-10] 
    return table[n]
print(count(20))
result:4 
(10, 10)
(5, 5, 10)
(5, 5, 5, 5)
(3, 3, 3, 3, 3, 5)
*****************************************

4、最小到站问题

#include <iostream>
#include<climits>
using namespace std;
#define INF INT_MAX
/*
最小到站问题,  { {0, 15, 80, 90},
              {INF, 0, 40, 50},
              {INF, INF, 0, 70},
              {INF, INF, INF, 0}
             };
矩阵表示,从r到c站,r>c时候是无效的,15表示从1站到2站所花费时间(money等都可以)
这个问题可以用迭代解决:
minCost(0, N-1) = MIN { cost[0][n-1],
                        cost[0][1] + minCost(1, N-1),
                        minCost(0, 2) + minCost(2, N-1),
                        ........,
                        minCost(0, N-2) + cost[N-2][n-1] }
 */
const int N=4;
int minCostRec(int cost[][N], int s, int d)
{
    if (s == d || s+1 == d)//起点等于终点,或者起点的下一站是终点
      return cost[s][d];
    int min = cost[s][d];//以第一个作为基准,找到最小的
    for (int i = s+1; i<d; i++)
    {
        int c = minCostRec(cost, s, i) +
                minCostRec(cost, i, d);
        if (c < min)
           min = c;
    }
    return min;
}

int minCost(int cost[][N])
{
    return minCostRec(cost, 0, N-1);
}
int main()
{
    int cost[N][N] = { {0, 15, 80, 90},
                       {INF, 0, 40, 50},
                       {INF, INF, 0, 70},
                       {INF, INF, INF, 0}
                     };
    cout << "最小到终点4站所花费的时间(钱)是:"<< minCost(cost)<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_15642411/article/details/85114442