贪吃蛇 题解

题目传送门

前言

让我出大模拟的都是 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
*/
相关推荐
小欣加油29 分钟前
leetcode 面试题01.02判定是否互为字符重排
数据结构·c++·算法·leetcode·职场和发展
王璐WL40 分钟前
【c++】c++第一课:命名空间
数据结构·c++·算法
aramae1 小时前
C++ -- 模板
开发语言·c++·笔记·其他
小马学嵌入式~1 小时前
嵌入式 SQLite 数据库开发笔记
linux·c语言·数据库·笔记·sql·学习·sqlite
hour_go2 小时前
用户态与内核态的深度解析:安全、效率与优化之道
笔记·操作系统
MChine慕青3 小时前
顺序表与单链表:核心原理与实战应用
linux·c语言·开发语言·数据结构·c++·算法·链表
摇滚侠3 小时前
Vue3入门到实战,最新版vue3+TypeScript前端开发教程,笔记03
javascript·笔记·typescript
岑梓铭4 小时前
考研408《计算机组成原理》复习笔记,第六章(1)——总线概念
笔记·考研·408·计算机组成原理·计组
Suckerbin4 小时前
digitalworld.local: TORMENT
笔记·安全·web安全·网络安全
骄傲的心别枯萎4 小时前
RV1126 NO.16:通过多线程同时获取H264和H265码流
linux·c++·音视频·rv1126