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,然后想了一下,能不 ...
随机推荐
- 牛客网NOIP赛前集训营-普及组
第一场: A-绩点 题目描述 小A刚考完大学考试.现在已经出了n门课的成绩,他想自己先算一下这些课的绩点是多少.设第i门课的他拿到的绩点是gpai,而这门课的学分是sci,那么他的总绩点用下面的公式计 ...
- nodejs on raspi
一. https://cnodejs.org/topic/54032efa9769c2e93797cd06 其中的 “安装Node.js” 一节 注意事项: 1. 它用的是v0.10.26(很早的版本 ...
- json数组某个数值对应渲染
当你统计某一年的某个值它对应的月份总数时,后台没有直接处理好,某个月对应某个值,这样会增加统计的负担,但当数据时这样的时候,在angularjs中时不能直接引用的. "data": ...
- HttpMessageNotWritableException异常解决办法
昨天做多对多的时遇到这个错误,网上找了一大堆,都没有解决掉,这个异常是说要解析的对象解析不了,就有可能该对象为null了,为了测试,我把数据库的数据都填上去 结果还是报错 看来是时候debug下 ...
- QQ面板拖拽(慕课网DOM事件探秘)(上)
QQ面板拖拽,效果如图 JavaScript代码如下: function getByClass(clsName, parent) { var oParent = parent ? document.g ...
- 批量部署Hadoop集群环境(1)
批量部署Hadoop集群环境(1) 1. 项目简介: 前言:云火的一塌糊涂,加上自大二就跟随一位教授做大数据项目,所以很早就产生了兴趣,随着知识的积累,虚拟机已经不能满足了,这次在服务器上以生产环境来 ...
- php配置之include_path
在php.ini中配置include_path,可在引入文件时直接引入配置目录下的文件. 项目中就可以直接 引入/var/www/phpxwlib及/var/www/huicuiserver/libs ...
- mac下elasticsearch安装部署
下载elaticsearch集成包 优势:封装了对插件的支持,且安装方式较简单 地址:https://github.com/medcl/elasticsearch-rtf 解压到指定目录后,获取该集成 ...
- Spring中@Value的使用
- Hystrix 断路器
断路器: 当客户端访问服务端,发现服务端有异常不能进行访问时,就会执行一个fallback 方法.