试题 算法提高 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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升