无题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. 题解报告:poj 1985 Cow Marathon(求树的直径)

    Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...

  2. JS防止页面被其他网站iframe使用方法

    if(window.top !== window.self){ window.top.location = window.location;} 这句话的意识是说:如果当前窗体不是顶级窗体,就把自己变成 ...

  3. 在 Windows 7 中禁用IPv6协议/IPv6隧道

    How to disable certain Internet Protocol version 6 (IPv6) components in Windows Vista, Windows 7 and ...

  4. .net 音频转换 .amr 转 .mp3 (七牛转换法)

    .amr 用于移动设备的音频,压缩比比较大,多用于人声.通话,效果还行!所以,移动设备多采用amr格式来进行录存!比较常见的例子:通话录音,微信语音以及录音等! 这个鬼,用两个字来形容,就是“蛋疼”: ...

  5. 教你如何配置WampServer

    httpdconfig 搜索deny 268行 Deny 换成Allow 在本机cmd 搜索 ipconfig 找到 本机的ip 地址 239 行 DocumentRoot "e:/mywe ...

  6. Android小玩意儿-- 从头开发一个正经的MusicPlayer(二)

    1·在Service中实例化MusicPlayer,实现对整个播放过程的控制 上一次做到了找到音乐数据,并封装成对象装在ArrayList里,把数据的信息显示在UI上.下面一个阶段就要开始真正的音乐播 ...

  7. 安装scount的es驱动,composer require tamayo/laravel-scout-elastic报错解决

    执行 composer require tamayo/laravel-scout-elastic 报错信息如下: Problem 1 - Installation request for tamayo ...

  8. Redis学习笔记(六)有序集合进阶

    1.基础操作 ZCARD(获取成员数量) ZINCRBY key_name num member(将member的分数加num) ZCOUNT key_name min max(获取分数在min与ma ...

  9. 伪类的格式重点:父标签层级 & 当前标签类型

    伪类的格式重点:父标签层级 & 当前标签类型 通过例子说明: css1: span:nth-of-type(2){color: red;} css2: span :nth-of-type(2) ...

  10. pickle 两个使用小方法

    def pickle_load(file_path): f = open(file_path,'r+') data = pickle.load(f) f.close() return data     ...