贪吃蛇 题解

题目传送门

前言

让我出大模拟的都是 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
*/
相关推荐
2301_764441338 小时前
Aella Science Dataset Explorer 部署教程笔记
笔记·python·全文检索
神仙别闹9 小时前
基于QT(C++)实现学本科教务系统(URP系统)
数据库·c++·qt
派大鑫wink9 小时前
【Java 学习日记】开篇:以日记为舟,渡 Java 进阶之海
java·笔记·程序人生·学习方法
deng-c-f9 小时前
Linux C/C++ 学习日记(49):线程池
c++·学习·线程池
ulias2129 小时前
C++ 的容器适配器——从stack/queue看
开发语言·c++
daidaidaiyu10 小时前
FFmpeg 关键的结构体
c++·ffmpeg
永远都不秃头的程序员(互关)10 小时前
大模型Agent落地实战:从核心原理到工业级任务规划器开发
笔记
欧特克_Glodon10 小时前
C++医学图像处理经典ITK库用法详解<一>:图像输入输出模块功能
c++·图像处理·itk
一个不知名程序员www11 小时前
算法学习入门---priority_queue(C++)
c++·算法
TL滕11 小时前
从0开始学算法——第十八天(分治算法)
笔记·学习·算法