鱼C论坛

 找回密码
 立即注册
查看: 3463|回复: 24

[技术交流] Python:每日一题 182

[复制链接]
发表于 2018-7-5 11:23:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 冬雪雪冬 于 2018-7-6 21:14 编辑

我们的玩法做了一下改变:

1. 楼主不再提供答案。
2. 请大家先独立思考,再参考其他鱼油的解答,这样才有助于自己编程水平的提高。开始阶段是看不到其他人的回帖的,等答题完成,开始评分时再取消限制。
3. 鼓励大家积极答题,奖励的期限为出题后24小时内。
4. 根据答案的质量给予1~3鱼币的奖励。

题目:
windows自带的游戏挖雷大家都会玩,这里给出一个10*10的矩阵,用“*”表示地雷,要求你把周围有几颗雷的数字替换“-”,以2*2的矩阵为例:
  1. -*
  2. --
复制代码

填好数字为
  1. 1*
  2. 11
复制代码


给定的10*10矩阵是:
  1. *----*----
  2. -----**---
  3. ---*--**--
  4. ------****
  5. ---*---*--
  6. -*--**---*
  7. ----------
  8. ----------
  9. --*--*--**
  10. -*---*----
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-7-5 12:49:12 | 显示全部楼层
本帖最后由 colinshi 于 2018-7-5 12:53 编辑
  1. n = [
  2. ['*', '-', '-', '-', '-', '*', '-', '-', '-', '-'],
  3. ['-', '-', '-', '-', '-', '*', '*', '-', '-', '-'],
  4. ['-', '-', '-', '*', '-', '-', '*', '*', '-', '-'],
  5. ['-', '-', '-', '-', '-', '-', '*', '*', '*' ,'*'],
  6. ['-', '-', '-', '*', '-', '-', '-', '*', '-', '-'],
  7. ['-', '*', '-', '-', '*', '*', '-', '-', '-', '*'],
  8. ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'],
  9. ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'],
  10. ['-', '-', '*', '-', '-', '*', '-', '-', '*', '*'],
  11. ['-', '*', '-', '-', '-', '*', '-', '-', '-', '-']
  12. ]

  13. r = [
  14. [-1, -1],
  15. [-1, 0],
  16. [-1, 1],
  17. [0, -1],
  18. [0, 1],
  19. [1, -1],
  20. [1, 0],
  21. [1, 1],
  22.         ]

  23. for i in range(10):
  24.         for j in range(10):
  25.                 if n[i][j] != '*':
  26.                         count=0
  27.                         for k,v in r:
  28.                                 if (i + k) >= 0 and (i + k) < 10 and (j + v) >= 0 and (j + v) < 10:
  29.                                         if n[i+k][j+v] == '*':
  30.                                                 count += 1
  31.                         if count > 0:
  32.                                 n[i][j] = count

  33. for x in n:
  34.         print(x)
复制代码


先把矩阵设置为一个二维数组。定义一个雷区周边范围。双循环搞定
结果也贴一下

  1. ['*', 1, '-', '-', 2, '*', 3, 1, '-', '-']
  2. [1, 1, 1, 1, 3, '*', '*', 3, 1, '-']
  3. ['-', '-', 1, '*', 2, 4, '*', '*', 4, 2]
  4. ['-', '-', 2, 2, 2, 2, '*', '*', '*', '*']
  5. [1, 1, 2, '*', 3, 3, 4, '*', 5, 3]
  6. [1, '*', 2, 2, '*', '*', 2, 1, 2, '*']
  7. [1, 1, 1, 1, 2, 2, 1, '-', 1, 1]
  8. ['-', 1, 1, 1, 1, 1, 1, 1, 2, 2]
  9. [1, 2, '*', 1, 2, '*', 2, 1, '*', '*']
  10. [1, '*', 2, 1, 2, '*', 2, 1, 2, 2]
复制代码

点评

把最后的print(x)换成print(*x),输出会更漂亮些  发表于 2018-7-6 21:15

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-5 13:07:59 | 显示全部楼层
  1. from copy import deepcopy as dc

  2. def cnt(lss,i,j,f='*'):
  3.     r = 0
  4.     for a in range(max(0,i-1),min(len(lss),i+2)):
  5.         for b in range(max(0,j-1),min(len(lss[a]),j+2)):
  6.             if lss[a][b] == f:
  7.                 r += 1
  8.     return r

  9. def fun( lss , f='*'):
  10.     lss = dc(lss)
  11.     for i in range(len(lss)):
  12.         for j in range(len(lss[i])):
  13.             t = lss[i][j]
  14.             if t == f:
  15.                 continue
  16.             lss[i][j] = str(cnt(lss,i,j,f))
  17.     return lss

  18. def to_lss(s):
  19.     return [list(x) for x in s.split()]

  20. def to_str(lss):
  21.     return '\n'.join(''.join(x) for x in lss)

  22. def main():
  23.     s = '''*----*----
  24. -----**---
  25. ---*--**--
  26. ------****
  27. ---*---*--
  28. -*--**---*
  29. ----------
  30. ----------
  31. --*--*--**
  32. -*---*----'''
  33.     print(s)
  34.     lss = to_lss(s)
  35.     res = fun(lss)
  36.     print( to_str(res) )

  37. if __name__ == "__main__":
  38.     main()
复制代码

结果
  1. *----*----
  2. -----**---
  3. ---*--**--
  4. ------****
  5. ---*---*--
  6. -*--**---*
  7. ----------
  8. ----------
  9. --*--*--**
  10. -*---*----
  11. *1002*3100
  12. 11113**310
  13. 001*24**42
  14. 002222****
  15. 112*334*53
  16. 1*22**212*
  17. 1111221011
  18. 0111111122
  19. 12*12*21**
  20. 1*212*2122
  21. >>>
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-5 14:33:47 | 显示全部楼层
  1. #判断是否超出矩阵范围
  2. def is_out_range(x,y):
  3.     if 0<=x<10:
  4.         if 0<=y<10:
  5.             return False
  6.         else:
  7.             return True
  8.     else:
  9.         return True


  10. #将给定矩阵存入test_list中
  11. test_str = '*----*---------**------*--**--------****---*---*---*--**---*----------------------*--*--**-*---*----'
  12. test_list = []
  13. for i in range(10):
  14.     temp = []
  15.     for j in range(10):
  16.         temp.append(test_str[10*i+j])
  17.     test_list.append(temp)

  18. #将test_list中的“-”改为“0”
  19. for each in test_list:
  20.     for i in range(10):
  21.         if each[i] == '-':
  22.             each[i] = 0

  23. #将每个“*”周围的数字加1
  24. for i in range(10):
  25.     for j in range(10):
  26.         if test_list[i][j] == '*':
  27.             #依次从“上、下、左、右、左上、左下、右上、右下”周围八个点分别进行判断
  28.             dir_list = [[i-1,j],[i+1,j],[i,j-1],[i,j+1],[i-1,j-1],[i+1,j-1],[i-1,j+1],[i+1,j+1]]
  29.             #当不超出矩阵范围以及不是“*”时将计数加1
  30.             for each in dir_list:
  31.                 if not is_out_range(each[0],each[1]) and test_list[each[0]][each[1]]!= '*':
  32.                     test_list[each[0]][each[1]] += 1

  33. #打印结果
  34. for each in test_list:
  35.     result = ''
  36.     for one in each:
  37.         if one == '*':
  38.             result += one
  39.         else:
  40.             result += str(one)
  41.     print(result)
复制代码

   
结果为:
  1. *1002*3100
  2. 11113**310
  3. 001*24**42
  4. 002222****
  5. 112*334*53
  6. 1*22**212*
  7. 1111221011
  8. 0111111122
  9. 12*12*21**
  10. 1*212*2122
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-5 16:44:18 | 显示全部楼层
本帖最后由 天圆突破 于 2018-7-5 16:54 编辑
  1. import random
  2. import copy
  3. def makeBattleField(N, seed):   #随机生成扫雷原图
  4.     mines, battleField = random.sample(range(N**2), seed), list()
  5.     background = list(map(lambda x: '*' if x in mines else '-', range(N**2)))
  6.     for i in range(N):
  7.         battleField.append(background[i*N:(i+1)*N])
  8.     return battleField

  9. def mines_clearance(N, seed):
  10.     army = [(-1,-1), (-1,0),(-1,1), (0,-1), (0, 1), (1, -1), (1, 0), (1,1)] #位置表
  11.     battleField = makeBattleField(N, seed)          #生成扫雷原图
  12.     battleField2 = copy.deepcopy(battleField)       #深拷贝一份原图随后一起传出去
  13.     for i in range(N):
  14.         for j in range(N):
  15.             if battleField[i][j] == '-':
  16.                 res = 0
  17.                 for warrior in army:
  18.                     if i+warrior[0] <0 or j+warrior[1]<0:
  19.                         continue
  20.                     try:
  21.                         if battleField[i+warrior[0]][j+warrior[1]] == '*':
  22.                             res += 1
  23.                     except:
  24.                         continue
  25.                 battleField[i][j] = res
  26.     return battleField, battleField2

  27. if __name__ == '__main__':
  28.     battleField, battleField2 = mines_clearance(10, 30)
  29.     #打印带战场迷雾的地图
  30.     for crater in battleField2:
  31.         print(*crater)
  32.     print('')
  33.     #打开输入作弊码以后的地图
  34.     for crater in battleField:
  35.         print(*crater)
复制代码

用现成地图编辑格式实在是太麻烦了啊,还不如随机生成个新的

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-5 18:26:18 | 显示全部楼层
本帖最后由 Luke李 于 2018-7-5 18:29 编辑

import numpy as np


def countValue(array, a, b, count):
    if array[a] == '-':
        if a - 1 >= 0 and b - 1 >= 0:
            if array[a - 1][b - 1] == '*': count += 1
        if a - 1 >= 0 and b >= 0:
            if array[a - 1] == '*': count += 1
        if a - 1 >= 0 and b + 1 <= 9:
            if array[a - 1][b + 1] == '*': count += 1
        if b - 1 >= 0:
            if array[a][b - 1] == '*': count += 1
        if b + 1 <= 9:
            if array[a][b + 1] == '*': count += 1
        if a + 1 <= 9 and b - 1 >= 0:
            if array[a + 1][b - 1] == '*': count += 1
        if a + 1 <= 9:
            if array[a + 1] == '*': count += 1
        if a + 1 <= 9 and b + 1 <= 9:
            if array[a + 1][b + 1] == '*': count += 1
        return count
    else:
        return array[a]


code='''
*----*----
-----**---
---*--**--
------****
---*---*--
-*--**---*
----------
----------
--*--*--**
-*---*----'''
C=[]
result = []
code_new = code.replace('\n', '')
for i in range(10):
    C.append([])
    for j in range(10):
        C.append(code_new[i*10+j])
code_array = np.array(C)

for k in range(10):
    for l in range(10):
        result.append(countValue(code_array, k, l, 0))
        
for m in range(10):
    for n in range(10):
        print(result[m*10+n], end='')
    print('\r')

点评

运行提示:11行IndexError: string index out of range  发表于 2018-7-6 21:18
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-5 21:37:48 | 显示全部楼层
  1. m=[
  2. '*----*----',
  3. '-----**---',
  4. '---*--**--',
  5. '------****',
  6. '---*---*--',
  7. '-*--**---*',
  8. '----------',
  9. '----------',
  10. '--*--*--**',
  11. '-*---*----']

  12. for i in range(10):
  13.     for j in range(10):
  14.         if m[i][j]!='*':
  15.             num=0
  16.             for p in range(-1,2):
  17.                 for q in range(-1,2):
  18.                     try:
  19.                         if m[i+q][j+p]=='*' and (i+q)>=0 and (j+p)>=0:
  20.                             num+=1
  21.                     except:
  22.                         pass

  23.             m[i]=m[i][:j]+str(num)+m[i][j+1:]
  24.     print(m[i])
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-6 10:57:42 | 显示全部楼层
本帖最后由 凌九霄 于 2018-7-6 11:17 编辑

这题不做过了么(每日一题第148题)
  1. boom = '''*----*----
  2. -----**---
  3. ---*--**--
  4. ------****
  5. ---*---*--
  6. -*--**---*
  7. ----------
  8. ----------
  9. --*--*--**
  10. -*---*----'''

  11. boomlst = boom.split('\n')
  12. lst = [list(x) for x in boomlst]


  13. def countboom(lst, x, y):
  14.     boom = 0
  15.     if lst[x][y] != '*':
  16.         if (x - 1) >= 0 and (y - 1) >= 0:
  17.             if lst[x - 1][y - 1] == '*':
  18.                 boom += 1
  19.         if (x - 1) >= 0:
  20.             if lst[x - 1][y] == '*':
  21.                 boom += 1
  22.         if (x - 1) >= 0 and (y + 1) <= 9:
  23.             if lst[x - 1][y + 1] == '*':
  24.                 boom += 1
  25.         if (y - 1) >= 0:
  26.             if lst[x][y - 1] == '*':
  27.                 boom += 1
  28.         if (y + 1) <= 9:
  29.             if lst[x][y + 1] == '*':
  30.                 boom += 1
  31.         if (x + 1) <= 9 and (y - 1) >= 0:
  32.             if lst[x + 1][y - 1] == '*':
  33.                 boom += 1
  34.         if (x + 1) <= 9:
  35.             if lst[x + 1][y] == '*':
  36.                 boom += 1
  37.         if (x + 1) <= 9 and (y + 1) <= 9:
  38.             if lst[x + 1][y + 1] == '*':
  39.                 boom += 1
  40.     return boom


  41. for i in range(10):
  42.     for j in range(10):
  43.         if lst[i][j] != '*':
  44.             bc = countboom(lst, i, j)
  45.             if bc == 0:
  46.                 lst[i][j] = '0'
  47.             else:
  48.                 lst[i][j] = str(bc)
  49.                
  50. print('\n数字标注雷区:')
  51. for i in lst:
  52.     for j in i:
  53.         print('{0:^3}'.format(j), end='')
  54.     print()

复制代码


360截图20180706111733761.jpg

点评

抱歉,题目重了。  发表于 2018-7-6 21:20

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-6 10:58:21 | 显示全部楼层
  1. str1 = '''\
  2. *----*----
  3. -----**---
  4. ---*--**--
  5. ------****
  6. ---*---*--
  7. -*--**---*
  8. ----------
  9. ----------
  10. --*--*--**
  11. -*---*----'''
  12. def mis(num):
  13.     m = 0
  14.     if num - 12 < 0:
  15.         if str1[num-1] == '*':
  16.             m += 1
  17.         if str1[num+1] == '*':
  18.             m += 1
  19.         if str1[num+10] == '*':
  20.             m += 1
  21.         if str1[num+11] == '*':
  22.             m += 1
  23.         if str1[num+12] == '*':
  24.             m += 1
  25.         else:
  26.             pass
  27.     elif num - 12 > 0 and num + 12 <= 108:
  28.         if str1[num-12] == '*':
  29.             m += 1
  30.         if str1[num-11] == '*':
  31.             m += 1
  32.         if str1[num-10] == '*':
  33.             m += 1
  34.         if str1[num-1] == '*':
  35.             m += 1
  36.         if str1[num+1] == '*':
  37.             m += 1
  38.         if str1[num+10] == '*':
  39.             m += 1
  40.         if str1[num+11] == '*':
  41.             m += 1
  42.         if str1[num+12] == '*':
  43.             m += 1
  44.         else:
  45.             pass
  46.     elif num >= 99 and num <= 107:
  47.         if str1[num-12] == '*':
  48.             m += 1
  49.         if str1[num-11] == '*':
  50.             m += 1
  51.         if str1[num-10] == '*':
  52.             m += 1
  53.         if str1[num-1] == '*':
  54.             m += 1
  55.         if str1[num+1] == '*':
  56.             m += 1
  57.     elif num == 108:
  58.         if str1[num-12] == '*':
  59.             m += 1
  60.         if str1[num-11] == '*':
  61.             m += 1
  62.         if str1[num-10] == '*':
  63.             m += 1
  64.         if str1[num-1] == '*':
  65.             m += 1
  66.     return (m)

  67. n = 0
  68. if __name__ == '__main__':
  69.     str2 = ''
  70.     for i in str1:
  71.         if i == '\n':
  72.             str2 += '\n'
  73.         elif i == '*':
  74.             str2 += '*'
  75.         else:
  76.             str2 += str(mis(n))
  77.         n += 1
  78.     print(str2)
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-6 13:31:45 | 显示全部楼层
  1. s=[ '*----*----',
  2.     '-----**---',
  3.         '---*--**--',
  4.         '------****',
  5.         '---*---*--',
  6.         '-*--**---*',
  7.         '----------',
  8.         '----------',
  9.         '--*--*--**',
  10.         '-*---*----']

  11. def check(i,j):
  12.         imin,jmin=max(0,i-1),max(0,j-1)
  13.         imax,jmax=min(9,i+1),min(9,j+1)
  14.         return sum(1 for ii in range(imin,imax+1) for jj in range(jmin,jmax+1) if s[ii][jj]=="*")


  15. res=[[0]*10 for i in range(10)]
  16. for i in range(10):
  17.         for j in range(10):
  18.                 res[i][j]="*" if s[i][j]=="*" else str(check(i,j))                       

  19. for i in range(len(res)):
  20.         print(" ".join(res[i]))
复制代码

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-6 15:00:01 | 显示全部楼层
本帖最后由 duliping 于 2018-7-6 16:01 编辑
  1. s = '''*----*----
  2. -----**---
  3. ---*--**--
  4. ------****
  5. ---*---*--
  6. -*--**---*
  7. ----------
  8. ----------
  9. --*--*--**
  10. -*---*----'''

  11. s = s.split('\n')
  12. result = np.zeros((len(s),len(s[0])))

  13. for i in range(len(s)):
  14.     for j in range(len(s[i])):
  15.         if s[i][j] == '*':
  16.             result[i][j] = -1
  17.             # 上、下、左、右、斜上、斜下 +1
  18.             if i - 1 >= 0 and s[i-1][j] != '*':
  19.                 result[i-1][j] += 1
  20.             if i - 1 >= 0 and j - 1 >= 0  and s[i-1][j-1] != '*':
  21.                 result[i-1][j-1] += 1
  22.             if i - 1 >= 0 and j + 1 < len(s[i])  and s[i-1][j+1] != '*':
  23.                 result[i-1][j+1] += 1
  24.             if i + 1 < len(s) and s[i+1][j] != '*':
  25.                 result[i+1][j] += 1
  26.             if i + 1 < len(s)and j - 1 >= 0  and s[i+1][j-1] != '*':
  27.                 result[i+1][j-1] += 1
  28.             if i + 1 < len(s) and j + 1 < len(s[i])  and s[i+1][j+1] != '*':
  29.                 result[i+1][j+1] += 1
  30.             if j - 1 >= 0 and s[i][j - 1] != '*':
  31.                 result[i][j - 1] += 1
  32.             if j + 1 < len(s[i]) and s[i][j + 1] != '*':
  33.                 result[i][j + 1] += 1

  34. for i in range(result.shape[0]):
  35.     for j in range(result.shape[1]):
  36.         if result[i][j] == -1:
  37.             print('*', end=' ')
  38.         else:
  39.             print(int(result[i][j]), end=' ')
  40.     print('\n')
复制代码

点评

差一行:import numpy as np  发表于 2018-7-6 21:22

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-6 17:22:37 | 显示全部楼层
  1. s='''*----*----
  2. -----**---
  3. ---*--**--
  4. ------****
  5. ---*---*--
  6. -*--**---*
  7. ----------
  8. ----------
  9. --*--*--**
  10. -*---*----'''
  11. bombpool={}
  12. bombpool2=''
  13. bomb=0
  14. xcount=0
  15. ycount=0
  16. for each in s:
  17.         if each != '\n':
  18.                 bombpool['x'+str(xcount)+'y'+str(ycount)]=each
  19.                 ycount+=1
  20.         else:
  21.                 xcount+=1
  22.                 ycount=0       
  23. for xcount in range(10):
  24.         for ycount in range(10):
  25.                 if bombpool['x'+str(xcount)+'y'+str(ycount)]=='-':
  26.                         left='x'+str(xcount-1)+'y'+str(ycount)
  27.                         right='x'+str(xcount+1)+'y'+str(ycount)
  28.                         up='x'+str(xcount)+'y'+str(ycount+1)
  29.                         down='x'+str(xcount)+'y'+str(ycount-1)
  30.                         lu='x'+str(xcount-1)+'y'+str(ycount-1)
  31.                         ru='x'+str(xcount-1)+'y'+str(ycount+1)
  32.                         ld='x'+str(xcount+1)+'y'+str(ycount-1)
  33.                         rd='x'+str(xcount+1)+'y'+str(ycount+1)
  34.                         if bombpool.get(left)=='*':
  35.                                 bomb+=1
  36.                         if bombpool.get(right)=='*':
  37.                                 bomb+=1
  38.                         if bombpool.get(up)=='*':
  39.                                 bomb+=1
  40.                         if bombpool.get(down)=='*':
  41.                                 bomb+=1
  42.                         if bombpool.get(lu)=='*':
  43.                                 bomb+=1
  44.                         if bombpool.get(ru)=='*':
  45.                                 bomb+=1       
  46.                         if bombpool.get(ld)=='*':
  47.                                 bomb+=1       
  48.                         if bombpool.get(rd)=='*':
  49.                                 bomb+=1
  50.                         bombpool['x'+str(xcount)+'y'+str(ycount)]=str(bomb)
  51.                 bomb=0
  52. for each in bombpool.values():
  53.         bombpool2+=each
  54.         bomb+=1
  55.         if bomb==10:
  56.                 print(bombpool2)
  57.                 bombpool2=''
  58.                 bomb=0
复制代码

这个方法太繁琐了,还要改进下。。。。

评分

参与人数 1荣誉 +3 鱼币 +3 收起 理由
冬雪雪冬 + 3 + 3

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-8 17:01:56 | 显示全部楼层
本帖最后由 A3122123 于 2018-7-9 12:37 编辑

n =[
'*', '-', '-', '-', '-', '*', '-', '-', '-', '-',
'-', '-', '-', '-', '-', '*', '*', '-', '-', '-',
'-', '-', '-', '*', '-', '-', '*', '*', '-', '-',
'-', '-', '-', '-', '-', '-', '*', '*', '*' ,'*',
'-', '-', '-', '*', '-', '-', '-', '*', '-', '-',
'-', '*', '-', '-', '*', '*', '-', '-', '-', '*',
'-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
'-', '-', '-', '-', '-', '-', '-', '-', '-', '-',
'-', '-', '*', '-', '-', '*', '-', '-', '*', '*',
'-', '*', '-', '-', '-', '*', '-', '-', '-', '-'
]
count1 = -1
count2 = -1
count3 = -1
count4 = -1
for i in n :
    count1 += 1
    if i == '-':
        n.pop(count1)
        n.insert(count1,0)
a = [-11,-1,+9,]
b = [+11,+1,-9,]
for i in n :
    count2 += 1
    if i == '*' :
        for i in a:
            if 0<= count2+i <= 99 and n[count2+i] != '*' and (count2+i)%10 != 9:
                n[count2+i] += 1
        for i in b:
            if 0<= count2+i <= 99 and n[count2+i] != '*' and (count2+i)%10 != 0:
                n[count2+i] += 1
        if 0<= count2+10 <= 99 and n[count2+10] != '*' :
                n[count2+10] += 1
        if 0<= count2-10 <= 99 and n[count2-10] != '*' :
                n[count2-10] += 1  
for i in n :
    count3 += 1
    if i == 0:
        n.pop(count3)
        n.insert(count3,'-')     
for i in n :
    count4 += 1
    print(i,end="  ")
    if count4%10 == 9:
        print("\n")
   
            
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-9 11:38:14 | 显示全部楼层
'''扫雷'''


def target():
    list_target = []
    for x in range(len(list2)):
        for y in range(len(list2[x])):
            if list2[x][y] == '-':
                list_target.append([x, y])
    return list_target

def list_sign(): # 符号位
    sign = []
    for x in [-1, 0, 1]:
        for y in [-1, 0, 1]:
            sign.append([x, y])
    sign.remove([0, 0])
    return sign

list1 = ['*----*----',
         '-----**---',
         '---*--**--',
         '------****',
         '---*---*--',
         '-*--**---*',
         '----------',
         '----------',
         '--*--*--**',
         '-*---*----'
         ]
list2 = []
for x in list1:
    list3 = []
    for y in x:
        list3.append(y)
    list2.append(list3)
list_target = target() # 需要修改的点
sign = list_sign()

for x in list_target: # x为需要修改的点
    count = 0
    for y in sign:
        x_index = x[0] - y[0]
        y_index = x[1] - y[1]
        if x_index < 0 or x_index > 9:
            continue
        elif y_index < 0 or y_index > 9:
            continue
        if list2[x_index][y_index] == '*':
            count += 1
        list2[x[0]][x[1]] = count
print(list2)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-11 13:38:07 | 显示全部楼层
虽然过期了,但是还是练练手参与一下吧

  1. import random

  2. #自定义雷阵大小
  3. booml = int(input('请输入地雷阵的长度:'))
  4. boomh = int(input('请输入地雷阵的高度:'))

  5. #设置雷的理论密度
  6. boom = ['-','*','-']


  7. #随机生成雷阵,并输出
  8. list_boom = [[random.choice(boom) for i in range(booml)] for x in range(boomh)]
  9. for d in list_boom:
  10.     print(d)
  11. print('\n')


  12. #判断‘-’的上下左右含有几个‘*’,并计数,最后替换
  13. for i in range(boomh):
  14.     for n in range(booml):
  15.         numb = 0
  16.         if list_boom[i][n] == '-':
  17.             lt = i-1
  18.             ld = i+2
  19.             ll = n-1
  20.             lr = n+2
  21.             if lt<0:
  22.                 lt = 0
  23.             if ld > boomh:
  24.                 ld = boomh
  25.             if ll<0:
  26.                 ll =0
  27.             if lr > booml:
  28.                 lr = booml

  29.             for u in range(lt,ld):
  30.                 for j in range(ll,lr):
  31.                     
  32.                     if list_boom[u][j] == '*':
  33.                         numb += 1
  34.             #将‘-’替换为统计好的函雷个数
  35.             list_boom[i][n] = str(numb)

  36. #输出雷阵详解
  37. for f in list_boom:
  38.     print(f)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-17 15:22:07 | 显示全部楼层
扫雷程序之前的练习有做过,直接借用了之前大佬的程序。
  1. def replace_matrix(list1, a, b):
  2.     r = len(list1)
  3.     c = len(list1[0])
  4.     for i in range(r):
  5.         for j in range(c):
  6.             if list1[i][j] == a:
  7.                 list1[i][j] = b
  8.     return list1

  9. def Num(l):
  10.     # l为10*10的列表,有雷的位置元素为'M',L为12*12的列表
  11.     L = [['-']*12]*12
  12.     for i in range(12):
  13.         if 1 <= i <= 10:
  14.             L[i] = [0]+l[i-1]+[0]
  15.     for i in range(1, 11):
  16.         for j in range(1, 11):
  17.             if L[i][j] != '*':
  18.                 for k in range(-1, 2):
  19.                     L[i][j] += L[i+k][j-1:j+2].count('*')
  20.                     l[i-1][j-1] = L[i][j]
  21.     return l



  22. l0 = [['*','-','-','-','-','*','-','-','-','-',],['-','-','-','-','-','*','*','-','-','-',],
  23.      ['-','-','-','*','-','-','*','*','-','-',],['-','-','-','-','-','-','*','*','*','*',],
  24.      ['-','-','-','*','-','-','-','*','-','-',],['-','*','-','-','*','*','-','-','-','*',],
  25.      ['-','-','-','-','-','-','-','-','-','-',],['-','-','-','-','-','-','-','-','-','-',],
  26.      ['-','-','*','-','-','*','-','-','*','*',],['-','*','-','-','-','*','-','-','-','-',]]
  27. l = replace_matrix(l0, '-', 0)
  28. l1 = Num(l)
  29. l2 = replace_matrix(l1, 0, '-')

  30. for each in l2:
  31.     print(*each)
复制代码


结果:
* 1 - - 2 * 3 1 - -
1 1 1 1 3 * * 3 1 -
- - 1 * 2 4 * * 4 2
- - 2 2 2 2 * * * *
1 1 2 * 3 3 4 * 5 3
1 * 2 2 * * 2 1 2 *
1 1 1 1 2 2 1 - 1 1
- 1 1 1 1 1 1 1 2 2
1 2 * 1 2 * 2 1 * *
1 * 2 1 2 * 2 1 2 2
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-7-19 22:12:36 | 显示全部楼层
这题148题出过了,一毛一样的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-19 22:14:48 | 显示全部楼层
solomonxian 发表于 2018-7-19 22:12
这题148题出过了,一毛一样的

是的,题目重了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-10-1 13:05:56 | 显示全部楼层
  1. def fun_182(list1):
  2.     list2,list3=[(i,j)for i in range(10) for j in range(10)],[list(str1)for str1 in list1]
  3.     print('before:')
  4.     print('\n'.join(list1))
  5.     num=len(list1)
  6.     for i in range(num):
  7.         for j in range(num):
  8.             if list1[i][j]!='*':
  9.                 x=0
  10.                 (temp1,temp2)=list2[num*i+j]
  11.                 for m in range(temp1-1,temp1+2):
  12.                     for n in range(temp2-1,temp2+2):
  13.                         if m>=0 and m<10 and n>=0 and n<10:
  14.                             if list3[m][n]=='*':
  15.                                 x+=1
  16.                 list3[i][j]=str(x)
  17.     print('after:')
  18.     for each in list3:
  19.         print(''.join(each))
  20. list1=['*----*----','-----**---','---*--**--','------****','---*---*--','-*--**---*','----------','----------','--*--*--**','-*---*----']
  21. fun_182(list1)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-7 15:43:05 | 显示全部楼层
  1. def test182():
  2.     lista = [['*', '-', '-', '-', '-', '*', '-', '-', '-', '-'],
  3.              ['-', '-', '-', '-', '-', '*', '*', '-', '-', '-'],
  4.              ['-', '-', '-', '*', '-', '-', '*', '*', '-', '-'],
  5.              ['-', '-', '-', '-', '-', '-', '*', '*', '*', '*'],
  6.              ['-', '-', '-', '*', '-', '-', '-', '*', '-', '-'],
  7.              ['-', '*', '-', '-', '*', '*', '-', '-', '-', '*'],
  8.              ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'],
  9.              ['-', '-', '-', '-', '-', '-', '-', '-', '-', '-'],
  10.              ['-', '-', '*', '-', '-', '*', '-', '-', '*', '*'],
  11.              ['-', '*', '-', '-', '-', '*', '-', '-', '-', '-']]
  12.     for i in range(len(lista)):
  13.         for j in range(len(lista[i])):
  14.             if lista[i][j] == '-':
  15.                 thisnum = 0
  16.                 for ii in range(-1, 2):
  17.                     for jj in range(-1, 2):
  18.                         if 0 <= i + ii < len(lista) and 0 <= j + jj < len(lista[i]) and lista[i + ii][j + jj] == '*':
  19.                             thisnum += 1
  20.                 lista[i][j] = thisnum if thisnum != 0 else '-'
  21.     for i in lista:
  22.         print(*i)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-24 14:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表