无题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. 不通过ecplise,只通过文件目录 创建最简单的JSP文件

    手动创建最简单的JSP 文件   1.在Tomcat 6.0的安装目录的webapps目录下新建一个目录,起名叫myapp. 2.在myapp目录下新建一个目录WEB-INF,注意,目录名称是区分大小 ...

  2. 单页Html及Android App供小孩学习常用汉字

    为了检验及帮助小孩学习常用汉字,简单开发本网页应用: 常用汉字是按使用频率排序的,来源于网上: 该简单应用 有Android APP下载 “学习常用汉字_20150910.apk” 单页Html 示例 ...

  3. oracle 权限、规则

    Oracle中关于权限与规则简单总结: --1,管理员登录 conn sys/orcl@orcl as sysdba; --2,创建用户方案---必须管理员身份才能操作 create user use ...

  4. Java基础--java简介

    1.Java的起源: Oak  -->  Java 2.Java的发展 Java1.0 Java2 JavaSE:Java平台标准版 JavaME:微型版 JavaEE:企业版 Sun公司 or ...

  5. spring Existing transaction found for transaction marked with propagation 'never' 解决

    先在申明事务中配置了所有的事务 <!--配置事物传播策略,以及隔离级别--> <tx:advice id="txAdvice" transaction-manag ...

  6. IOS开发之关于UIButton点击没有响应问题

    1.如果一个UIButton的frame超出父视图的frame,UIButton还是可以显现的,但响应不了点击事件了,当开发中,遇到UIButton点击没有响应问题时,我们需要输出btn及它父视图的f ...

  7. 生产线上的Nginx如何添加未编译安装模块

    正在生产线上跑着web前端是nginx+tomcat,现在有这样一个需求,需要对网站的单品页面和列表页设置缓存,不同的页面设置不同的缓存,但是由于开始没有安装ngx_cache_purge这个模块,现 ...

  8. Elasticsearch搜索含有数字标签的处理

    {"tag_id":“12345”} 在search的时候是完全匹配,因为Elasticsearch在处理这个的过程中把“123456”字符当成一个整体的数据,因此折腾了好久就是找 ...

  9. zc.buildout构建项目时报错‘AttributeError: '_NamespacePath' object has no attribute 'sort'’

    在使用zc.buildout构建项目时如果碰到‘AttributeError: '_NamespacePath' object has no attribute 'sort'’报错: An inter ...

  10. upload 上传按钮组件 iview

    <!-- * @description 导入Excel * @fileName importExcel.vue * @author 彭成刚 * @date // :: * @version V1 ...