《程序员》算法擂台之骑士聚会问题(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

相关文章

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决

Springboot如何正确使用AOP问题

《Springboot如何正确使用AOP问题》:本文主要介绍Springboot如何正确使用AOP问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录​一、AOP概念二、切点表达式​execution表达式案例三、AOP通知四、springboot中使用AOP导出

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到