洛谷普及场(广搜)

本文通过P1605迷宫、P1141 01迷宫和P1443 马的遍历等题目,介绍了深度优先搜索(DFS)在解决迷宫问题中的基本思路和技巧。在DFS搜索过程中,需要注意状态的还原以避免死循环。同时,对于特殊走法如象棋马的移动,需要理解其行走规则来正确实现搜索算法。
  • P1605迷宫

dfs搜索基础题,dfs搜索时每次搜索完要记得将转态还原

#include <cstdio>
const int N = 8 ; 
int n , m , t , ans ;
int maze[N][N] ; 
int dir[4][2] = {{-1,0},{0,-1},{1,0},{0,1}} ; 
int a , b , c , d ;
void dfs(int x1 , int y1 , int x2 , int y2){
	int x , y ;
	if (x1 == x2 && y1 == y2){
		ans ++ ; 
		return ; 
	} 
	for (int i = 0 ; i < 4 ; ++i){
		x = x1 + dir[i][0] ; 
		y = y1 + dir[i][1] ; 
		if (x>=1&&x<=n&&y>=1&&y<=m && !maze[x][y]){
			maze[x][y] = 1 ; 
			dfs(x,y,x2,y2) ; 
			maze[x][y] = 0 ;	//还原没走过的转态否则后面的走不了 
		}   
	}
}
int main(){
	scanf ("%d%d%d",&n,&m,&t) ;
	scanf ("%d%d",&a,&b) ;
	scanf ("%d%d",&c,&d) ;  
	int x , y ;
	for (int i = 1 ; i <= t ; ++i){
		scanf ("%d%d",&x,&y) ; 
		maze[x][y] = 1 ; 
	}
	maze[a][b] = 1 ; 
	dfs(a,b,c,d) ; 
	printf ("%d\n",ans) ; 
	return 0 ;  
}
  • P1141 01迷宫

  • 单独开了一篇博客:https://blog.youkuaiyun.com/coder370/article/details/101625931

  • P1443 马的遍历
    普通的搜索题,但是是象棋马的走法,一开始以为就是普通的上下左右(尴尬.jpg)

#include <cstdio>
#include <queue>
using namespace std ; 
const int N = 410 ; 
int dir[8][2] = {{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2},{-2,1},{-1,2}} ; 
int chess[N][N] ;
struct node{
	int x , y ; 
}; 
int n , m ; 
bool vis[N][N] ; 
int step ; 
void bfs(int x , int y){
	vis[x][y] = true ; 
	chess[x][y] = 0 ;
	queue<node> q ;
	node head , next ;
	head.x = x , head.y = y ;  
	q.push(head) ; 
	while(!q.empty()){
		head = q.front() ; 
		q.pop() ;
		for (int i = 0 ; i < 8 ; i ++){
			next.x = head.x + dir[i][0] , next.y = head.y + dir[i][1] ;
			if (next.x>=0&&next.x<n && next.y>=0&&next.y<m && !vis[next.x ][next.y]){
				chess[next.x ][next.y] = chess[head.x][head.y] + 1 ; 
				vis[next.x ][next.y] = true ;
				q.push(next) ; 
			}
		}
	}
	
}
int main(){
	int x , y ;   
	scanf ("%d%d%d%d",&n,&m,&x,&y) ;
	bfs(x-1,y-1) ; 
	for (int i = 0 ; i < n ; ++i){
		for (int j = 0 ; j < m ; ++j){
			if (chess[i][j] == 0){
				if (i == x-1 && j == y-1)
					printf ("%-5d",0) ; 
				else	printf ("%-5d",-1) ; 
			}
			else	printf ("%-5d",chess[i][j]) ; 
		}
		printf ("\n") ; 
	}
	return 0 ; 
}
洛谷是一个广受欢迎的在线编程学习平台,它提供了大量的编程题目,特别是针对NOIP(全国青少年信息学奥林匹克)普及组的比赛训练。对于初学者来说,掌握这些例题的Python实现是提高编程能力的重要途径。以下是一些典型的洛谷普及组例题及其Python实现。 --- ### P1001 A+B Problem 题目要求输入两个整数 `a` 和 `b`,输出它们的和。 ```python a, b = map(int, input().split()) print(a + b) ``` --- ### P1075 质因数分解 题目要求将一个正整数分解为质因数的乘积形式。 ```python n = int(input()) i = 2 while i * i <= n: while n % i == 0: print(i) n //= i i += 1 if n > 1: print(n) ``` --- ### P1914 小书童——凯撒密码 题目要求实现凯撒密码的加密过程,即将每个字母按照给定的偏移量进行移位。 ```python s = input().strip() k = int(input()) result = '' for c in s: if 'a' <= c <= 'z': result += chr((ord(c) - ord('a') + k) % 26 + ord('a')) elif 'A' <= c <= 'Z': result += chr((ord(c) - ord('A') + k) % 26 + ord('A')) else: result += c print(result) ``` --- ### P1028 数的计算 题目要求计算满足条件的数的数量,可以通过递归或动态规划来实现。 ```python n = int(input()) dp = [0] * (n + 1) def count(x): if dp[x] != 0: return dp[x] res = 1 for i in range(1, x // 2 + 1): res += count(i) dp[x] = res return res print(count(n)) ``` --- ### P1035 级数求和 题目要求计算级数 `1 + 1/2 + 1/3 + ... + 1/n` 的前 `n` 项和,直到和大于等于 `k`。 ```python k = int(input()) s = 0.0 n = 0 while s < k: n += 1 s += 1.0 / n print(n) ``` --- ### P1423 小玉在游泳 题目要求计算小玉游过 `n` 米所需的最少时间。 ```python n = float(input()) speed = 2.0 time = 0 while speed <= n: n -= speed speed *= 0.98 time += 1 time += n / speed print(int(time) + (1 if time - int(time) > 0 else 0)) ``` --- ### P1307 数字反转 题目要求将一个整数反转输出。 ```python n = input().strip() if n[0] == '-': print('-' + n[:0:-1].lstrip('0')) else: print(n[::-1].lstrip('0')) ``` --- ### P1980 计算器的改良 题目要求解析一个简单的计算器表达式并输出结果。 ```python expr = input().strip() result = eval(expr) print(result) ``` --- ### P1085 不高兴的津津 题目要求找出津津一周中最不高兴的一天。 ```python max_val = 0 day = 0 for i in range(1, 8): a, b = map(int, input().split()) total = a + b if total > max_val: max_val = total day = i print(day) ``` --- ### P1427 小鱼的数字游戏 题目要求将输入的数字逆序输出。 ```python nums = list(map(int, input().split())) nums.pop() for num in reversed(nums): print(num, end=' ') ``` --- 以上是部分洛谷普及组例题的Python实现。这些题目涵盖了顺序结构、循环结构、字符串处理、递归与动态规划等多个方面,适合初学者逐步掌握编程技巧。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值