第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-1001 跳马

文章介绍了蓝桥杯竞赛中的跳马问题,提供C、C++、Java和Python四种语言的解题示例,强调了刷题和理解思路的重要性。同时,提到了竞赛中涉及的数学知识,如位运算、线性代数和概率论,并分享了历届赛事的部分题解链接。
摘要由CSDN通过智能技术生成

第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-1001 跳马


目录

第十四届蓝桥杯集训——练习解题阶段(无序阶段)-ALGO-1001 跳马

前言

关于数学的疑问

算法训练 跳马

C语言

C++语言

Java语言

Python语言

总结

第六届——第十三届省赛题解

第六届——第十二届国赛题解


前言

        这段时间我会把蓝桥杯官网上的所有非VIP题目都发布一遍,让大家方便去搜索,所有题目都会有几种语言的写法,帮助大家提供一个思路,当然,思路只是思路,千万别只看着答案就认为会了啊,这个方法基本上很难让你成长,成长是在思考的过程中找寻到自己的那个解题思路,并且首先肯定要依靠于题海战术来让自己的解题思维进行一定量的训练,如果没有这个量变到质变的过程你会发现对于相对需要思考的题目你解决的速度就会非常慢,这个思维过程甚至没有纸笔的绘制你根本无法在大脑中勾勒出来,所以我们前期学习的时候是学习别人的思路通过自己的方式转换思维变成自己的模式,说着听绕口,但是就是靠量来堆叠思维方式,刷题方案自主定义的话肯定就是从非常简单的开始,稍微对数据结构有一定的理解,暴力、二分法等等,一步步的成长,数据结构很多,一般也就几种啊,线性表、树、图、再就是其它了。顺序表与链表也就是线性表,当然栈,队列还有串都是属于线性表的,这个我就不在这里一一细分了,相对来说都要慢慢来一个个搞定的。蓝桥杯中对于大专来说相对是比较友好的,例如三分枚举、离散化,图,复杂数据结构还有统计都是不考的,我们找简单题刷个一两百,然后再进行中等题目的训练,当我们掌握深度搜索与广度搜索后再往动态规划上靠一靠,慢慢的就会掌握各种规律,有了规律就能大胆的长一些难度比较高的题目了,再次说明,刷题一定要循序渐进,千万别想着直接就能解决难题,那只是对自己进行劝退处理。加油,平常心,一步步前进。

关于数学的疑问

蓝桥杯中涉及到的数学说多不多,说少也不少,这里罗列了一下能用到的,其中红色的是【大学C组】会使用到的

1、简单数学(基础运算)

2、位运算

3、线性代数

4、离散数学(组合数学)

5、初等数论(整数的性质)

6、概率论

7、几何

虽然看到了线性代数、离散数学、初等数论,但是对于C组来说不会考的太复杂,会基础就好。


算法训练 跳马

资源限制

内存限制:256.0MB   C/C++时间限制:1.0s   Java时间限制:3.0s   Python时间限制:5.0s

问题描述

  一个8×8的棋盘上有一个马初始位置为(a,b),他想跳到(c,d),问是否可以?如果可以,最少要跳几步?

输入格式

  一行四个数字a,b,c,d。

输出格式

  如果跳不到,输出-1;否则输出最少跳到的步数。

样例输入

1 1 2 3

样例输出

1

数据规模和约定

  0<a,b,c,d≤8且都是整数

题解:

C语言

#include <stdio.h>
#include <stdlib.h>
#define N 1000
typedef struct{
     int x;
     int y;
     int t;
}node;
node line[100];
int s[10][10];
int dir[8][2]={{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
int bfs(int a,int b,int c,int d)
{
    if(a==c&&b==d)
        return 0;
    int i,j,front=0,rear=0;
    for(i=1;i<=8;i++)
    {
        for(j=1;j<=8;j++)
            s[i][j]=-1;
    }
    s[a][b]=0;
    node p;
    p.x=a;
    p.y=b;
    p.t=0;
    line[rear++]=p;
    int min=100000000;
    while(front!=rear)
    {
        int m,n;
        node q=line[front++];
        for(i=0;i<8;i++)
        {
            m=q.x+dir[i][0];
            n=q.y+dir[i][1];
            if(m<1||m>8||n<1||n>8)
                continue;
            if(s[m][n]==1)
                continue;
            if(m>=1&&m<=8&&n>=1&&n<=8&&s[m][n]<0)
            {
                node qq;
                qq.x=m;
                qq.y=n;
                qq.t=q.t+1;
                line[rear++]=qq;
                s[m][n]=1;
            }
            if(m==c&&n==d)
            {
                min=min<(q.t+1)?min:(q.t+1);
            }
        }
    }
    return min;
}
int main()
{
    int a,b,c,d;
    scanf("%d %d %d %d",&a,&b,&c,&d);
    printf("%d",bfs(a,b,c,d));
    return 0;
}

C++语言

#include <iostream>
#include <string>
#include <cmath>
#include <queue>
#include <algorithm>

using namespace std;

struct node
{
	int x, y, step;
};

int dx[8] = {1, 1, -1, -1, 2, 2, -2, -2}, dy[8] = {2, -2, -2, 2, 1, -1, -1, 1}, hh[10][10];

void bfs(int a, int b, int c, int d)
{
	node temp = {a, b, 0};
	queue<node> q;
	hh[a][b] = 1;
	q.push(temp);
	while(!q.empty())
	{
		temp = q.front();
		if(temp.x == c && temp.y == d)
		{
			cout << temp.step;
			return ;
		}
		q.pop();
		for(int i = 0; i < 8; i ++)
		{
			int nx = temp.x + dx[i], ny = temp.y + dy[i], nstep = temp.step + 1;
			if(nx >= 1 && nx <= 8 && ny >= 1 && ny <= 8 && hh[nx][ny] == 0)
			{
				node res = {nx, ny, nstep};
				q.push(res);
				hh[nx][ny] = 1;
			}
		}
	}
}

int main()
{
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	bfs(a, b, c, d);
	return 0;
}

Java语言

在扫描输入内容上会有不同的方法,但是与Scanner的用法是相同的。只是相对的录入速度快于Scanner这样在整体运算的过程中可以适当节约时间。

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class Main {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static int in() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    public static void main(String[] args) throws IOException {
        int a = in();
        int b = in();
        int c = in();
        int d = in();
        int n = 8;
        int[][] distance = new int[n + 1][n + 1];
        for (int[] ints : distance) {
            Arrays.fill(ints, -1);
        }
        int[] dx = {-2, -1, 1, 2, -2, -1, 1, 2};
        int[] dy = {-1, -2, -2, -1, 1, 2, 2, 1};
        Queue<int[]> queue = new LinkedList<>();
        queue.offer(new int[]{a, b});
        distance[a][b] = 0;
        while(!queue.isEmpty()) {
            int[] poll = queue.poll();
            int x = poll[0];
            int y = poll[1];
            if(x == c && y == d) {
                System.out.println(distance[c][d]);
                return;
            }
            for (int i = 0; i < 8; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if(nx >= 1 && nx <= n && ny >= 1 && ny <= n && distance[nx][ny] == -1) {
                    distance[nx][ny] = distance[x][y] + 1;
                    queue.offer(new int[]{nx, ny});
                }
            }
        }
        System.out.println(-1);
    }
}

Python语言

相对简洁,但是需要对Python的一些语法很了解,特别是列表推导式的熟悉。

a,b,c,d=list(map(int,input().split()))
dir=[[1,2],[1,-2],[2,1],[-2,1],[-2,-1],[2,-1],[-1,-2],[-1,2]]
visited=[]
for i in range(9):
    visited.append([0]*9)
minnum=float('inf')
 
def dfs(x,y,step):
    global visited,minnum
    if step>=minnum:
        return
    if x==c and y==d:
        minnum=step
        return
    for i in range(8):
        nx=x+dir[i][0]
        ny=y+dir[i][1]
        if nx > 0 and nx <= 8 and ny > 0 and ny <= 8:
            if visited[nx][ny]==0:
                visited[nx][ny]=1
                dfs(nx,ny,step+1)
                visited[nx][ny]=0
 
visited[a][b]=1
dfs(a,b,0)
if minnum==float('inf'):
    print(-1)
else:
    print(minnum)

总结

没有什么不付出就能拿到的结果,我们都是在负重前行,最终结果与自身先天的脑力有一定的关系,但是还是有很大一部分看自己后天的努力,其实从报名到比赛也就5个月左右,真正刷题的事件也就2个月,2个月回忆一下你真正的认真刷过题吗,如果你真的用尽所有的精力去努力了,那么我相信你最终的成绩一定会让你满意的,加油。

第六届——第十三届省赛题解

所有的题目都做了讲解,最难的有配套的视频,视频提供者是【2020级的弓家宜】先生。

第六届Java省赛C组https://laoshifu.blog.csdn.net/article/details/123284163
第七届Java省赛C组https://laoshifu.blog.csdn.net/article/details/123285783
第八届Java省赛C组https://laoshifu.blog.csdn.net/article/details/123302677
第九届Java省赛C组https://laoshifu.blog.csdn.net/article/details/123303285
第十届Java省赛C组https://laoshifu.blog.csdn.net/article/details/123319090
第十一届Java省赛C组https://laoshifu.blog.csdn.net/article/details/123320205
第十二届Java省赛C组第一套https://laoshifu.blog.csdn.net/article/details/123413141
第十二届Java省赛C组第二套https://laoshifu.blog.csdn.net/article/details/123413271
第十三届Java省赛C组https://laoshifu.blog.csdn.net/article/details/128891276

第六届——第十二届国赛题解

所有题目均有题解,部分第10题非最优解,至少跑20%数据。

第六届Java国赛C组https://laoshifu.blog.csdn.net/article/details/123440705
第七届Java国赛C组https://laoshifu.blog.csdn.net/article/details/123442982
第八届Java国赛C组https://laoshifu.blog.csdn.net/article/details/123443626
第九届Java国赛C组https://laoshifu.blog.csdn.net/article/details/123443908
第十届Java国赛C组https://laoshifu.blog.csdn.net/article/details/123444946
第十一届Java国赛C组https://laoshifu.blog.csdn.net/article/details/123445601
第十二届Java国赛C组https://laoshifu.blog.csdn.net/article/details/123446589
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

红目香薰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值