贪吃蛇 题解

题目传送门

前言

让我出大模拟的都是 shaby !!!

先在这里道个歉,题面确实挺屎的,但是这个锅还是让 hb 来背好了

反正是他说的让我们题面复杂点的

还有这个数据是真的很难造啊

看这篇题解的前提条件

你要充分读题,搞明白蛇是怎么走的

长度短的蛇会后空翻 指的是因为尾巴先走再头走,所以长度为 2 2 2 的蛇是可以直接掉头的

正文

讲真我感觉这个真没什么好讲的,都搞这么久竞赛这都不会那马力是有点太弱了,该练练了

搞两个数组,记录当前位置有没有蛇的身体,还有一个记录蛇的走向,然后就没了。

讲几个易错点

看一下自己变量名有没有打错了

看一下每一步操作的顺序

一些记录数据的地方也要看一下会不会因为顺序出问题

结语

其实没有什么结语

感觉你们这一届很强,rp++;

还有保护好肠胃。

Code

cpp 复制代码
/*
t <= 1e6
n <= 1000,m <= 1000
*/
#include<bits/stdc++.h>
const int N = 1100;
const int M = 1e6+10;
#define fi first
#define se second
using namespace std;
int n,m;
int mp[N][N];
int dirx[N][N],diry[N][N];
char op[M];
int q,p;
pair<int,int> food[M];
int hx,hy,tx,ty,cntf = 1,dx,dy; 
int main(){
//	freopen("snake10.in","r",stdin);
//	freopen("snake10.out","w",stdout);
	cin >> n >> m >> q >> p;
	for(int i = 1;i <= q; i++){
		int t;
		char x;
		cin >> t >> x;
		op[t] = x;
	}
	for(int i = 1; i <= p; i++) cin >> food[i].fi >> food[i].se;
	cin >> hx >> hy;
//	cout << hx << " " << hy << endl;
	tx = hx;
	ty = hy-1;
	dirx[hx][hy] = 0;
	dirx[tx][ty] = 0;
	diry[hx][hy] = 1;
	diry[tx][ty] = 1;
	dx = 0,dy = 1;
	int t = 0;
	bool flag = 0;
	while(++t){
		if(hx == food[cntf].fi && hy == food[cntf].se) cntf++,flag = 1;
//		cout << cntf ;
//		break;
//		cout << tx << " " << ty << " " << hx << " " << hy  << " " << dx << " " << dy<< endl;
		if(!flag){
			mp[tx][ty] = 0;
			int xx = dirx[tx][ty];
			int yy = diry[tx][ty];
			tx += xx;
			ty += yy;
		}
		if(tx <= 0) tx = n;
		if(tx > n) tx = 1;
		if(ty <= 0) ty = m;
		if(ty > m) ty = 1;
		flag = 0;
		if(op[t] == 'D') dx = 1,dy = 0;
		if(op[t] == 'U') dx = -1,dy = 0;
		if(op[t] == 'R') dx = 0,dy = 1;
		if(op[t] == 'L') dx = 0,dy = -1;
		dirx[hx][hy] = dx;
		diry[hx][hy] = dy;
		hx += dx;
		hy += dy;
		if(hx <= 0) hx = n;
		if(hx > n) hx = 1;
		if(hy <= 0) hy = m;
		if(hy > m) hy = 1;
//		cout << t << ": " << tx << " " << ty << "xxx" << hx << " " << hy << endl; 
		if(mp[hx][hy]) break;
		mp[hx][hy] = 1;
		if(t > 1e6){
			cout << "-1 " << cntf+1 << "\n";
			return 0;
		}
//		 if(t >= 4970)cout << tx << " " << ty << " xxx "<< hx << " " << hy << endl;
//		if(t <= 2500) printf("%d %d\n",hx,hy);
//		 if(t >= 44560 && t <= 44800 && t % 14 == 0) cout << hx << " " << hy << endl;
	}
//	cout << hx << " " << hy << " " << tx << " " << ty << endl;
	cout << t << " " << cntf+1 << "\n";
	return 0;
}
/*
1 3 1 2
3 L
1 1
1 2
1 1
*/
相关推荐
一个不知名程序员www2 分钟前
算法学习入门---二分查找(C++)
c++·算法
2301_8079973827 分钟前
代码随想录-day26
数据结构·c++·算法·leetcode
闭着眼睛学算法36 分钟前
【双机位A卷】华为OD笔试之【排序】双机位A-银行插队【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·javascript·c++·python·算法·华为od
TL滕37 分钟前
从0开始学算法——第一天(认识算法)
数据结构·笔记·学习·算法
小欣加油39 分钟前
leetcode 3318 计算子数组的x-sum I
c++·算法·leetcode·职场和发展
YuforiaCode44 分钟前
神领物流v2.0-day3-运费微服务笔记(个人记录、含练习答案、仅供参考)
笔记
zhangrelay2 小时前
如何使用AI快速编程实现标注ROS2中sensor_msgs/msg/Image图像色彩webots2025a
人工智能·笔记·opencv·学习·计算机视觉·机器人视觉
j_xxx404_2 小时前
C++ STL:list|了解list|相关接口|相关操作
开发语言·c++
m0_598250002 小时前
电源完整性07-如何确定PDN网络中的大电容
笔记·单片机·嵌入式硬件·硬件工程
kyle~2 小时前
机器视觉---Intel RealSense SDK 2.0 开发流程
运维·c++·windows·深度相机·intel realsense