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

相关文章

IDEA和GIT关于文件中LF和CRLF问题及解决

《IDEA和GIT关于文件中LF和CRLF问题及解决》文章总结:因IDEA默认使用CRLF换行符导致Shell脚本在Linux运行报错,需在编辑器和Git中统一为LF,通过调整Git的core.aut... 目录问题描述问题思考解决过程总结问题描述项目软件安装shell脚本上git仓库管理,但拉取后,上l

深入理解Mysql OnlineDDL的算法

《深入理解MysqlOnlineDDL的算法》本文主要介绍了讲解MysqlOnlineDDL的算法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小... 目录一、Online DDL 是什么?二、Online DDL 的三种主要算法2.1COPY(复制法)

idea npm install很慢问题及解决(nodejs)

《ideanpminstall很慢问题及解决(nodejs)》npm安装速度慢可通过配置国内镜像源(如淘宝)、清理缓存及切换工具解决,建议设置全局镜像(npmconfigsetregistryht... 目录idea npm install很慢(nodejs)配置国内镜像源清理缓存总结idea npm in

pycharm跑python项目易出错的问题总结

《pycharm跑python项目易出错的问题总结》:本文主要介绍pycharm跑python项目易出错问题的相关资料,当你在PyCharm中运行Python程序时遇到报错,可以按照以下步骤进行排... 1. 一定不要在pycharm终端里面创建环境安装别人的项目子模块等,有可能出现的问题就是你不报错都安装

idea突然报错Malformed \uxxxx encoding问题及解决

《idea突然报错Malformeduxxxxencoding问题及解决》Maven项目在切换Git分支时报错,提示project元素为描述符根元素,解决方法:删除Maven仓库中的resolv... 目www.chinasem.cn录问题解决方式总结问题idea 上的 maven China编程项目突然报错,是

Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题

《Python爬虫HTTPS使用requests,httpx,aiohttp实战中的证书异步等问题》在爬虫工程里,“HTTPS”是绕不开的话题,HTTPS为传输加密提供保护,同时也给爬虫带来证书校验、... 目录一、核心问题与优先级检查(先问三件事)二、基础示例:requests 与证书处理三、高并发选型:

前端导出Excel文件出现乱码或文件损坏问题的解决办法

《前端导出Excel文件出现乱码或文件损坏问题的解决办法》在现代网页应用程序中,前端有时需要与后端进行数据交互,包括下载文件,:本文主要介绍前端导出Excel文件出现乱码或文件损坏问题的解决办法,... 目录1. 检查后端返回的数据格式2. 前端正确处理二进制数据方案 1:直接下载(推荐)方案 2:手动构造

Python绘制TSP、VRP问题求解结果图全过程

《Python绘制TSP、VRP问题求解结果图全过程》本文介绍用Python绘制TSP和VRP问题的静态与动态结果图,静态图展示路径,动态图通过matplotlib.animation模块实现动画效果... 目录一、静态图二、动态图总结【代码】python绘制TSP、VRP问题求解结果图(包含静态图与动态图

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

k8s容器放开锁内存限制问题

《k8s容器放开锁内存限制问题》nccl-test容器运行mpirun时因NCCL_BUFFSIZE过大导致OOM,需通过修改docker服务配置文件,将LimitMEMLOCK设为infinity并... 目录问题问题确认放开容器max locked memory限制总结参考:https://Access