`
Mr_Lian
  • 浏览: 1452 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

python深搜&回朔

阅读更多
好久没有做题了,以前都是用C++做ACM题目,但是自从发现python,发现python写算法更优雅。
所以以后决定一有时间就坚决要来做做题。因为ZOJ的OJ支持python语言的,所以决定选择ZOJ了。
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=2
这道题目比较简单,这里就不做解释了,直接贴源码了!

def init():
	for i in range(0,int(n)):
		row = []
		x = raw_input()
		for i in range(0,len(x)):
			if x[i] == 'X':
				row.append(2)
			else:
				row.append(0)
		map.append(row);
	DFS()

def find(x,y):
	for i in range(y,-1,-1):
		if(map[x][i] == 1):
			return 0
		if(map[x][i] == 2):
			break
	for i in range(y,int(n)):
		if(map[x][i] == 1):
			return 0
		if(map[x][i] == 2):
			break
	for i in range(x,-1,-1):
		if(map[i][y] == 1):
			return 0
		if(map[i][y] == 2):
			break;
	for i in range(x,int(n)):
		if(map[i][y] == 1):
			return 0
		if(map[i][y] == 2):
			break
	return 1
		
def DFS():
	global count
	global number
	if(count >= number):
		number = count
	for i in range(0,int(n)):
		for j in range(0,int(n)):
			if((not map[i][j]) and find(i,j)):
				map[i][j] = 1
				count = count + 1
#				print count
				DFS()
				map[i][j] = 0
				count = count - 1
	return count
map = []
count = 0
number = 0
n = 0
if __name__ == "__main__":
	while(1):
		count = 0
		number = 0
		map = []
		n = raw_input()
		if(int(n) == 0):
			break
		else:	
			init()
			print number
		

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics