试题 算法提高 Cat And Mouse

2022/2/27 12:51:32

本文主要是介绍试题 算法提高 Cat And Mouse,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

比结果大一,但是基本步骤没错,我麻了,只能强行减一了,不过能过蓝桥杯

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
char map[20][20];
//0北,1东,2南,3,西;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};//方位向量
int cnt;

struct {//x,y,方向
	int x, y;
	int dir;
} cat, mouse;

void change(int &x, int &y, int &f) {//移动
	for (int i = 0; i < 4; i ++) {//四个方向
		int xx = x + dx[i];
		int yy = y + dy[i];
		if ((map[xx][yy] == '*' || xx > 10 || yy > 10 || xx < 1 || yy < 1) && f == i)//特殊情况,遇到'*',或者到达边界;
		{
			f = (f + 1) % 4;//改变方向
			if (i == 3 && f == 0) //特判
			{
				x += -1, y += 0;
				break;
			}
		} else if (f == i) {//方向确认返回
			x = xx;
			y = yy;
			break;
		}
	}
}

void cat_mouse_game(char map[20][20], int cx, int cy, int cf, int mx, int my, int mf){
	if (cat.x == mouse.x && cat.y == mouse.y) {//退出条件
		cout << cnt - 1;
		return ;
	}
	change(cat.x, cat.y, cat.dir);//

	


这篇关于试题 算法提高 Cat And Mouse的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程