无题I

Time Limit: 10000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 2234
64-bit integer IO format: %I64d      Java class name: Main

 
一天机器人小A在玩一个简单的智力游戏,这个游戏是这样的,在一个4*4的矩阵中分别有4个1,4个2,4个3和4个4分别表示4种不同的东西,每一步小A可以把同一行的4个数往左移或者往右移一步或者把同一列的4个数字往上移或者往下移一步(1,2,3,4往左移后是2,3,4,1),小A现在想知道进过最少的几步移动可以将矩阵的每行上的4个数字都一样或者每列上的4个数字都一样。但是小A又不想走太多步,他只要知道最少步数是否少于等于5步,是的话输出准确的步数,否则输出-1。

 

Input

先输入一个整数T,表示有T组数据。
对于每组数据输入4行,每行4列表示这个矩阵。

 

Output

对于每组输入输出一个正整数表示最少的移动步数,大于5则输出-1.

 

Sample Input

2

1 2 3 4
1 2 3 4
1 2 3 4
2 3 4 1 4 1 1 1
1 2 2 2
2 3 3 3
3 4 4 4

Sample Output

1
1

Source

 
 
 
解题:IDA*
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int g[][],ans;
bool check(int (*t)[]){
int i,j;
bool flag = false;
for(i = ; i < ; i++){
for(j = ; j < ; j++)
if(t[j][i] != t[j-][i]) {flag = true;break;}//检查每一列
if(flag) break;
}
if(!flag) return true;
for(i = ; i < ; i++){
for(j = ; j < ; j++)
if(t[i][j] != t[i][j-]) {flag = false;break;}
if(!flag) break;
}
return flag;
}
void shift(int (*t)[],int dir,int u){
int i,temp;
if(dir == ){//行左移
temp = t[u][];
for(i = ; i < ; i++)
t[u][i] = t[u][i+];
t[u][i] = temp;
}else if(dir == ){//行右移
temp = t[u][];
for(i = ; i; i--)
t[u][i] = t[u][i-];
t[u][i] = temp;
}else if(dir == ){//列上移
temp = t[][u];
for(i = ; i < ; i++)
t[i][u] = t[i+][u];
t[i][u] = temp;
}else if(dir == ){//列下移
temp = t[][u];
for(i = ; i; i--)
t[i][u] = t[i-][u];
t[i][u] = temp;
}
} bool dfs(int (*t)[],int step){
int mp[][],i,j,k;
if(ans == step && check(t)) return true;
if(ans <= step) return false;
for(i = ; i < ; i++){
for(j = ; j < ; j++){
memcpy(mp,t,sizeof(mp));
shift(mp,j,i);
if(dfs(mp,step+)) return true;
}
}
return false;
}
int main(){
int ks,i,j;
scanf("%d",&ks);
while(ks--){
for(i = ; i < ; i++){
for(j = ; j < ; j++)
scanf("%d",g[i]+j);
}
if(check(g)) {puts("");continue;}
for(ans = ; ans < ; ans++)
if(dfs(g,)) break;
printf("%d\n",ans<?ans:-);
}
return ;
}

优化了下,效果不明显啊!

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int g[][],ans;
bool check(int (*t)[]){
int i,j;
bool flag = false;
for(i = ; i < ; i++){
for(j = ; j < ; j++)
if(t[j][i] != t[j-][i]) {flag = true;break;}//检查每一列
if(flag) break;
}
if(!flag) return true;
for(i = ; i < ; i++){
for(j = ; j < ; j++)
if(t[i][j] != t[i][j-]) {flag = false;break;}
if(!flag) break;
}
return flag;
}
void shift(int (*t)[],int dir,int u){
int i,temp;
if(dir == ){//行左移
temp = t[u][];
for(i = ; i < ; i++)
t[u][i] = t[u][i+];
t[u][i] = temp;
}else if(dir == ){//行右移
temp = t[u][];
for(i = ; i; i--)
t[u][i] = t[u][i-];
t[u][i] = temp;
}else if(dir == ){//列上移
temp = t[][u];
for(i = ; i < ; i++)
t[i][u] = t[i+][u];
t[i][u] = temp;
}else if(dir == ){//列下移
temp = t[][u];
for(i = ; i; i--)
t[i][u] = t[i-][u];
t[i][u] = temp;
}
} bool dfs(int (*t)[],int step,int pre,int dir){
int mp[][],i,j,k;
if(ans == step && check(t)) return true;
if(ans <= step) return false;
for(i = ; i < ; i++){
for(j = ; j < ; j++){
memcpy(mp,t,sizeof(mp));
if(i == pre && j == dir) continue;
shift(mp,j,i);
if(dfs(mp,step+,i,-j)) return true;
}
}
return false;
}
int main(){
int ks,i,j;
scanf("%d",&ks);
while(ks--){
for(i = ; i < ; i++){
for(j = ; j < ; j++)
scanf("%d",g[i]+j);
}
if(check(g)) {puts("");continue;}
for(ans = ; ans < ; ans++)
if(dfs(g,,-,-)) break;
printf("%d\n",ans<?ans:-);
}
return ;
}

BNUOJ 6378 无题I的更多相关文章

  1. BNUOJ 52325 Increasing or Decreasing 数位dp

    传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...

  2. bnuoj 24251 Counting Pair

    一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...

  3. bnuoj 44359 快来买肉松饼

    http://www.bnuoj.com/contest/problem_show.php?pid=44359 快来买肉松饼 Time Limit: 5000 ms     Case Time Lim ...

  4. BNUOJ 1006 Primary Arithmetic

    Primary Arithmetic 来源:BNUOJ 1006http://www.bnuoj.com/v3/problem_show.php?pid=1006 当你在小学学习算数的时候,老师会教你 ...

  5. bnuoj 34985 Elegant String DP+矩阵快速幂

    题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=34985 We define a kind of strings as elegant s ...

  6. bnuoj 25659 A Famous City (单调栈)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=25659 #include <iostream> #include <stdio.h ...

  7. bnuoj 25662 A Famous Grid (构图+BFS)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=25662 #include <iostream> #include <stdio.h ...

  8. bnuoj 4207 台风(模拟题)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=4207 [题意]:中文题,略 [题解]:模拟 [code]: #include <iostrea ...

  9. bnuoj 4208 Bubble sort

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=4208 [题意]:如题,求冒泡排序遍历趟数 [题解]:这题开始2B了,先模拟TLE,然后想了一下,能不 ...

随机推荐

  1. 逆序数 HDOJ 4911 Inversion

    题目传送门 题意:可以交换两个相邻的数字顺序k次,问最后逆序对最少有多少 分析:根据逆序数的定理如果逆序数大于0,那么必定存在1<=i<n使得i和i+1交换后逆序数减1假设原逆序数为cnt ...

  2. 题解报告:hdu 2141 Can you find it?(二分)

    Problem Description Give you three sequences of numbers A, B, C, then we give you a number X. Now yo ...

  3. 1-8继承extends

    什么是继承? 继承是面向对象三大特征之一.java中的继承描述的是两个类之间的关系,被继承的类称为父类,继承的类称为子类,使用extends关键字来表示.在java语言里面只支持单继承,即一个类只能有 ...

  4. 转-sql之left join、right join、inner join的区别

    left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只 ...

  5. RAID基础

    磁盘类型 类型 IDE Integrated Drive Electronics SATA Srial ATA SCSI Small Computer System Interface FC Fibe ...

  6. JVM内存配置参数-XMX,-XMS,-XMN的例子

    转载:http://www.nowcoder.com/questionTerminal/093bfa948d144ce3b0a68b938ae8b4ec 对于JVM内存配置参数: -Xmx10240m ...

  7. Hadoop的ChainMapper和ChainReducer使用案例(链式处理)(四)

    不多说,直接上干货!      Hadoop的MR作业支持链式处理,类似在一个生产牛奶的流水线上,每一个阶段都有特定的任务要处理,比如提供牛奶盒,装入牛奶,封盒,打印出厂日期,等等,通过这样进一步的分 ...

  8. CocoaPods安装遇到的坑。

    //官方推荐地址 CocoaPods :http://code4app.com/article/cocoapods-install-usage cooped的安装  $(inherited) 报pod ...

  9. findFile的用法

    ===============================================   @echo off   echo **No Options:   for /f %%a in (&q ...

  10. 洛谷 P1918 保龄球

    题目描述 DL 算缘分算得很烦闷,所以常常到体育馆去打保龄球解闷.因为他保龄球已经打了几十年了,所以技术上不成问题,于是他就想玩点新花招. DL 的视力真的很不错,竟然能够数清楚在他前方十米左右每个位 ...