zoukankan      html  css  js  c++  java
  • 回溯算法之火力网

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SeqListSort
    {
        /// <summary>
        /// <ather>
        /// lihonglin
        /// </ather>
        /// <content>
        /// 在一个n*n的网格里,每个网格可能为“墙壁”(用‘X’表示)和“街道”(用‘.’表示)。现在在街道
        /// 放置碉堡,每个碉堡可以向上下左右四个方向开火,子弹射程无限远。墙壁可以阻挡子弹。问最多能放置
        /// 多少个碉堡,使它们彼此不会互相摧毁。
        /// 
        /// 之前的8皇后是一行或者一列只能放一个,而火力网一行列可能有多个,或者一个也没有,所以回溯的
        /// 递归要多一个判断没有放置任何碉堡
        /// </content>
        /// </summary>
        class FireNet
        {
            public static char[,] map = new char[4,4];//地图最大为4行4列
            public static int maxNum = 0; // 最大放置碉堡个数
            public static int n;// 创建地图的行列个数
    
            public static void InitMap()
            {
                Console.WriteLine("请输入地图的行数(n> 0 && n <5)");
                n = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入地图的数据‘x’表示墙,'.'表示街道");
                for (int i = 0; i < n; ++i )
                {
                    for (int j = 0; j < n; ++j )
                    {
                        map[i, j] = Convert.ToChar( Console.Read() );
                    }
                    Console.ReadLine();
                }
                PutShooter(0,0);
            }
            // 放置碉堡
            public static void PutShooter(int k, int curNum)// 参数k,得到行列数,curNum当前碉堡数
            {
                int i = 0;
                int j = 0;
                // 结束条件
                if ( k == n * n )
                {
                    if ( curNum >= maxNum )//当前碉堡数 》最大碉堡数停止
                    {
                        // 输出  保存当前路径的最大值
                        maxNum = curNum;
                        Console.WriteLine("最多能放碉堡数 = " + maxNum);
                        return;
                    }
                }
                else
                {
                    i = k / n;// row
                    j = k % n;//col
                    if ((map[i,j] == '.') && IsOK(i,j))//是街道,并且有墙阻挡,可以放置碉堡
                    {
                        map[i, j] = 'o';
                        PutShooter(k + 1, curNum + 1);// 继续检测下一列
                        map[i, j] = '.';//回溯
                    }
                    // 当前点不能放置或者回溯回来
                    PutShooter(k + 1, curNum);
                }
            }
            public virtual void text()
            {
                string str1 = "a";
                string str2 = str1;
                //str1="a"+"b";
                Console.WriteLine("str1 = " + str1.GetHashCode() + "str2= " + str2.GetHashCode());
            }
            
            //放置的条件
            public static bool IsOK(int row, int col)
            {
                for (int i = row - 1; i >= 0; --i )//检测每一列
                {
                    if (map[i,col] == 'o')// 当前位置已有碉堡
                    {
                        return false;
                    }
                    if (map[i, col] == 'x') break;// 当前为墙,可以放置
                }
                // 检测每一行是否可放
                for (int j = col - 1; j >= 0; --j )
                {
                    if (map[row,j] == 'o')
                    {
                        return false;
                    }
                    if (map[row, j] == 'x') break;
                }
                return true;
            }
        }
    }
  • 相关阅读:
    mybatis
    Hello mybatis
    linux各种终端类型的区别和概念
    页面对象的定位
    laravel 操作 redis
    Python应用与实践
    Mysql与Oracle区别
    PHP 中 call_user_func 函数 和 call_user_func_array 函数的区别
    php依赖注入
    Linux/Unix 怎样找出并删除某一时间点的文件
  • 原文地址:https://www.cnblogs.com/lihonglin2016/p/4307868.html
Copyright © 2011-2022 走看看