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

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. Vue07-Axios

    Axios axios是一个网络请求相关的库. axios: ajax i/o system 使用axios编写的网络请求代码,可以运行在浏览器端,也可以在Node环境中运行. 01. 支持的请求方式 ...

  2. Acwing4244牛的比赛

    Acwing4244.牛的比赛 题目部分 N 头奶牛,编号 1∼N,一起参加比赛. 奶牛的战斗力两两不同. 这些奶牛之间已经进行了 M轮两两对决. 在对决中,战斗力高的奶牛一定会战胜战斗力低的奶牛. ...

  3. JavaWeb开发-HTML基础学习

    1.HTML的基本语法 HTML是什么?:HTML是一种超文本标记语言,负责网页的结构,设计页面的元素内容等 超文本:超越文本限制,除了文本信息,还可以定义图片,音频,视频等 标记语言:由标签构成的语 ...

  4. 七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置

    前言 在上一章节我们在后端框架中引入 SQLite-net ORM 并封装常用方法(SQLiteHelper),今天我们的任务是设计好班级管理相关的表.完善后端班级管理相关接口并对Swagger自定义 ...

  5. Ruby 版本升级

    一.升级原因 在开发shopify app的时候,提示我当前的Ruby版本不支持(如下图),所以需要升级Ruby. 由于Ruby 中的一些 Gem 依赖于 OpenSSL 库,所以更改 Ruby 版本 ...

  6. 文心一言 VS 讯飞星火 VS chatgpt (161)-- 算法导论13.1 1题

    一.用go语言,按照图 13-1(a)的方式,画出在关键字集合(1,2,-,15)上高度为 3 的完全二叉搜索树.以三种不同方式向图中加入 NIL 叶结点并对各结点着色,使所得的红黑树的黑高分别为2. ...

  7. CatFly【汇编代码还原】

    CatFly[难度:1] 题目界面 下载附件,发现是dll文件,放到linux中运行一下,运行界面如图所示: 从上图中可以看到两处字符串,上面的字符串不断滚动,下方字符串在次数上不断累加,猜测上方字符 ...

  8. crictl命令

    containerd提供了ctr命令行用于镜像管理容器,但功能比较简单 所以一般会用k8s提供的crictl命令. 该命令的特点是:只要符合K8S的CRI接口的,都可以使用. 另外一点就是,cricr ...

  9. [转]NLog学习笔记

    配置文件 NLog所有的配置信息都可以写到一个单独的xml文件中,也可以在程序代码中进行配置. 配置文件位置 启动的时候,NLog会试图查找配置文件完成自动配置,查找的文件依次如下(找到配置信息则结束 ...

  10. BUUCTF-Crypto详细Writeup

    每一天都要努力啊    ----2024-01-01 18:11:36 1.一眼就解密 原题:下面的字符串解密后便能获得flag:ZmxhZ3tUSEVfRkxBR19PRl9USElTX1NUUkl ...