做题之前,可以先到下面这个网站玩一会游戏:

https://unixpapa.com/floodit/?sz=14&nc=6

游戏开发里面,比较常用的一个搜索算法是寻路算法,寻路算法里面用的最多的是A*算法以及很多优化的A*算法,对于只有4个方向的寻路算法,之前在网上见到有A*的位运算优化,性能非常高,1ms能处理上百个格子的地图寻路!很多MMORPG网络游戏都会应用这些算法,如果有兴趣做游戏的童鞋可以多关注下~

本文暂时不讲解A*算法,而是讲解另外一种搜索算法,也就是IDA*算法。

IDA*算法的具体名称是 Iterative Deepening A*, 由Korf1985年提出。该算法的最初目的是为了利用深度搜索的优势解决广度A*的空间问题,其代价是会产生重复搜索

IDA*算法最显著的特点是它有一个限制深度(max-depth),每次迭代都不会超过这个深度,另外和A*算法一样,也有一个评估函数用于剪枝。

学习IDA*最好的办法是先跟着看这道题的代码和注释然后再去理解,对于新手看IDA*的文字描述只会让你一脸懵逼。

题目就不翻译了,可以直接略过,题目要求的就是输入棋盘输出最少步数

Problem Description

Flood-it is a fascinating puzzle game on Google+ platform. The game interface is like follows:

At the beginning of the game, system will randomly generate an N×N square board and each grid of the board is painted by one of the six colors. The player starts from the top left corner. At each step, he/she selects a color and changes all the grids connected with the top left corner to that specific color. The statement “two grids are connected” means that there is a path between the certain two grids under condition that each pair of adjacent grids on this path is in the same color and shares an edge. In this way the player can flood areas of the board from the starting grid (top left corner) until all of the grids are in same color. The following figure shows the earliest steps of a 4×4 game (colors are labeled in 0 to 5):

Given a colored board at very beginning, please find the minimal number of steps to win the game (to change all the grids into a same color).

Input

The input contains no more than 20 test cases. For each test case, the first line contains a single integer N (2<=N<=8) indicating the size of game board.

The following N lines show an N×N matrix (\(a_{i,j}\))n×n representing the game board. \(a_{i,j}\) is in the range of 0 to 5 representing the color of the corresponding grid.

The input ends with N = 0.

Output

For each test case, output a single integer representing the minimal number of steps to win the game.

Sample Input

2
0 0
0 0
3
0 1 2
1 1 2
2 2 1
0

Sample Output

0
3

解题思路:

这道题用IDA*算法解还是很自然的,大概思路如下:

(1)输入棋盘;

(2)迭代限制最大深度,每次迭代执行过程;

(3)每次迭代清零标记;

(4)从位置(0,0)开始寻找颜色;

(5)通过DFS深度搜索迭代,依次尝试6种颜色,搜索该次迭代深度的可能解;

(6)一旦搜索到解,就是最优解~(因为有迭代加深的特性)

源代码:GCC 184ms

#include <stdio.h>
#include <string.h> #define MAXN 8 int size;
int board[MAXN][MAXN];
int dir[4][2] = { {0, 1}, {1, 0}, {0, -1}, { -1, 0} };
int flags[MAXN][MAXN];
int max_depth; //更新棋盘的标记
void update_flags(int i, int j, int color)
{
//如果当前位置颜色不同,则不用更新
if (board[i][j] != color)
{
//标记为2
flags[i][j] = 2;
return;
} //如果相同则标记为1
flags[i][j] = 1; //4个方向
for (int k = 0; k < 4; k++)
{
int x = i + dir[k][0];
int y = j + dir[k][1]; if (x < 0 || y < 0 || x >= size || y >= size || flags[x][y])
continue; update_flags(x, y, color);
}
} //计算剩余的颜色数量
int color_count()
{
int ret = 0;
int s = 0; for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
//如果标记不是相同颜色,位置1
if (flags[i][j] != 1)
s |= (1 << board[i][j]);
}
} //检查还有几种颜色
while (s) {
ret += s & 1;
s >>= 1;
} return ret;
} int dfs(int depth)
{
//如果颜色数量大于剩余步数,则行不通(评估函数)
if (color_count() > max_depth - depth)
return 0; //如果当前深度刚好等于最大深度,说明已经找完了
if (depth == max_depth)
return 1; int temp[MAXN][MAXN];
int color_exist; //直接内存拷贝一份二维数组
memcpy(temp, flags, sizeof(flags)); //6种颜色
for (int color = 0; color < 6; color++)
{
color_exist = 0; for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
//当前位置是不同颜色且正好等于要搜索的颜色
if (flags[i][j] == 2 && board[i][j] == color)
{
//存在这种颜色
color_exist = 1;
update_flags(i, j, color);
}
}
} //颜色不存在,继续搜索
if (!color_exist)
continue;
//如果更新了棋盘,就深度搜索一下,如果成功就返回
if (dfs(depth + 1))
return 1; //内存拷贝一份二维数组,还原flags标记
memcpy(flags, temp, sizeof(flags));
}
return 0;
} void solved() {
//输入棋盘
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++)
scanf ("%d", &board[i][j]);
} //限定最大深度
for (max_depth = 0; ; max_depth++)
{
//flags清零
memset(flags, 0, sizeof(flags));
//更新棋盘
update_flags(0, 0, board[0][0]); if (dfs(0))
break;
} printf ("%d\n", max_depth);
} int main() {
//freopen("test.txt", "r", stdin); while (scanf("%d", &size), size) {
solved();
} return 0;
}
// Cpp
// Author : RioTian
// Time : 20/11/23
#include <bits/stdc++.h>
#define ms(a, b) memset(a, b, sizeof(a))
#define mc(a, b) memcpy(a, b, sizeof(a))
using namespace std;
const int N = 8;
int Size;
int e[N][N]; //保存棋盘
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int flags[N][N];
int max_depth; // (1)输入棋盘; // (2)迭代限制最大深度,每次迭代执行过程; // (3)每次迭代清零标记; // (4)从位置(0,0)开始寻找颜色; // (5)通过DFS深度搜索迭代,依次尝试6种颜色,搜索该次迭代深度的可能解; // (6)一旦搜索到解,就是最优解~(因为有迭代加深的特性) //更新棋盘的标记
void update_flags(int i, int j, int color) {
//如果当前位置颜色不同,则不用更新
if (e[i][j] != color) {
//标记为2
flags[i][j] = 2;
return;
} //如果相同则标记为1
flags[i][j] = 1; // 4个方向
for (int k = 0; k < 4; k++) {
int x = i + dir[k][0];
int y = j + dir[k][1]; if (x < 0 || y < 0 || x >= Size || y >= Size || flags[x][y])
continue; update_flags(x, y, color);
}
} //计算剩余的颜色数量
int color_count() {
int ret = 0;
int s = 0; for (int i = 0; i < Size; i++) {
for (int j = 0; j < Size; j++) {
//如果标记不是相同颜色,位置1
if (flags[i][j] != 1)
s |= (1 << e[i][j]);
}
} //检查还有几种颜色
while (s) {
ret += s & 1;
s >>= 1;
} return ret;
} int dfs(int depth) {
//如果颜色数量大于剩余步数,则行不通(评估函数)
if (color_count() > max_depth - depth)
return 0; //如果当前深度刚好等于最大深度,说明已经找完了
if (depth == max_depth)
return 1; int temp[N][N];
int color_exist; //直接内存拷贝一份二维数组
memcpy(temp, flags, sizeof(flags)); // 6种颜色
for (int color = 0; color < 6; color++) {
color_exist = 0; for (int i = 0; i < Size; i++) {
for (int j = 0; j < Size; j++) {
//当前位置是不同颜色且正好等于要搜索的颜色
if (flags[i][j] == 2 && e[i][j] == color) {
//存在这种颜色
color_exist = 1;
update_flags(i, j, color);
}
}
} //颜色不存在,继续搜索
if (!color_exist)
continue;
//如果更新了棋盘,就深度搜索一下,如果成功就返回
if (dfs(depth + 1))
return 1; //内存拷贝一份二维数组,还原flags标记
memcpy(flags, temp, sizeof(flags));
}
return 0;
} void solve() {
//键入棋盘
for (int i = 0; i < Size; ++i)
for (int j = 0; j < Size; ++j)
cin >> e[i][j]; //限定最大深度
for (max_depth = 0;; max_depth++) {
// flags清零
memset(flags, 0, sizeof(flags));
//更新棋盘
update_flags(0, 0, e[0][0]); if (dfs(0))
break;
}
cout << max_depth << endl;
}
int main() {
freopen("in.txt", "r", stdin);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
while (cin >> Size && Size)
solve();
}

最后安利一下关于A*算法的介绍:here

本题文章参考:https://blog.csdn.net/weizhuwyzc000/article/details/47345573

涂色游戏Flood-it!(IDA star算法) - HDU 4127的更多相关文章

  1. hdu 4559 涂色游戏(SG)

    在一个2*N的格子上,Alice和Bob又开始了新游戏之旅. 这些格子中的一些已经被涂过色,Alice和Bob轮流在这些格子里进行涂色操作,使用两种涂色工具,第一种可以涂色任意一个格子,第二种可以涂色 ...

  2. NOIp十连测 涂色游戏

    [问题描述]小A 和小B 在做游戏.他们找到了一个n 行m 列呈网格状的画板.小A 拿出了p 支不同颜色的画笔,开始在上面涂色.看到小A 涂好的画板,小B 觉得颜色太单调了,于是把画板擦干净,希望涂上 ...

  3. [CSP-S模拟测试]:涂色游戏(DP+组合数+矩阵快速幂)

    题目描述 小$A$和小$B$在做游戏.他们找到了一个$n$行$m$列呈网格状的画板.小$A$拿出了$p$支不同颜色的画笔,开始在上面涂色.看到小$A$涂好的画板,小$B$觉得颜色太单调了,于是把画板擦 ...

  4. hdu 4559 涂色游戏(对SG函数的深入理解,推导打SG表)

    提议分析: 1 <= N <= 4747 很明显应该不会有规律的,打表发现真没有 按题意应该分成两种情况考虑,然后求其异或(SG函数性质) (1)找出单独的一个(一列中只有一个) (2)找 ...

  5. LYDSY模拟赛day3 涂色游戏

    /* 非常好的题 */ #include <cstdio> #include <iostream> #include <cstdlib> #include < ...

  6. 联赛模拟测试5 涂色游戏 矩阵优化DP

    题目描述 分析 定义出\(dp[i][j]\)为第\(i\)列涂\(j\)种颜色的方案数 然后我们要解决几个问题 首先是求出某一列涂恰好\(i\)种颜色的方案数\(d[i]\) 如果没有限制必须涂\( ...

  7. 【矩阵乘优化DP】涂色游戏

    题目大意 用 \(p\) 种颜色填 \(n\times m\) 的画板,要求任意相邻两列的颜色数都不少于 \(q\) ,求方案数. 数据范围 \(1\leq n\leq 100,1\leq m\leq ...

  8. hdu 4559 涂色游戏 博弈论

    构造SG函数:sg[i]表示2*i的sg值!! 代码如下: #include<iostream> #include<stdio.h> #include<algorithm ...

  9. [NOI Online #2 提高组]涂色游戏 题解

    题目描述 你有 1020 个格子,它们从 0 开始编号,初始时所有格子都还未染色,现在你按如下规则对它们染色: 编号是 p1 倍数的格子(包括 0号格子,下同)染成红色. 编号是 p2 倍数的格子染成 ...

  10. 算法习题---4-4骰子涂色(UVa253)

    一:题目 分别对两个骰子的六个面涂色r-红 b-蓝 g-绿,通过转动骰子,看两个骰子是不是一样的涂色方法 (一)题目详解 题目规定了正方体的六个面的序号:从1-,按照这个需要提供涂色序列 (二)案例展 ...

随机推荐

  1. WPF应用框架中工作流模块的介绍

    在前面的随笔,我对我们开发的审批工作流做了不少的介绍,其中有包括WInform的.Vue+Element.Bootstrap Asp.net的,在各个框架上,我们都尽量争取界面能够一致化,以便客户能够 ...

  2. Oracle-Rman备份全解析

    RMAN备份数据库物理文件到备份集(backupset)中.在创建备份集时,仅备份已经使用的数据库(不备份空闲的数据块),而且还可以采用压缩功能. RMAN恢复时指当数据库出现介质失败时,使用RMAN ...

  3. 超轻量级MP4封装方法介绍

    liwen01 2023.12.17 前言 MP4是目前非常常用的一种视频封装格式,关于MP4的介绍资料也非常多.我们常用的封装库或工具有:ffmpeg,libmp4v2,GPAC,MP4.js,它们 ...

  4. Calico IPIP模式下的Cross Subnet特性分析

    本文分享自华为云社区<Calico IPIP模式下的CrossSubnet特性分析>,作者: 可以交个朋友. Calico ipip crossSubnet 模式 Calico-ipip模 ...

  5. python3发送Gratuitous ARP更新vip绑定关系

    操作系统 :CentOS 7.6_x64 Python版本:3.9.12 FreeSWITCH版本 :1.10.9 高可用场景下,vip切换完成后需要发送arp广播更新ip和mac地址的绑定关系,如果 ...

  6. 摆脱自研难题,AUI Kit助力企业快速搭建专属互动课堂

    本专栏将分享阿里云视频云MediaBox系列技术文章,深度剖析音视频开发利器的技术架构.技术性能.开发能效和最佳实践,一起开启音视频的开发之旅.本文为MediaBox最佳实践篇,重点从互动课堂AUI ...

  7. 好玩、有趣的 Linux 命令学习神器 kmdr!

    大家好,我是你们的民工哥. 所有学习Linux系统的初学者都知道,入门时除了简单的系统知识需要了解之外,其次,最重要的就是学习与理解Linux命令的用法与其应用场景. 所以,大家也会遇到这样的情况,不 ...

  8. 如何保证XML正确性

    如何保证XML正确性 XML是个盒子,什么都能装,但是装进去的东西正确与否恐怕无法得知.往往我们都人工审核.双人复核保证,但是次数多了难免会出错.那么我们如何保证和避免这种问题出现呢? 那就是XSD, ...

  9. react 事件函数中 this 绑定问题

    在使用类方式创建组件时,类中定义一个函数,并且绑定到元素的点击事件上,此时这个函数中this指向并不是当前这个组件. 组件代码如下: class App extends React.Component ...

  10. VSCode C++开发环境配置: LLVM clang clangd

    工欲善其事,必先利其器 llvm/clang 比 VSCode 自带的代码提示功能速度更快,功能更强(支持 clang-tidy). 安装 llvm.clang sudo apt install ll ...