为每个状态定义两个函数S和H,分别表示当前状态到列一致和行一致的目标状态的最少操作次数。

然后有了估价函数F=Min(S,H)就可以IDA*了。

#include <cstdio>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue> #define rep(i, l, r) for(int i=l; i<=r; i++)
#define down(i, l, r) for(int i=l; i>=r; i--)
#define maxn 40320
#define MAX 1<<30 using namespace std; int t, n[][], k; inline void Up(int x) { int a; a = n[][x]; n[][x] = n[][x]; n[][x] = n[][x]; n[][x] = n[][x]; n[][x] = a; }
inline void Down(int x) { int a; a = n[][x]; n[][x] = n[][x]; n[][x] = n[][x]; n[][x] = n[][x]; n[][x] = a; }
inline void Right(int x) { int a; a = n[x][]; n[x][] = n[x][]; n[x][] = n[x][]; n[x][] = n[x][]; n[x][] = a; }
inline void Left(int x) { int a; a = n[x][]; n[x][] = n[x][]; n[x][] = n[x][]; n[x][] = n[x][]; n[x][] = a; } int S()
{
int b[], o, o2 = ;
rep(i, , )
{
rep(j, , ) b[j] = ; o = ;
rep(j, , ) b[n[j][i]]++;
rep(j, , ) o = max(o, b[j]);
o2 = max(o2, -o);
}
return o2;
} int H()
{
int b[], o, o2 = ;
rep(i, , )
{
rep(j, , ) b[j] = ; o = ;
rep(j, , ) b[n[i][j]]++;
rep(j, , ) o = max(o, b[j]);
o2 = max(o2, -o);
}
return o2;
} bool Search(int x)
{
if (x == k)
{
if (!S() || !H()) return true; else return false;
}
int now = min(S(), H()); if (now > k-x) return false;
Left(); if (min(S(), H()) <= now) if (Search(x+)) return true; Right();
Left(); if (min(S(), H()) <= now) if (Search(x+)) return true; Right();
Left(); if (min(S(), H()) <= now) if (Search(x+)) return true; Right();
Left(); if (min(S(), H()) <= now) if (Search(x+)) return true; Right();
Right(); if (min(S(), H()) <= now) if (Search(x+)) return true; Left();
Right(); if (min(S(), H()) <= now) if (Search(x+)) return true; Left();
Right(); if (min(S(), H()) <= now) if (Search(x+)) return true; Left();
Right(); if (min(S(), H()) <= now) if (Search(x+)) return true; Left();
Up(); if (min(S(), H()) <= now) if (Search(x+)) return true; Down();
Up(); if (min(S(), H()) <= now) if (Search(x+)) return true; Down();
Up(); if (min(S(), H()) <= now) if (Search(x+)) return true; Down();
Up(); if (min(S(), H()) <= now) if (Search(x+)) return true; Down();
Down(); if (min(S(), H()) <= now) if (Search(x+)) return true; Up();
Down(); if (min(S(), H()) <= now) if (Search(x+)) return true; Up();
Down(); if (min(S(), H()) <= now) if (Search(x+)) return true; Up();
Down(); if (min(S(), H()) <= now) if (Search(x+)) return true; Up();
return false;
} int main()
{
scanf("%d", &t);
while (t--)
{
rep(i, , ) rep(j, , ) scanf("%d", &n[i][j]);
k = ;
while (k <= )
if (Search()) break; else k++;
if (k == ) k = -; printf("%d\n", k);
}
return ;
}

HDU-2234 无题I的更多相关文章

  1. HDU 2236 无题II(二分图匹配+二分)

    HDU 2236 无题II 题目链接 思路:行列仅仅能一个,想到二分图,然后二分区间长度,枚举下限.就能求出哪些边是能用的,然后建图跑二分图,假设最大匹配等于n就是符合的 代码: #include & ...

  2. HDU 2236 无题Ⅱ

    HDU 2236 无题Ⅱ 题目大意 这是一个简单的游戏,在一个\(n*n\)的矩阵中,找n个数使得这n个数都在不同的行和列里并且要求这n个数中的最大值和最小值的差值最小. solution 暴枚\(i ...

  3. hdu 2234(IDA*)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2234 思路:IDA*可以搞,借鉴的是大牛的启发式函数h(): 可以考虑把每一行上的数转化成相同的,或者 ...

  4. Hdu 2236 无题II 最大匹配+二分

    题目链接: pid=2236">Hdu 2236 解题思路: 将行和列理解为二分图两边的端点,给出的矩阵即为二分图中的全部边, 假设二分图能全然匹配,则说明 不同行 不同列的n个元素 ...

  5. HDU 1871 无题

    无题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  6. HDU 2236 无题II 题解

    题目 这是一个简单的游戏,在一个n*n的矩阵中,找n个数使得这n个数都在不同的行和列里并且要求这n个数中的最大值和最小值的差值最小. 输入格式 输入一个整数\(T\)表示\(T\)组数据. 对于每组数 ...

  7. 【最大匹配+二分答案】HDU 2236 无题II

    题目内容 这是一个简单的游戏,在一个\(n×n\)的矩阵中,找\(n\)个数使得这\(n\)个数都在不同的行和列里并且要求这\(n\)个数中的最大值和最小值的差值最小. 输入格式 输入一个整数\(T\ ...

  8. HDU 2236:无题II(二分搜索+二分匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=2236 题意:中文题意. 思路:先找出最大和最小值,然后二分差值,对于每一个差值从下界开始枚举判断能不能二分匹配. ...

  9. (二分匹配“匈牙利算法”)无题II --HDU --2236

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=2236 代码: #include<cstdio> #include<cstring> ...

  10. 无题II hdu 2236(二分枚举区间)

    分析:只需要用二分找一个区间,然后不断枚举这个区间是否可以达到最大匹配,一直二分到答案为止.   代码: =============================================== ...

随机推荐

  1. 影响一个UIView是否能正常显示的几个因素

    在使用代码实现UIView及其子类的对象的时候,经常会遇到创建的某个view没有显示在屏幕上.以下总结了本人遇到过的几种情况.可能还有些其它的原因也会导致view不能正常显示,限于个人经历有限,无法全 ...

  2. Sequence II

    6990: Sequence II 时间限制: 3 Sec  内存限制: 128 MB提交: 206  解决: 23[提交][状态][讨论版][命题人:admin] 题目描述 We define an ...

  3. Bootstrap历练实例:模态框(Modal)插件

    模态框(Modal)是覆盖在父窗体上的子窗体.通常,其目的是显示来自一个单独源的内容,可以在不离开父窗体的情况下进行一些交互,子窗体提供一些交互或信息. <!DOCTYPE html>&l ...

  4. java用org.apache.poi包操作excel

    一.POI简介 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API 目前比较成熟的是HSSF接口,处理MS Excel(97- ...

  5. ssh整合思想 Spring与Hibernate的整合ssh整合相关JAR包下载 .MySQLDialect方言解决无法服务器启动自动update创建表问题

    除之前的Spring相关包,还有structs2包外,还需要Hibernate的相关包 首先,Spring整合其他持久化层框架的JAR包 spring-orm-4.2.4.RELEASE.jar  ( ...

  6. NSOperation、NSOperationQueue

    NSOperation.NSOperationQueue NSOperation 和 NSOperationQueue 配合使用也能实现多线程. NSOperation 继承于 NSObject,是一 ...

  7. Spring Security和Shiro的比较和使用

    https://blog.csdn.net/it_java_shuai/article/details/78054951 Spring Security和Shiro的比较和使用 2017年09月21日 ...

  8. python从列表中删除相邻重复元素

    这里以一个栗子来用三种方法实现,输入a=['1','1','2','2','1','1'],输出b=['1', '2', '1'] 方法一: list1 = ['] def del_adjacent( ...

  9. MYSQL不能显示中文字,显示错误“ERROR 1366 (HY000): Incorrect string value: '\xE5\xBC\xA0\xE4\xB8\x89'”

    或者建表时带上编码utf8 CREATE TABLE `students`( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR( ...

  10. 如何下载并安装 robomongo 到Ubuntu 系统

    官网下载软件,https://robomongo.org/download wget https://download.robomongo.org/1.2.1/linux/robo3t-1.2.1-l ...