A - Platforms Jumping

探讨如何在宽度为n的河流上,利用m个木板,每次跳跃距离为1到d,从左岸0到达右岸n+1的策略。文章分析了在不改变木板相对顺序的情况下,如何移动木板以确保能够通过河流。

There is a river of width nn. The left bank of the river is cell 00 and the right bank is cell n+1n+1 (more formally, the river can be represented as a sequence of n+2n+2 cells numbered from 00 to n+1n+1). There are also mm wooden platforms on a river, the ii-th platform has length cici (so the ii-th platform takes cici consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed nn.

You are standing at 00 and want to reach n+1n+1 somehow. If you are standing at the position xx, you can jump to any position in the range [x+1;x+d][x+1;x+d]. However you don’t really like the water so you can jump only to such cells that belong to some wooden platform. For example, if d=1d=1, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells 00 and n+1n+1 belong to wooden platforms.

You want to know if it is possible to reach n+1n+1 from 00 if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms.

Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping).

For example, if n=7n=7, m=3m=3, d=2d=2 and c=[1,2,1]c=[1,2,1], then one of the ways to reach 88 from 00 is follow:

The first example: n=7n=7.
Input
The first line of the input contains three integers nn, mm and dd (1≤n,m,d≤1000,m≤n1≤n,m,d≤1000,m≤n) — the width of the river, the number of platforms and the maximum distance of your jump, correspondingly.

The second line of the input contains mm integers c1,c2,…,cmc1,c2,…,cm (1≤ci≤n,∑i=1mci≤n1≤ci≤n,∑i=1mci≤n), where cici is the length of the ii-th platform.

Output
If it is impossible to reach n+1n+1 from 00, print NO in the first line. Otherwise, print YES in the first line and the array aa of length nn in the second line — the sequence of river cells (excluding cell 00 and cell n+1n+1).

If the cell ii does not belong to any platform, aiai should be 00. Otherwise, it should be equal to the index of the platform (11-indexed, platforms are numbered from 11 to mm in order of input) to which the cell ii belongs.

Note that all aiai equal to 11 should form a contiguous subsegment of the array aa of length c1c1, all aiai equal to 22 should form a contiguous subsegment of the array aa of length c2c2, …, all aiai equal to mm should form a contiguous subsegment of the array aa of length cmcm. The leftmost position of 22 in aa should be greater than the rightmost position of 11, the leftmost position of 33 in aa should be greater than the rightmost position of 22, …, the leftmost position of mm in aa should be greater than the rightmost position of m−1m−1.

See example outputs for better understanding.

Examples
Input
7 3 2
1 2 1
Output
YES
0 1 0 2 2 0 3
Input
10 1 11
1
Output
YES
0 0 0 0 0 0 0 0 0 1
Input
10 1 5
2
Output
YES
0 0 0 0 1 1 0 0 0 0
Note
Consider the first example: the answer is [0,1,0,2,2,0,3][0,1,0,2,2,0,3]. The sequence of jumps you perform is 0→2→4→5→7→80→2→4→5→7→8.

Consider the second example: it does not matter how to place the platform because you always can jump from 00 to 1111.

Consider the third example: the answer is [0,0,0,0,1,1,0,0,0,0][0,0,0,0,1,1,0,0,0,0]. The sequence of jumps you perform is 0→5→6→110→5→6→11.

题意:
给你m个木板,问你怎么摆放能过河;每次可以跳1—d下;

思路:
每次跳最大的距离d,看是否可以过河;还要考虑一种情况,过程中是否能摆放下所有的木板,如果不能,就应该跳小于d的距离;

代码:

#include"stdio.h"
int a[1010],c[1010];
int main()
{
	int n,m,d,s=0,l=0,i,j,t;
	scanf("%d %d %d",&n,&m,&d);
	for(i=1;i<=m;i++)
	{
		scanf("%d",&c[i]);
		s+=c[i];
	}
	for(i=1;i<=m;i++)
	{
		s-=c[i];
		if(n-(l+d+c[i]-1)>=s)
		{
			l+=d;
			a[l]=i;
			for(j=1;j<c[i];j++)
			{
				l++;
				a[l]=i;
			}
		}
		else
		{
			t=1;
			for(j=d-1;j>=1;j--)
			{
				if(n-(l+j+c[i]-1)>=s)
				{
					t=j;
					break;
				}
			}
			l+=t;
			a[l]=i;
			for(j=1;j<c[i];j++)
			{
				l++;
				a[l]=i;
			}
		}
	}
	if(l+d<n+1)//再跳一下看是否能到达对岸
	printf("NO");
	else
	{
		printf("YES\n");
		for(i=1;i<=n;i++)
		printf("%d ",a[i]);
	}
	return 0;
} 
#include <GL/freeglut.h> #include <cmath> #include <vector> #include <ctime> #include <cstdlib> #include <string> // 游戏常量 const float GRAVITY = -0.001f; const float JUMP_FORCE_MULTIPLIER = 0.015f; const float MAX_JUMP_FORCE = 0.6f; const float MIN_JUMP_FORCE = 0.1f; // 游戏状态 struct GameState { struct Platform { float x, z; float size; float height; float r, g, b; }; struct Player { float x, y, z; float velocityY; bool isJumping; float jumpForce; float chargeTime; }; Player player; std::vector<Platform> platforms; int currentPlatform; bool isCharging; int score; bool gameOver; }; GameState game; // 初始化游戏 void initGame() { // 初始化玩家 game.player = { 0, 0.5f, 0, 0, false, 0, 0 }; game.currentPlatform = 0; game.isCharging = false; game.score = 0; game.gameOver = false; // 清空平台 game.platforms.clear(); // 创建初始平台 game.platforms.push_back({ 0, 0, 2.0f, 0.5f, 0.2f, 0.6f, 0.3f }); // 创建随机平台 srand(time(0)); for (int i = 1; i < 20; ++i) { float x = game.platforms[i - 1].x + (rand() % 5 + 3) * (rand() % 2 ? 1 : -1); float z = game.platforms[i - 1].z + (rand() % 5 + 3) * (rand() % 2 ? 1 : -1); float size = 1.5f + (rand() % 100) * 0.01f; float height = 0.3f + (rand() % 100) * 0.01f; game.platforms.push_back({ x, z, size, height, (rand() % 100) / 100.0f, (rand() % 100) / 100.0f, (rand() % 100) / 100.0f }); } } // 绘制立方体 void drawCube(float x, float y, float z, float size, float r, float g, float b) { glPushMatrix(); glTranslatef(x, y, z); glColor3f(r, g, b); float s = size / 2.0f; glBegin(GL_QUADS); // 前面 glVertex3f(-s, -s, s); glVertex3f(s, -s, s); glVertex3f(s, s, s); glVertex3f(-s, s, s); // 后面 glVertex3f(-s, -s, -s); glVertex3f(-s, s, -s); glVertex3f(s, s, -s); glVertex3f(s, -s, -s); // 左面 glVertex3f(-s, -s, s); glVertex3f(-s, s, s); glVertex3f(-s, s, -s); glVertex3f(-s, -s, -s); // 右面 glVertex3f(s, -s, s); glVertex3f(s, -s, -s); glVertex3f(s, s, -s); glVertex3f(s, s, s); // 上面 glVertex3f(-s, s, s); glVertex3f(s, s, s); glVertex3f(s, s, -s); glVertex3f(-s, s, -s); // 下面 glVertex3f(-s, -s, s); glVertex3f(-s, -s, -s); glVertex3f(s, -s, -s); glVertex3f(s, -s, s); glEnd(); glPopMatrix(); } // 绘制玩家 void drawPlayer() { glPushMatrix(); glTranslatef(game.player.x, game.player.y, game.player.z); glColor3f(1.0f, 0.0f, 0.0f); // 红色玩家 // 简单的人物表示 - 一个小立方体 glutSolidCube(0.3f); // 如果正在蓄力,绘制蓄力指示器 if (game.isCharging) { glPushMatrix(); glTranslatef(0, 0.5f, 0); glColor3f(1.0f, 1.0f, 0.0f); glutWireSphere(game.player.jumpForce * 2.0f, 10, 10); glPopMatrix(); } glPopMatrix(); } // 显示函数 void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // 设置固定视角的相机 - 从斜上方观察 gluLookAt( 0.0f, 10.0f, 10.0f, // 相机位置 (固定在正上方偏后) 0.0f, 0.0f, 0.0f, // 观察点 (场景中心) 0.0f, 1.0f, 0.0f // 上向量 ); // 绘制地面网格 glColor3f(0.5f, 0.5f, 0.5f); glBegin(GL_LINES); for (int i = -50; i <= 50; i += 2) { glVertex3f(i, 0, -50); glVertex3f(i, 0, 50); glVertex3f(-50, 0, i); glVertex3f(50, 0, i); } glEnd(); // 绘制平台 for (const auto& platform : game.platforms) { drawCube(platform.x, platform.height / 2.0f, platform.z, platform.size, platform.r, platform.g, platform.b); } // 绘制玩家 drawPlayer(); // 显示分数和游戏状态 glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluOrtho2D(0, 800, 0, 600); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glColor3f(1.0f, 1.0f, 1.0f); glRasterPos2i(10, 580); std::string scoreStr = "Score: " + std::to_string(game.score); for (char c : scoreStr) { glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, c); } if (game.gameOver) { glRasterPos2i(350, 300); std::string gameOverStr = "Game Over! Press R to restart"; for (char c : gameOverStr) { glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, c); } } glPopMatrix(); glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glutSwapBuffers(); } // 更新游戏状态 void update(int value) { if (!game.gameOver) { // 更新玩家位置 if (game.player.isJumping) { game.player.y += game.player.velocityY; game.player.velocityY += GRAVITY; // 检查是否落地 if (game.player.y <= game.platforms[game.currentPlatform].height) { game.player.y = game.platforms[game.currentPlatform].height; game.player.isJumping = false; game.player.velocityY = 0; } } // 蓄力时增加跳跃力 if (game.isCharging) { game.player.chargeTime += 0.1f; game.player.jumpForce = std::min(game.player.chargeTime * JUMP_FORCE_MULTIPLIER, MAX_JUMP_FORCE); } // 检查是否掉出世界 if (game.player.y < -5.0f) { game.gameOver = true; } } glutPostRedisplay(); glutTimerFunc(16, update, 0); // 约60FPS } // 键盘按键处理 void keyboard(unsigned char key, int x, int y) { if (game.gameOver && (key == 'r' || key == 'R')) { initGame(); return; } if (!game.gameOver && key == ' ') { if (!game.player.isJumping && !game.isCharging) { game.isCharging = true; game.player.chargeTime = 0; game.player.jumpForce = MIN_JUMP_FORCE; } } } // 键盘释放处理 void keyboardUp(unsigned char key, int x, int y) { if (!game.gameOver && key == ' ' && game.isCharging) { game.isCharging = false; if (!game.player.isJumping) { game.player.isJumping = true; game.player.velocityY = game.player.jumpForce; // 计算跳跃方向 (随机方向) float angle = static_cast<float>(rand() % 360) * 3.14159f / 180.0f; float jumpDistance = game.player.jumpForce * 30.0f; game.player.x += jumpDistance * sin(angle); game.player.z += jumpDistance * cos(angle); // 检查是否落在平台上 bool landed = false; for (size_t i = 0; i < game.platforms.size(); ++i) { auto& p = game.platforms[i]; if (fabs(game.player.x - p.x) < p.size / 2 && fabs(game.player.z - p.z) < p.size / 2) { game.currentPlatform = i; landed = true; // 计算得分 (越靠近中心得分越高) float distFromCenter = sqrt(pow(game.player.x - p.x, 2) + pow(game.player.z - p.z, 2)); float maxDist = p.size / 2; int points = static_cast<int>(10 * (1 - distFromCenter / maxDist)); game.score += std::max(1, points); break; } } // 如果没落在任何平台上,游戏结束 if (!landed) { game.gameOver = true; } } } } // 窗口大小调整 void reshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, (float)w / (float)h, 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(800, 600); glutCreateWindow("跳一跳游戏 - 固定视角"); glEnable(GL_DEPTH_TEST); glClearColor(0.53f, 0.81f, 0.92f, 1.0f); // 天空蓝色背景 initGame(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutKeyboardUpFunc(keyboardUp); glutTimerFunc(0, update, 0); glutMainLoop(); return 0; }帮我修改一下代码,我现在想要一开始就有小人站在第一个正方体上
06-16
提供了一个基于51单片机的RFID门禁系统的完整资源文件,包括PCB图、原理图、论文以及源程序。该系统设计由单片机、RFID-RC522频射卡模块、LCD显示、灯控电路、蜂鸣器报警电路、存储模块和按键组成。系统支持通过密码和刷卡两种方式进行门禁控制,灯亮表示开门成功,蜂鸣器响表示开门失败。 资源内容 PCB图:包含系统的PCB设计图,方便用户进行硬件电路的制作和调试。 原理图:详细展示了系统的电路连接和模块布局,帮助用户理解系统的工作原理。 论文:提供了系统的详细设计思路、实现方法以及测试结果,适合学习和研究使用。 源程序:包含系统的全部源代码,用户可以根据需要进行修改和优化。 系统功能 刷卡开门:用户可以通过刷RFID卡进行门禁控制,系统会自动识别卡片并判断是否允许开门。 密码开门:用户可以通过输入预设密码进行门禁控制,系统会验证密码的正确性。 状态显示:系统通过LCD显示屏显示当前状态,如刷卡成功、密码错误等。 灯光提示:灯亮表示开门成功,灯灭表示开门失败或未操作。 蜂鸣器报警:当刷卡或密码输入错误时,蜂鸣器会发出报警声,提示用户操作失败。 适用人群 电子工程、自动化等相关专业的学生和研究人员。 对单片机和RFID技术感兴趣的爱好者。 需要开发类似门禁系统的工程师和开发者。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值