贪吃蛇 题解

题目传送门

前言

让我出大模拟的都是 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
*/
相关推荐
ZZZ_O^O30 分钟前
二分查找算法——寻找旋转排序数组中的最小值&点名
数据结构·c++·学习·算法·二叉树
小飞猪Jay3 小时前
C++面试速通宝典——13
jvm·c++·面试
rjszcb4 小时前
一文说完c++全部基础知识,IO流(二)
c++
小字节,大梦想4 小时前
【C++】二叉搜索树
数据结构·c++
吾名招财5 小时前
yolov5-7.0模型DNN加载函数及参数详解(重要)
c++·人工智能·yolo·dnn
我是哈哈hh5 小时前
专题十_穷举vs暴搜vs深搜vs回溯vs剪枝_二叉树的深度优先搜索_算法专题详细总结
服务器·数据结构·c++·算法·机器学习·深度优先·剪枝
憧憬成为原神糕手5 小时前
c++_ 多态
开发语言·c++
郭二哈5 小时前
C++——模板进阶、继承
java·服务器·c++
m0_689618285 小时前
水凝胶发生器,不对称设计妙,医电应用前景广
笔记
Ace'5 小时前
每日一题&&学习笔记
笔记·学习