贪吃蛇 题解

题目传送门

前言

让我出大模拟的都是 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
*/
相关推荐
敢敢J的憨憨L14 分钟前
GPTL(General Purpose Timing Library)使用教程
java·服务器·前端·c++·轻量级计时工具库
小欣加油16 分钟前
leetcode 62 不同路径
c++·算法·leetcode·职场和发展
让我们一起加油好吗1 小时前
【C++】封装红黑树模拟实现 set 和 map
linux·c++·set·map·红黑树
hsjkdhs1 小时前
C++之类的继承与派生
开发语言·c++
冷徹 .1 小时前
2024ICPC区域赛香港站
数据结构·c++·算法
沐怡旸1 小时前
【底层机制】std:: function 解决的痛点?是什么?如何实现?如何正确用?
c++·面试
浅川.252 小时前
xtuoj string
开发语言·c++·算法
Cathy Bryant2 小时前
球极平面投影
经验分享·笔记·数学建模
Larry_Yanan3 小时前
QML学习笔记(三十一)QML的Flow定位器
java·前端·javascript·笔记·qt·学习·ui
The_Killer.3 小时前
近世代数(抽象代数)详细笔记--环(也有域的相关内容)
笔记·学习·抽象代数·