虫食算 90

本文通过一个具体的递归回溯算法实例,展示了如何解决一类特定问题的方法。该算法利用了深度优先搜索的思想,逐步构建解并进行验证,当发现已构建的部分解无法得到完整解时则撤销部分步骤,尝试其他路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<cstring>
#include <cstdio>
#include <stdlib.h>
using namespace std;


int n;
 char s[4][30];
 bool used[30];
 int f[30];
 
 int id(char x)
 {
  return x-'A'+1;
 }


 


 void dfs(int x,int y,int z)
 {
 
if(x==0)
{
if(z==0)
{
for(int i=1;i<n;i++)
{
cout<<f[i]<<' ';
}
cout<<f[n];
exit(0);
}
return ;
}


if(f[id(s[y][x])]==-1)
{
for(int i=n-1;i>=0;i--)
{
if(!used[i])
{
if(y!=3)
{

f[id(s[y][x])]=i;
used[i]=true;
dfs(x,y+1,z);
f[id(s[y][x])]=-1;
used[i]=false;
}
else
{
int w=f[id(s[1][x])]+f[id(s[2][x])]+z;

if(w%n!=i)
continue;

used[i]=1;f[id(s[3][x])]=i;

dfs(x-1,1,w/n) ;

used[i]=0;
f[id(s[3][x])]=-1;

}
}
}




}
else
{

if(y!=3)
{
dfs(x,y+1,z);
}
else
{
int w=f[id(s[1][x])]+f[id(s[2][x])]+z;

if(f[id(s[3][x])]!=w%n)
return ;

dfs(x-1,1,w/n);
}
}



 
 }
int main()
{
cin>>n;
// cout<<id('A');
cin>>s[1]+1;
cin>>s[2]+1;
cin>>s[3]+1;

memset(f,-1,sizeof(f));
dfs(n,1,0);
return 0;
 } 
Tkinter是Python的一个标准GUI库,可以用于创建图形用户界面。贪吃程序是一个经典的小游戏,玩家控制一个子在屏幕上移动并吃掉物,每吃到一个物,子的身体会变长一节。下面是一个简单的Tkinter贪吃程序的编写示例: ```python import tkinter as tk import random class SnakeGame(tk.Canvas): def __init__(self, master): super().__init__(master, width=400, height=400, background="black") self.snake_body = [(100, 100), (90, 100), (80, 100)] self.food = self.create_food() self.direction = "Right" self.bind_all("<Key>", self.on_key_press) self.score = 0 self.speed = 150 self.game_loop() def create_food(self): while True: x = random.randint(1, 39) * 10 y = random.randint(1, 39) * 10 if (x, y) not in self.snake_body: return self.create_oval(x, y, x+10, y+10, fill="red") def move(self): head_x, head_y = self.snake_body[0] if self.direction == "Up": new_head = (head_x, head_y - 10) elif self.direction == "Down": new_head = (head_x, head_y + 10) elif self.direction == "Left": new_head = (head_x - 10, head_y) else: new_head = (head_x + 10, head_y) self.snake_body.insert(0, new_head) if self.food in self.snake_body: self.score += 1 self.delete(self.food) self.food = self.create_food() if self.score % 5 == 0: self.speed -= 10 else: self.delete(self.snake_body.pop()) if (new_head[0] < 0 or new_head[0] >= 400 or new_head[1] < 0 or new_head[1] >= 400 or new_head in self.snake_body[1:]): self.game_over() else: self.create_rectangle(new_head[0], new_head[1], new_head[0]+10, new_head[1]+10, fill="green") def game_loop(self): self.move() if not self.game_over_flag: self.after(self.speed, self.game_loop) def on_key_press(self, event): keysyms = event.keysym.lower() if keysyms in ["up", "down", "left", "right"]: opposite_directions = {"Up": "Down", "Down": "Up", "Left": "Right", "Right": "Left"} if keysyms != opposite_directions[self.direction]: self.direction = keysyms def game_over(self): self.delete(tk.ALL) self.create_text(self.winfo_width() / 2, self.winfo_height() / 2, text=f"Game Over! Score: {self.score}", fill="white", font=("Courier", 20), justify=tk.CENTER) self.game_over_flag = True root = tk.Tk() root.title("Snake Game") root.resizable(False, False) game = SnakeGame(root) game.pack() root.mainloop() ``` 这个示例代码使用了Tkinter的Canvas组件来创建游戏界面,通过继承Canvas类并重写相关方法来实现贪吃游戏的逻辑。玩家可以通过方向键控制子的移动方向,当子吃到物时,得分增加并且速度加快。当子碰到边界或者自己的身体时,游戏结束。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值