BNUOJ 6378 无题I
无题I
This problem will be judged on HDU. Original ID: 2234
64-bit integer IO format: %I64d Java class name: Main
Input
对于每组数据输入4行,每行4列表示这个矩阵。
Output
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
#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的更多相关文章
- BNUOJ 52325 Increasing or Decreasing 数位dp
传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...
- bnuoj 24251 Counting Pair
一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...
- bnuoj 44359 快来买肉松饼
http://www.bnuoj.com/contest/problem_show.php?pid=44359 快来买肉松饼 Time Limit: 5000 ms Case Time Lim ...
- BNUOJ 1006 Primary Arithmetic
Primary Arithmetic 来源:BNUOJ 1006http://www.bnuoj.com/v3/problem_show.php?pid=1006 当你在小学学习算数的时候,老师会教你 ...
- 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 ...
- bnuoj 25659 A Famous City (单调栈)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25659 #include <iostream> #include <stdio.h ...
- bnuoj 25662 A Famous Grid (构图+BFS)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25662 #include <iostream> #include <stdio.h ...
- bnuoj 4207 台风(模拟题)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=4207 [题意]:中文题,略 [题解]:模拟 [code]: #include <iostrea ...
- bnuoj 4208 Bubble sort
http://www.bnuoj.com/bnuoj/problem_show.php?pid=4208 [题意]:如题,求冒泡排序遍历趟数 [题解]:这题开始2B了,先模拟TLE,然后想了一下,能不 ...
随机推荐
- 题解报告:poj 1985 Cow Marathon(求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- JS防止页面被其他网站iframe使用方法
if(window.top !== window.self){ window.top.location = window.location;} 这句话的意识是说:如果当前窗体不是顶级窗体,就把自己变成 ...
- 在 Windows 7 中禁用IPv6协议/IPv6隧道
How to disable certain Internet Protocol version 6 (IPv6) components in Windows Vista, Windows 7 and ...
- .net 音频转换 .amr 转 .mp3 (七牛转换法)
.amr 用于移动设备的音频,压缩比比较大,多用于人声.通话,效果还行!所以,移动设备多采用amr格式来进行录存!比较常见的例子:通话录音,微信语音以及录音等! 这个鬼,用两个字来形容,就是“蛋疼”: ...
- 教你如何配置WampServer
httpdconfig 搜索deny 268行 Deny 换成Allow 在本机cmd 搜索 ipconfig 找到 本机的ip 地址 239 行 DocumentRoot "e:/mywe ...
- Android小玩意儿-- 从头开发一个正经的MusicPlayer(二)
1·在Service中实例化MusicPlayer,实现对整个播放过程的控制 上一次做到了找到音乐数据,并封装成对象装在ArrayList里,把数据的信息显示在UI上.下面一个阶段就要开始真正的音乐播 ...
- 安装scount的es驱动,composer require tamayo/laravel-scout-elastic报错解决
执行 composer require tamayo/laravel-scout-elastic 报错信息如下: Problem 1 - Installation request for tamayo ...
- Redis学习笔记(六)有序集合进阶
1.基础操作 ZCARD(获取成员数量) ZINCRBY key_name num member(将member的分数加num) ZCOUNT key_name min max(获取分数在min与ma ...
- 伪类的格式重点:父标签层级 & 当前标签类型
伪类的格式重点:父标签层级 & 当前标签类型 通过例子说明: css1: span:nth-of-type(2){color: red;} css2: span :nth-of-type(2) ...
- pickle 两个使用小方法
def pickle_load(file_path): f = open(file_path,'r+') data = pickle.load(f) f.close() return data ...