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

相关文章

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3

解决RocketMQ的幂等性问题

《解决RocketMQ的幂等性问题》重复消费因调用链路长、消息发送超时或消费者故障导致,通过生产者消息查询、Redis缓存及消费者唯一主键可以确保幂等性,避免重复处理,本文主要介绍了解决RocketM... 目录造成重复消费的原因解决方法生产者端消费者端代码实现造成重复消费的原因当系统的调用链路比较长的时

深度解析Nginx日志分析与499状态码问题解决

《深度解析Nginx日志分析与499状态码问题解决》在Web服务器运维和性能优化过程中,Nginx日志是排查问题的重要依据,本文将围绕Nginx日志分析、499状态码的成因、排查方法及解决方案展开讨论... 目录前言1. Nginx日志基础1.1 Nginx日志存放位置1.2 Nginx日志格式2. 499

kkFileView启动报错:报错2003端口占用的问题及解决

《kkFileView启动报错:报错2003端口占用的问题及解决》kkFileView启动报错因office组件2003端口未关闭,解决:查杀占用端口的进程,终止Java进程,使用shutdown.s... 目录原因解决总结kkFileViewjavascript启动报错启动office组件失败,请检查of

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束

Python错误AttributeError: 'NoneType' object has no attribute问题的彻底解决方法

《Python错误AttributeError:NoneTypeobjecthasnoattribute问题的彻底解决方法》在Python项目开发和调试过程中,经常会碰到这样一个异常信息... 目录问题背景与概述错误解读:AttributeError: 'NoneType' object has no at

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

nginx中端口无权限的问题解决

《nginx中端口无权限的问题解决》当Nginx日志报错bind()to80failed(13:Permissiondenied)时,这通常是由于权限不足导致Nginx无法绑定到80端口,下面就来... 目录一、问题原因分析二、解决方案1. 以 root 权限运行 Nginx(不推荐)2. 为 Nginx

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原