soj4271 Love Me, Love My Permutation (DFS)
4271: Love Me, Love My Permutation
Description
Given a permutation of n: a[0], a[1] ... a[n-1], ( its elements range from 0 to n-1, For example: n=4, one of the permutation is 2310) we define the swap operation as: choose two number i, j ( 0 <= i < j <= n-1 ), take a[i] out and then insert a[i] to the back of a[j] of the initial permutation to get a new permutation :a[0], a[1], ..., a[i-1], a[i+1], a[i+2] ... a[j-1], a[j], a[i], a[j+1], ..., a[n-1]. For example: let n = 5 and the permutation be 03142, if we do the swap operation be choosing i = 1, j = 3, then we get a new permutation 01432, and if choosing i = 0, j = 4, we get 31420.
Given a confusion matrix of size n*n which only contains 0 and 1 (ie. each element of the matrix is either 0 or 1), the confusion value of a permutation a[0],a[1], ..., a[n-1] can be calculated by the following function:
int confusion()
{
int result = 0;
for(int i = 0;i < n-1; i++)
for(int j = i+1; j < n; j++)
{
result = result + mat[a[i]][a[j]];
}
return result;
}
Besides, the confusion matrix satisfies mat[i][i] is 0 ( 0 <= i < n) and mat[i][j] + mat[j][i] = 1 ( 0 <= i < n, 0 <= j < n, i != j ) given the n, the confusion matrix mat, you task is to find out how many permutations of n satisfies: no matter how you do the swap function on the permutation(only do the swap function once), its confusion value does not increase.
Input
The first line is the number of cases T(1 <= T <= 5), then each case begins with a integer n (2 <= n <= 12), and the next n line forms the description of the confusion matrix (see the sample input).
Output
For each case , if there is no permutation which satisfies the condition, just print one line “-1”, or else, print two lines, the first line is a integer indicating the number of the permutations satisfy the condition, next line is the Lexicographic smallest permutation which satisfies the condition.
Sample Input
2
2
0 1
0 0
3
0 0 0
1 0 1
1 0 0
Sample Output
1
0 1
1
1 2 0 题解:DFS
从后向前搜索,放进去的一位要和后面的每一段(n~n-1,n~n-2...n~0)保持1的个数大于0的个数,如果成立,则这一位可以放这个数,在向前继续搜索。
因为只能前面的数插入到后面,所以不需要考虑新加进去的数会不会对后面已经排好的短造成影响。也是这个原因要从后向前搜索。 AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std; int ans[25];
int num[25][25];
int out[25];
int n;
int ct; void print()
{
//cout<<'?'<<endl;
int flag = 1;
for(int i = n-1;i>=0;i--)
{
if(ans[i]<out[i]) {flag = 0; break;}
if(ans[i]>out[i]) break;
}
if(!flag)
for(int i = n-1;i>=0;i--)
out[i] = ans[i];
ct++;
} void dfs(int pos)
{
if(pos>=n)
{
print();
return;
}
if(pos == 0)
{
for(int i = 0;i<n;i++)
{
ans[pos] = i;
dfs(pos+1);
}
}
else
{
for(int i = 0;i<n;i++)
{
int cnt = 0;
int flag = 1;
for(int j = pos-1;j>=0;j--)
{ if(num[i][ans[j]]==0) {flag = 0;break;}
cnt += num[i][ans[j]];
//cout<<i<<' '<<ans[j]<<' '<<num[i][ans[j]]<<' '<<cnt<<endl;
if(cnt<0) {flag = 0;break;}
}
if(flag) {ans[pos] = i; dfs(pos+1);}
}
}
return;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ct = 0;
scanf("%d",&n);
M(num,0);
M(ans,0);
M(out,0x3f);
for(int i = 0;i<n;i++)
for(int j = 0;j<n;j++)
{
scanf("%d",&num[i][j]);
if(i == j) num[i][j] = 0;
else if(num[i][j]==0)
num[i][j] = -1;
}
dfs(0);
if(ct==0) {puts("-1"); continue;}
printf("%d\n",ct);
for(int j = n-1;j>0;j--)
printf("%d ",out[j]);
printf("%d\n",out[0]);
}
return 0;
}
soj4271 Love Me, Love My Permutation (DFS)的更多相关文章
- Tree and Permutation dfs hdu 6446
Problem Description There are N vertices connected by N−1 edges, each edge has its own length.The se ...
- codeforces 691D D. Swaps in Permutation(dfs)
题目链接: D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes inpu ...
- codeforces 691D Swaps in Permutation DFS
这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #in ...
- HNU Joke with permutation (深搜dfs)
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=13341&courseid=0 Joke with pe ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 1009 - Tree and Permutation 【dfs+树上两点距离和】
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs
题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...
- codeforces 285 D. Permutation Sum 状压 dfs打表
题意: 如果有2个排列a,b,定义序列c为: c[i] = (a[i] + b[i] - 2) % n + 1 但是,明显c不一定是一个排列 现在,给出排列的长度n (1 <= n <= ...
- leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码
Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...
- 【HDOJ6628】permutation 1(dfs)
题意:求1到n的排列中使得其差分序列的字典序为第k大的原排列 n<=20,k<=1e4 思路:爆搜差分序列,dfs时候用上界和下界剪枝 #include<bits/stdc++.h& ...
随机推荐
- java+tomcat(apr,native)
#pdd 2014_12-24#安装java环境rpm -ivh jdk-7u72-linux-x64.rpm vim /etc/profile #set for java export JAVA_H ...
- centos安装163源
cd /etc/yum.repos.d/ mv CentOS-Base.repo CentOS-Base.repo.cp wget http://mirrors.163.com/.help/CentO ...
- [iOS 图像处理相关]
//使用CGImage获取并修改图片像素 #define Mask8(x) ( (x) & 0xFF ) #define R(x) ( Mask8(x) ) #define G(x) ( Ma ...
- IDLE快捷键
Ctrl + Space 完成类.方法.变量名称的自动输入,这个快捷键是我最经常使用的快捷键了,它可以完成类.方法.变量名称的自动录入,很方便.(不过在我的电脑上和输入法冲突) Ctrl + N 快 ...
- wildfly 在 jee war 外部写配置文件
有时需要写属性文件,保存配置值,当然也可以写在数据库.这里我们用文件方式. 最简单做法: 写在wildfly的配置目录里面: File confDir = new File(System.getPro ...
- Linux下安装部署Java
Java安装6小部(适合新手) 1.下载软件包 # wget http://download.oracle.com/otn-pub/java/jdk/7u79-b15/jdk-7u79-linux-x ...
- #第一用Markdown编辑器#
Markdown初次使用 This is a simple Markdown editor based on 'Markdown' it's * italic * style. it's also _ ...
- Apache+php+mysql+SQLyog在windows7下的安装与配置图解
先准备好软件: Apache官方下载地址:httpd-2.2.25-win32-x86-openssl-0.9.8y.msi,更多版本在这里: php官方下载地址:php-5.4.37-Win32-V ...
- win 2012 关闭IE增强设置
- linux手动或者自动启动oracle11g的服务 Oracle 自动启动脚本
手动启动: [oracle@localhost ~]$ sqlplus SQL*Plus: Release 11.2.0.1.0 Production on Wed Mar 26 23:39:52 2 ...