Skip to content

Instantly share code, notes, and snippets.

@liruqi
Created August 28, 2012 15:39
Show Gist options
  • Save liruqi/3499215 to your computer and use it in GitHub Desktop.
Save liruqi/3499215 to your computer and use it in GitHub Desktop.
一个4x4的矩阵,从左上到右下走,可以左右上下走,但是不能走走过的点,有多少种走法?
gCnt = 0
def main():
squre = []
for i in range(4):
squre.append([0,0,0,0])
print squre
def dfs(x,y,d):
if (x==3 and y==3) :
global gCnt
gCnt = gCnt+1
print 'found ', gCnt
return
squre[x][y] = d
for delta in [[0,1],[0,-1],[1,0],[-1,0]]:
xx = x + delta[0]
yy = y + delta[1]
if (xx<0 or xx>=4):continue
if (yy<0 or yy>=4):continue
if (squre[xx][yy]): continue
dfs(xx,yy,d+1)
squre[x][y] = 0
dfs(0,0,1)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment