为每个状态定义两个函数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. UVA 11992 Fast Matrix Operations (降维)

    题意:对一个矩阵进行子矩阵操作. 元素最多有1e6个,树套树不好开(我不会),把二维坐标化成一维的,一个子矩阵操作分解成多条线段的操作. 一次操作的复杂度是RlogC,很容易找到极端的数据(OJ上实测 ...

  2. Android(java)学习笔记106:Android设置文本颜色的4种方法

    1. Android设置文本颜色的4种方法: (1)利用系统自带的颜色类: tv.setTextColor(android.graphics.Color.RED); (2)数字颜色表示: tv.set ...

  3. c++文件偏移

    #include <iostream> #include <fstream> #include <cassert> using namespace std; int ...

  4. 01_8_session

    01_8_session 1. session总结 1.1服务器的一块内存(存key-value) 1.2和客户端窗口对应(子窗口)(独一无二) 1.3客户端和服务器有对应的SessionID 1.4 ...

  5. iOS项目工程及目录结构

    做过一些iOS的项目,不同项目的沉淀没有积累到一起,目录的管理都在后期随着人员的增加越来越混乱,因此在这里做一些梳理,希望达到两个目的. 一套相对通用的目录结构,作为后续项目的模版. 积累相应的基础库 ...

  6. 基于matlab的蓝色车牌定位与识别---分割

    接着上面的工作,接下去就该是进行字符分割了.考虑到为了后面的字符识别,因此在这部分需要实现的目标是需要把车牌的边框全部切除,对重新定位的车牌进行垂直方向水平方向调整,保证字符是正的.最后才是字符的分割 ...

  7. jenkins 全局工具配置

  8. PHP redis使用命令

    很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/owlient/phpredis(支持redis 2.0.4) Redis::__constru ...

  9. python3 完全平方数(循环)

    题目 一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? 代码: for i in range(1,85): if 168 % i == 0: j = 168 ...

  10. 20个必不可少的Python库也是基本的第三方库

    个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz写的最富盛名的http库.每个Python程序员都应该有它. Scrapy. ...