《程序员》算法擂台之骑士聚会问题(c语言版) 一

2024-03-28 05:59

本文主要是介绍《程序员》算法擂台之骑士聚会问题(c语言版) 一,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


#include  < stdio.h >
#include 
< stdlib.h >
#include
< time.h >

struct  point
{
    
int  x;
    
int  y;
    
int *  CastStep;
};
point
*  GetCanGo(point, int );
void  Walk(point, int );

int  index  =   0 ; // 当前的起点下标

int  rows  =   0 ;
int  cells  =   0 ;
point
**  Chessboard;
point
*  StartPoint; 

void  main()
{
    
int  StartNum  =   0 ;
    printf(
" 欢迎使用神奇的棋盘^_^\n " );
    printf(
" 输入棋盘的行数: " );
    scanf(
"  %d " , & rows);
    printf(
" 输入棋盘的列数: " );
    scanf(
"  %d " , & cells);
    printf(
" 您使用了%d X %d的棋盘,输入的座标x必须在0-%d之间  y必须在0-%d之间\n " ,rows,cells,rows - 1 ,cells - 1 );
    Chessboard 
=  (point ** )malloc( 4 * rows);
    
for ( int  e = 0 ;e < rows;e ++ )
    {
        Chessboard[e] 
=  (point * )malloc( sizeof (point) * cells);
    }
    printf(
" 请输入起点个数: " );
    scanf(
" %d " , & StartNum);

    
for ( int  i = 0 ;i < rows;i ++ ) // 初始化棋盘
    {
        
for ( int  j = 0 ;j < cells;j ++ )
        {
            Chessboard[i][j].x 
=  i;
            Chessboard[i][j].y 
=  j;
            Chessboard[i][j].CastStep 
=  ( int * )malloc( 4   *  StartNum);
            
for ( int  a = 0 ;a < StartNum;a ++ )
                Chessboard[i][j].CastStep[a] 
=   0 ;
        }
    }

    StartPoint 
=  (point * )malloc( sizeof (point) * StartNum);
    
for ( int  k = 0 ;k < StartNum;k ++ )
    {
        printf(
" 输入第%d个起点的座标\nx= " ,k + 1 );
        scanf(
" %d " , & StartPoint[k].x);
        printf(
" y= " );
        scanf(
" %d " , & StartPoint[k].y);
    }
    printf(
" 必须信息读取完毕!\n计算中 " );
    
long  start  =  clock();
    
for (index = 0 ;index < StartNum;index ++ ) // 让每个起点走遍整个棋盘
    {
        Walk(StartPoint[index],
0 );
    }
    
int  CastSteps  =   0 ;
    
int  CastDays  =   0 ;
    point LastPoint;
    
for ( int  a = 0 ;a < rows;a ++ )
    {
        
for ( int  b = 0 ;b < cells;b ++ )
        {
            
int  sum = 0 ;
            
int  max = 0 ;
            
for ( int  c = 0 ;c < StartNum;c ++ )
            {
                sum 
+=  Chessboard[a][b].CastStep[c];
                
if (Chessboard[a][b].CastStep[c]  >  max)
                    max 
=  Chessboard[a][b].CastStep[c];
            }
            
if (CastSteps  >=  sum  &&  CastDays  >=  max)
            {
                CastSteps 
=  sum;
                CastDays 
=  max;
                LastPoint 
=  Chessboard[a][b];
                
continue ;
            }
            
if (CastSteps  ==   0 )
            {
                CastSteps 
=  sum;
                CastDays 
=  max;
                LastPoint 
=  Chessboard[a][b];
            }
        }
    }
    
long  end  =  clock();
    printf(
" \n计算完毕,用时:%ld毫秒\n会聚地点:(%d,%d)\n需要的时间:%d天\n所有人总共花去时间:%d天\n " ,end - start,LastPoint.x,LastPoint.y,CastDays,CastSteps);
    printf(
" 输入任意字符退出.. " );
    
char  c;
    scanf(
"  %c " , & c);
}
int  count  =   0 ; // 保存下一次能走的点的个数
void  Walk(point nowPoint, int  SumSteps)
{
    Chessboard[nowPoint.x][nowPoint.y].CastStep[index] 
=  SumSteps;
    point
*  NextCanGo  =  GetCanGo(nowPoint, ++ SumSteps); // 在这里累加的注意咯
     int  num  =  count;
    
for ( int  i = 0 ;i < num;i ++ )
    {
        Walk(NextCanGo[i],SumSteps);
    }
}

point
*  GetCanGo(point nowPoint, int  SumSteps) // 得到下一步可以走的点
{
    count 
=   0 ;
    point temp[
8 ];
    
if ((nowPoint.x - 1   >=   0   &&  nowPoint.y + 2   <  cells)  &&  (Chessboard[nowPoint.x - 1 ][nowPoint.y + 2 ].CastStep[index]  ==   0  
        
||  SumSteps  <  Chessboard[nowPoint.x - 1 ][nowPoint.y + 2 ].CastStep[index]))
    {
        count
++ ;
        temp[
0 ].x  =  nowPoint.x - 1 ;
        temp[
0 ].y  =  nowPoint.y + 2 ;
    }
    
else
        temp[
0 ].x  =   - 1 ; // 为了标记用

    
if ((nowPoint.x - 1   >=   0   &&  nowPoint.y - 2   >   0 &&  (Chessboard[nowPoint.x - 1 ][nowPoint.y - 2 ].CastStep[index]  ==   0  
        
||  SumSteps  <  Chessboard[nowPoint.x - 1 ][nowPoint.y - 2 ].CastStep[index]))
    {
        count
++ ;
        temp[
1 ].x  =  nowPoint.x - 1 ;
        temp[
1 ].y  =  nowPoint.y - 2 ;
    }
    
else
        temp[
1 ].x  =   - 1 ; // 为了标记用

    
if ((nowPoint.x + 1   <  rows  &&  nowPoint.y + 2   <  cells)  &&  (Chessboard[nowPoint.x + 1 ][nowPoint.y + 2 ].CastStep[index]  ==   0
        
||  SumSteps  <  Chessboard[nowPoint.x + 1 ][nowPoint.y + 2 ].CastStep[index]))
    {
        count
++ ;
        temp[
2 ].x  =  nowPoint.x + 1 ;
        temp[
2 ].y  =  nowPoint.y + 2 ;
    }
    
else
        temp[
2 ].x  =   - 1 ; // 为了标记用

    
if ((nowPoint.x + 1   <  rows  &&  nowPoint.y - 2   >=   0 &&  (Chessboard[nowPoint.x + 1 ][nowPoint.y - 2 ].CastStep[index]  ==   0
        
||  SumSteps  <  Chessboard[nowPoint.x + 1 ][nowPoint.y - 2 ].CastStep[index]))
    {
        count
++ ;
        temp[
3 ].x  =  nowPoint.x + 1 ;
        temp[
3 ].y  =  nowPoint.y - 2 ;
    }
    
else
        temp[
3 ].x  =   - 1 ; // 为了标记用    

    
if ((nowPoint.x + 2   <  rows  &&  nowPoint.y + 1   <  cells)  &&  (Chessboard[nowPoint.x + 2 ][nowPoint.y + 1 ].CastStep[index]  ==   0
        
||  SumSteps  <  Chessboard[nowPoint.x + 2 ][nowPoint.y + 1 ].CastStep[index]))
    {
        count
++ ;
        temp[
4 ].x  =  nowPoint.x + 2 ;
        temp[
4 ].y  =  nowPoint.y + 1 ;
    }
    
else
        temp[
4 ].x  =   - 1 ; // 为了标记用

    
if ((nowPoint.x + 2   <  rows  &&  nowPoint.y - 1   >=   0 &&  (Chessboard[nowPoint.x + 2 ][nowPoint.y - 1 ].CastStep[index]  ==   0
        
||  SumSteps  <  Chessboard[nowPoint.x + 2 ][nowPoint.y - 1 ].CastStep[index]))
    {
        count
++ ;
        temp[
5 ].x  =  nowPoint.x + 2 ;
        temp[
5 ].y  =  nowPoint.y - 1 ;
    }
    
else
        temp[
5 ].x  =   - 1 ; // 为了标记用

    
if ((nowPoint.x - 2   >=   0   &&  nowPoint.y + 1   <  cells)  &&  (Chessboard[nowPoint.x - 2 ][nowPoint.y + 1 ].CastStep[index]  ==   0
        
||  SumSteps  <  Chessboard[nowPoint.x - 2 ][nowPoint.y + 1 ].CastStep[index]))
    {
        count
++ ;
        temp[
6 ].x  =  nowPoint.x - 2 ;
        temp[
6 ].y  =  nowPoint.y + 1 ;
    }
    
else
        temp[
6 ].x  =   - 1 ; // 为了标记用

    
if ((nowPoint.x - 2   >=   0   &&  nowPoint.y - 1   >=   0 &&  (Chessboard[nowPoint.x - 2 ][nowPoint.y - 1 ].CastStep[index]  ==   0
        
||  SumSteps  <  Chessboard[nowPoint.x - 2 ][nowPoint.y - 1 ].CastStep[index]))
    {
        count
++ ;
        temp[
7 ].x  =  nowPoint.x - 2 ;
        temp[
7 ].y  =  nowPoint.y - 1 ;
    }
    
else
        temp[
7 ].x  =   - 1 ; // 为了标记用
    point *  re  =  (point * )malloc( sizeof (point)  *  count);
    
int  t  =   0 ;
    
for ( int  i = 0 ;i < 8 ;i ++ )
    {
        
if (temp[i].x  !=   - 1 )
        {
            re[t] 
=  temp[i];
            t
++ ;
        }
    }
    
return  re;
}

转载于:https://www.cnblogs.com/zhoukun921/archive/2008/06/05/1214148.html

这篇关于《程序员》算法擂台之骑士聚会问题(c语言版) 一的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/854662

相关文章

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

SpringBoot启动报错的11个高频问题排查与解决终极指南

《SpringBoot启动报错的11个高频问题排查与解决终极指南》这篇文章主要为大家详细介绍了SpringBoot启动报错的11个高频问题的排查与解决,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一... 目录1. 依赖冲突:NoSuchMethodError 的终极解法2. Bean注入失败:No qu

MySQL新增字段后Java实体未更新的潜在问题与解决方案

《MySQL新增字段后Java实体未更新的潜在问题与解决方案》在Java+MySQL的开发中,我们通常使用ORM框架来映射数据库表与Java对象,但有时候,数据库表结构变更(如新增字段)后,开发人员可... 目录引言1. 问题背景:数据库与 Java 实体不同步1.1 常见场景1.2 示例代码2. 不同操作

Java时间轮调度算法的代码实现

《Java时间轮调度算法的代码实现》时间轮是一种高效的定时调度算法,主要用于管理延时任务或周期性任务,它通过一个环形数组(时间轮)和指针来实现,将大量定时任务分摊到固定的时间槽中,极大地降低了时间复杂... 目录1、简述2、时间轮的原理3. 时间轮的实现步骤3.1 定义时间槽3.2 定义时间轮3.3 使用时

如何解决mysql出现Incorrect string value for column ‘表项‘ at row 1错误问题

《如何解决mysql出现Incorrectstringvalueforcolumn‘表项‘atrow1错误问题》:本文主要介绍如何解决mysql出现Incorrectstringv... 目录mysql出现Incorrect string value for column ‘表项‘ at row 1错误报错

如何解决Spring MVC中响应乱码问题

《如何解决SpringMVC中响应乱码问题》:本文主要介绍如何解决SpringMVC中响应乱码问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring MVC最新响应中乱码解决方式以前的解决办法这是比较通用的一种方法总结Spring MVC最新响应中乱码解

pip无法安装osgeo失败的问题解决

《pip无法安装osgeo失败的问题解决》本文主要介绍了pip无法安装osgeo失败的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 进入官方提供的扩展包下载网站寻找版本适配的whl文件注意:要选择cp(python版本)和你py