为每个状态定义两个函数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. jquery的uploadify插件实现的批量上传V3.2.1版

    你需要如下配置(包括引入文件)HTML: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&quo ...

  2. Adobe登陆出现Access denied解决方法

    当我安装好Adobe Acrobat Reader DC时,想要登陆Adobe账号时被Access denied. 一般说需要梯子,然而本人亲测这种方法不行(香港主机,全局模式下). 一个简单有效的方 ...

  3. 广播监听USB插入与拔出

    package com.joy.usbbroadcastreceiver; import android.content.BroadcastReceiver; import android.conte ...

  4. 2018.5.7 androidStudio中:layout_gravity 与 gravity的属性的区别

    android:gravity:设置的是控件自身上面的内容位置 android:layout_gravity:设置控件本身相对于父控件的显示位置. 看下面 <LinearLayout xmlns ...

  5. linux文件或文件夹常见操作

    创建文件夹 mkdir [-p] DirName  在工作目录下,建立一个名为 A 新的子目录 : mkdir A  在工作目录下的 B目录中,建立一个名为 T 的子目录:    若 B 目录不存在, ...

  6. linux 使用wget下载https连接地址cannot verify github.com's certificate

    使用linux的wget下载时候会出现网站没有证书警告的问题, 例如下载git时,可以使用wget https://github.com/git/git/archive/v2.3.0.zip --no ...

  7. VueJS坎坷之路111---_self.$scopedSlots.default is not a function

    VueJs + Element 话不多说,直接贴错: _self.$scopedSlots.default is not a function <el-table stripe border r ...

  8. cesium底图加载底图切换 基于天地图服务

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 地理位置编码geohash学习笔记

    1.geohash及其性质 一种空间索引技术. (1)将二维的经纬度位置数据转换为一维的字符串(基本上hash族的算法都是这样): 其优点在于hash编码后的字符串,可以方便查找和索引,从而减少相似计 ...

  10. Counting Cliques HDU - 5952 单向边dfs

    题目:题目链接 思路:这道题vj上Time limit:4000 ms,HDU上Time Limit: 8000/4000 MS (Java/Others),且不考虑oj测评机比现场赛慢很多,但10月 ...