B - Red and Black+dfs

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int w,h;
char f[21][21];
int k,kk; 


int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};




int main()
{

int dfs(int r,int c);


int i,j;
while(scanf("%d%d",&w,&h))
{




getchar();
if (w==0 && h==0 ) break;



memset(f,0,sizeof(f)); 
 


int startx,starty;

for (i=1;i<=h;i++)
{
for (j=1;j<=w;j++)
{
scanf("%c",&f[i][j]);
if (f[i][j]=='@') {startx=i; starty=j;f[i][j]=1;}


if (f[i][j]=='#') { f[i][j]=1;}




}
getchar();
}



k=1; 
dfs(startx,starty);
 



printf("%d\n",k);


 

}

return 0;





int is_board(int r, int c)
{

if ( r>=1 && r<=h && c>=1 && c<=w) return 1;
return 0;
}




int dfs(int r,int c)

 
int i;
for (i=0;i<4;i++)
{
if (  is_board(r+dx[i],c+dy[i]) && f[r+dx[i]][c+dy[i]]=='.' )
{
f[r+dx[i]][c+dy[i]]=1;
 
k++;

dfs(r+dx[i],c+dy[i]); 
 

}
}


  









return 0;
}
好的,下面是一份基于深度优先搜索算法实现的AI贪吃蛇代码: ```python import pygame import random # 设置游戏场地大小 SIZE = 20 WIDTH = 800 HEIGHT = 600 ROWS = HEIGHT // SIZE COLUMNS = WIDTH // SIZE # 定义颜色 WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 定义方向 UP = (0, -1) DOWN = (0, 1) LEFT = (-1, 0) RIGHT = (1, 0) # 初始化pygame pygame.init() # 创建游戏窗口 screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('AI贪吃蛇') # 设置游戏时钟 clock = pygame.time.Clock() class Snake: def __init__(self): self.head = [COLUMNS // 2, ROWS // 2] self.body = [self.head, [self.head[0], self.head[1] + 1], [self.head[0], self.head[1] + 2]] self.direction = UP self.score = 0 def move(self, apple): # 判断新的蛇头位置是否合法 new_head = [self.head[0] + self.direction[0], self.head[1] + self.direction[1]] if new_head[0] < 0 or new_head[0] >= COLUMNS or new_head[1] < 0 or new_head[1] >= ROWS or new_head in self.body: return False self.head = new_head self.body.insert(0, self.head) # 如果吃到苹果,增加分数并生成新的苹果 if self.head == apple: self.score += 1 return True else: self.body.pop() return False class Apple: def __init__(self): self.position = [random.randint(0, COLUMNS - 1), random.randint(0, ROWS - 1)] def draw(self): pygame.draw.rect(screen, RED, (self.position[0] * SIZE, self.position[1] * SIZE, SIZE, SIZE)) def dfs(snake, apple, visited): # 如果找到苹果,返回路径 if snake.head == apple.position: return [] # 遍历所有可能的方向 for direction in [UP, DOWN, LEFT, RIGHT]: new_head = [snake.head[0] + direction[0], snake.head[1] + direction[1]] if new_head[0] < 0 or new_head[0] >= COLUMNS or new_head[1] < 0 or new_head[1] >= ROWS or new_head in snake.body or new_head in visited: continue # 尝试以该方向继续搜索 new_snake = Snake() new_snake.head = new_head new_snake.body = [new_head] + snake.body[:-1] new_snake.direction = direction visited.append(new_head) path = dfs(new_snake, apple, visited) # 如果找到了一条路径,返回路径 if path is not None: return [new_head] + path # 没有找到路径,返回空 return None def update_screen(snake, apple): # 绘制背景 screen.fill(BLACK) # 绘制苹果和蛇 apple.draw() for body_part in snake.body: pygame.draw.rect(screen, GREEN, (body_part[0] * SIZE, body_part[1] * SIZE, SIZE, SIZE)) # 绘制分数 font = pygame.font.SysFont('arial', 30) text = font.render('Score: ' + str(snake.score), True, WHITE) screen.blit(text, (10, 10)) # 更新屏幕 pygame.display.update() def main(): snake = Snake() apple = Apple() while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() # 计算下一步方向 visited = [] path = dfs(snake, apple, visited) if path is not None: next_direction = [path[0][0] - snake.head[0], path[0][1] - snake.head[1]] snake.direction = next_direction # 移动蛇 if not snake.move(apple.position): break # 生成新的苹果 apple = Apple() # 更新屏幕 update_screen(snake, apple) # 设置游戏时钟 clock.tick(10) # 显示游戏结束信息 font = pygame.font.SysFont('arial', 50) text = font.render('Game Over!', True, WHITE) screen.blit(text, (WIDTH // 2 - text.get_width() // 2, HEIGHT // 2 - text.get_height() // 2)) pygame.display.update() pygame.time.wait(2000) # 退出游戏 pygame.quit() quit() if __name__ == '__main__': main() ``` 该代码使用深度优先搜索算法计算下一步的方向,以使蛇能够吃到苹果。搜索过程中,如果找到了一条路径,就返回该路径,并将蛇的方向设置为该路径的第一个方向。如果找不到路径,就继续尝试其他方向,直到所有方向都尝试完毕。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值