部分题目渐进性分类
http://cs101.openjudge.cn/dsapre/
把 http://xzmdsa.openjudge.cn/ 2023年春季的几次作业陆续加到cs101,其中有些题目在 cs101已经有。
因为缺少图的题目,我们额外增加了 H7: 图应用
H1: Python入门
22359: Goldbach Conjecture
http://cs101.openjudge.cn/dsapre/22359/
02039: 反反复复
http://cs101.openjudge.cn/dsapre/02039/
22548: 机智的股民老张
http://cs101.openjudge.cn/dsapre/22548/
23563: 多项式时间复杂度
http://cs101.openjudge.cn/practice/23563/
H2: 线性表
05345: 位查询
http://cs101.openjudge.cn/dsapre/05345/
05344: 最后的最后
http://cs101.openjudge.cn/dsapre/05344/
05467: 多项式加法
http://cs101.openjudge.cn/dsapre/05467/
07297: 神奇的幻方
http://cs101.openjudge.cn/dsapre/07297/
21006: 放苹果(盘子相同)
http://cs101.openjudge.cn/dsapre/21006/
22068: 合法出栈序列
http://cs101.openjudge.cn/dsapre/22068/
23451: 交互四则运算计算器_带错误表达式版
http://cs101.openjudge.cn/dsapre/23451/
H3: 递归与动态规划
04117: 简单的整数划分问题
http://cs101.openjudge.cn/practice/04117/
02773: 采药
http://cs101.openjudge.cn/practice/02773/
08780: 拦截导弹
http://cs101.openjudge.cn/dsapre/02945/
题目出现在 https://github.com/GMyhf/2020fall-cs101 题集 2020fall_cs101.openjudge.cn_problems.md,
提交Optional problems 部分的 02945: 拦截导弹 的代码直接AC了。
k=int(input())
l=list(map(int,input().split()))
dp=[0]*k
for i in range(k-1,-1,-1):
maxn=1
for j in range(k-1,i,-1):
if l[i]>=l[j] and dp[j]+1>maxn:
maxn=dp[j]+1
dp[i]=maxn
print(max(dp))22636: 修仙之路
http://cs101.openjudge.cn/dsapre/22636/
24375: 小木棍
http://cs101.openjudge.cn/dsapre/24375/
25815: 回文字符串
http://cs101.openjudge.cn/dsapre/25815/
20650: 最长的公共子序列的长度
http://cs101.openjudge.cn/dsapre/20650/
H4: 查找与排序
07745: 整数奇偶排序
http://cs101.openjudge.cn/dsapre/07745/
04143: 和为给定数
http://cs101.openjudge.cn/practice/04143/
04135: 月度开销
http://cs101.openjudge.cn/practice/04135/
09201: Freda的越野跑
http://cs101.openjudge.cn/dsapre/09201/
20741: 两座孤岛最短距离
http://cs101.openjudge.cn/practice/20741/
23568: 幸福的寒假生活
http://cs101.openjudge.cn/practice/23568/
04136: 矩形分割
http://cs101.openjudge.cn/practice/04136/
H5: 树及算法-上
01145:Tree Summing
http://cs101.openjudge.cn/practice/01145/
02255:重建二叉树
http://cs101.openjudge.cn/practice/02255/
02694: 波兰表达式
http://cs101.openjudge.cn/practice/02694/
02788: 二叉树
http://cs101.openjudge.cn/dsapre/02788/
04081: 树的转换
http://cs101.openjudge.cn/practice/04081/
04082: 树的镜面映射
http://cs101.openjudge.cn/practice/04082/
14683: 合并果子
http://cs101.openjudge.cn/dsapre/14683/
H6: 树及算法-下
02756: 二叉树
http://cs101.openjudge.cn/practice/02756/
06646: 二叉树的深度
http://cs101.openjudge.cn/dsapre/06646/
06648: Sequence
http://cs101.openjudge.cn/practice/06648/
01760: Disk Tree
http://cs101.openjudge.cn/practice/01760
04079: 二叉搜索树
http://cs101.openjudge.cn/practice/04079/
04089: 电话号码
http://cs101.openjudge.cn/practice/04089/
05430: 表达式·表达式树·表达式求值
http://cs101.openjudge.cn/practice/05430/
H7: 图应用
05442: 兔子与星空
http://cs101.openjudge.cn/practice/05442/
05443: 兔子与樱花
http://cs101.openjudge.cn/practice/05443/
01178: Camelot
http://cs101.openjudge.cn/practice/01178/
01376: Robot
http://cs101.openjudge.cn/practice/01376/
02049: Finding Nemo
http://cs101.openjudge.cn/practice/02049/
01324: Holedox Moving
http://cs101.openjudge.cn/practice/01324/
During winter, the most hungry and severe time, Holedox sleeps in its lair. When spring comes, Holedox wakes up, moves to the exit of its lair, comes out, and begins its new life. Holedox is a special snake, but its body is not very long. Its lair is like a maze and can be imagined as a rectangle with n*m squares. Each square is either a stone or a vacant place, and only vacant places allow Holedox to move in. Using ordered pair of row and column number of the lair, the square of exit located at (1,1).
Holedox's body, whose length is L, can be represented block by block. And let B1(r1,c1) B2(r2,c2) .. BL(rL,cL) denote its L length body, where Bi is adjacent to Bi+1 in the lair for 1 <= i <=?L-1, and B1 is its head, BL is its tail.
To move in the lair, Holedox chooses an adjacent vacant square of its head, which is neither a stone nor occupied by its body. Then it moves the head into the vacant square, and at the same time, each other block of its body is moved into the square occupied by the corresponding previous block.
For example, in the Figure 2, at the beginning the body of Holedox can be represented as B1(4,1) B2(4,2) B3(3,2)B4(3,1). During the next step, observing that B1'(5,1) is the only square that the head can be moved into, Holedox moves its head into B1'(5,1), then moves B2 into B1, B3 into B2, and B4 into B3. Thus after one step, the body of Holedox locates in B1(5,1)B2(4,1)B3(4,2) B4(3,2) (see the Figure 3).
Given the map of the lair and the original location of each block of Holedox's body, your task is to write a program to tell the minimal number of steps that Holedox has to take to move its head to reach the square of exit (1,1). 
输入
The input consists of several test cases. The first line of each case contains three integers n, m (1<=n, m<=20) and L (2<=L<=8), representing the number of rows in the lair, the number of columns in the lair and the body length of Holedox, respectively. The next L lines contain a pair of row and column number each, indicating the original position of each block of Holedox's body, from B1(r1,c1) to BL(rL,cL) orderly, where 1<=ri<=n, and 1<=ci<=m,1<=i<=L. The next line contains an integer K, representing the number of squares of stones in the lair. The following K lines contain a pair of row and column number each, indicating the location of each square of stone. Then a blank line follows to separate the cases.
The input is terminated by a line with three zeros.
Note: Bi is always adjacent to Bi+1 (1<=i<=L-1) and exit square (1,1) will never be a stone.
输出
For each test case output one line containing the test case number followed by the minimal number of steps Holedox has to take. "-1" means no solution for that case.
样例输入
5 6 4
4 1
4 2
3 2
3 1
3
2 3
3 3
3 4
4 4 4
2 3
1 3
1 4
2 4
4
2 1
2 2
3 4
4 2
0 0 0样例输出
Case 1: 9
Case 2: -1提示
In the above sample case, the head of Holedox can follows (4,1)->(5,1)->(5,2)->(5,3)->(4,3)->(4,2)->(4,1)->(3,1)->(2,1)->(1,1) to reach the square of exit with minimal number of step, which is nine.
来源
Beijing 2002
题目大意:一个蛇,长度为l,要爬到(1,1)点,问你最短的路径是多少。如果不存在最短路径就输出-1
解题思路:BFS+哈希(状态压缩BFS)。将蛇的身体当做状态,记录蛇头位置,然后以蛇头位置开始,向后面计算。比如说第一个样例。
我们约定(0,1)为状态00,(1,0)为状态01,(0,-1)为状态10,(-1,0)为状态11,注意这个部分与后面运算的过程要一一对应。
蛇头位置为(4,1)记录下来,然后考虑第二个位置(4,2),发现两者差别为(0,1),所以状态为00,第三个位置(3,2),与第二个位置差距为(-1,0),所以状态为11,第四个位置(3,1),与第三个位置差距为(0,-1),所以状态为10。
那么把这三个状态组合在一起,就成为状态101100,同样的也可以轻松的按照这个把状态还原为蛇的位置。
判断这个题目只需要考虑是否满足蛇的下个位置:1)不会与当前身体的任何部分相碰撞,2)没有另外一个相同的状态在下个蛇头的位置出现过。在满足这两个条件之后,就可以得到最后的结果了。
# https://www.cnblogs.com/wiklvrain/p/8179443.html
from collections import deque
# Constants for the maximum grid size
maxn = 21
# Directions representing right, down, left, up
dir = [(0, 1), (1, 0), (0, -1), (-1, 0)]
# Function to judge if a move is valid
def judge(p, t, l):
a, b = p[0], p[1]
row, col = a + dir[t][0], b + dir[t][1]
if row == a and col == b:
return False
k = l - 1
while k:
q = p[2] & 3
p = (p[0], p[1], p[2] >> 2)
nx, ny = a + dir[q][0], b + dir[q][1]
if nx == row and ny == col:
return False
a, b = nx, ny
k -= 1
return True
# BFS function to find the shortest path for the snake
def bfs(s, n, m, l, g):
q = deque()
vis = [[[0] * (1 << 14) for _ in range(maxn)] for _ in range(maxn)]
q.append(s)
vis[s[0]][s[1]][s[2]] = 1
while q:
p = q.popleft()
if p[0] == 1 and p[1] == 1:
return vis[p[0]][p[1]][p[2]] - 1
for i in range(4):
nx, ny = p[0] + dir[i][0], p[1] + dir[i][1]
st = (p[2] & ((1 << (2 * (l - 2))) - 1)) << 2
st |= (i + 2) % 4
if 1 <= nx <= n and 1 <= ny <= m and not vis[nx][ny][st] and not g[nx][ny] and judge(p, i, l):
vis[nx][ny][st] = vis[p[0]][p[1]][p[2]] + 1
q.append((nx, ny, st))
return -1
def main():
cas = 1
while True:
n, m, l = map(int, input().split())
if n == 0 and m == 0 and l == 0:
break
# Initialize the snake
ss = (0, 0, 0)
tmp1, tmp2 = 0, 0
for i in range(l):
a, b = map(int, input().split())
if i == 0:
ss = (a, b, 0)
else:
for j in range(4):
nx = tmp1 + dir[j][0]
ny = tmp2 + dir[j][1]
if nx == a and ny == b:
ss = (ss[0], ss[1], ss[2] | (j << (2 * (i - 1))))
break
tmp1, tmp2 = a, b
# Read obstacles
k = int(input())
g = [[0] * maxn for _ in range(maxn)]
#for _ in range(k):
while k:
try:
a, b = map(int, input().split())
except ValueError:
continue
k -= 1
g[a][b] = 1
# Perform BFS
result = bfs(ss, n, m, l, g)
print(f"Case {cas}: {result}")
cas += 1
input()
if __name__ == "__main__":
main()