Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
E. Arrange Teams
Syrian Collegiate Programming Contest (SCPC) is the qualified round for the Arab Collegiate Programming Contest. Each year SCPC organizers face a problem that wastes a lot of time to solve it, it is about how should they arrange the teams in the contest hall.
Organizers know that they have t teams and n*m tables allocated in n rows and m columns in the contest hall. They don't want to put teams in a random way. Each year SCPC chief judge puts a list of paired teams (list of a,b teams) that should not sit next to each other (because they are so good or so bad!).
if pair (a,b) is in chief judge list, this means that:
- team number a should not sit in-front of team number b
- team number b should not sit in-front of team number a
- team number a should not sit right to team number b
- team number b should not sit right to team number a
Organizers wastes a lot of time to find a good team arrangement that satisfy all chief judge needs. This year they are asking you to write a program that can help them.
First line contains number of test cases. The first line in each test case contains three numbers: (1 ≤ n,m ≤ 11) and (1 ≤ t ≤ 10). Second line in each test case contains (0 ≤ p ≤ 40) number of pairs. Then there are p lines, each one of them has two team numbers a and b (1 ≤ a,b ≤ t) where a is different than b.
For each test case, print one line contains the total number of teams arrangements that satisfy all chief judge needs (We guarantee that it will be less than 9,000,000 for each test case). If there is no suitable arrangements print "impossible".
2
1 3 2
1
1 2
2 2 4
2
1 2
1 3
2
impossible
In test case 1 there are 2 teams and 3 tables in one row at the contest hall. There are only one pair (1,2), so there are 2 solutions:
team1 then empty table then team2
team2 then empty table then team1
In test case 2 there are 4 tables in 2 rows and 2 columns, and there are 4 teams. There is no arrangement that can satisfy chief judge needs.
题目链接:http://codeforces.com/gym/100952/problem/E

分析:dfs、剪枝
首先用dp[vis[a][b]][k]布尔数组双向的记录那些vis[i][j-1],vis[i][j+1],vis[i-1][j],vis[i+1][j];a=i-1,i+1,i,i;b=j,j,j-1,j+1;
然后inline void dfs(int k)表示当前正在处理队伍k,然后遍历所有i, j如果没有访问过,并且满足条件则dfs(k + 1)
直到顺利的把所以的t个队伍都填进去了,那一个分枝才ans++; return;
剪枝以后的复杂度不大算的出来,但看数据大小 (1 ≤ n,m ≤ 11) and (1 ≤ t ≤ 10) 这样做一般可以AC
下面给出AC代码:
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')
f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x>)
write(x/);
putchar(x%+'');
}
int n,m,t,ans;
int vis[][];
bool dp[][];
inline void DFS(int k)
{
if(k==t+)
{
ans++;
return;
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(vis[i][j])
continue;
if(!dp[vis[i-][j]][k]&&!dp[vis[i+][j]][k]&&!dp[vis[i][j-]][k]&&!dp[vis[i][j+]][k])
{
vis[i][j]=k;
DFS(k+);
vis[i][j]=;
}
}
}
}
int main()
{
int T,q,x,y;
T=read();
while(T--)
{
ans=;
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
n=read();
m=read();
t=read();
q=read();
for(int i=;i<q;i++)
{
x=read();
y=read();
dp[x][y]=;
dp[y][x]=;
}
DFS();
if(ans!=)
write(ans),printf("\n");
else printf("impossible\n");
}
return ;
}
Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】的更多相关文章
- Gym 100952F&&2015 HIAST Collegiate Programming Contest F. Contestants Ranking【BFS+STL乱搞(map+vector)+优先队列】
F. Contestants Ranking time limit per test:1 second memory limit per test:24 megabytes input:standar ...
- Gym 100952J&&2015 HIAST Collegiate Programming Contest J. Polygons Intersection【计算几何求解两个凸多边形的相交面积板子题】
J. Polygons Intersection time limit per test:2 seconds memory limit per test:64 megabytes input:stan ...
- Gym 100952I&&2015 HIAST Collegiate Programming Contest I. Mancala【模拟】
I. Mancala time limit per test:3 seconds memory limit per test:256 megabytes input:standard input ou ...
- Gym 100952H&&2015 HIAST Collegiate Programming Contest H. Special Palindrome【dp预处理+矩阵快速幂/打表解法】
H. Special Palindrome time limit per test:1 second memory limit per test:64 megabytes input:standard ...
- Gym 100952G&&2015 HIAST Collegiate Programming Contest G. The jar of divisors【简单博弈】
G. The jar of divisors time limit per test:2 seconds memory limit per test:64 megabytes input:standa ...
- Gym 100952D&&2015 HIAST Collegiate Programming Contest D. Time to go back【杨辉三角预处理,组合数,dp】
D. Time to go back time limit per test:1 second memory limit per test:256 megabytes input:standard i ...
- Gym 100952C&&2015 HIAST Collegiate Programming Contest C. Palindrome Again !!【字符串,模拟】
C. Palindrome Again !! time limit per test:1 second memory limit per test:64 megabytes input:standar ...
- Gym 100952B&&2015 HIAST Collegiate Programming Contest B. New Job【模拟】
B. New Job time limit per test:1 second memory limit per test:64 megabytes input:standard input outp ...
- Gym 100952A&&2015 HIAST Collegiate Programming Contest A. Who is the winner?【字符串,暴力】
A. Who is the winner? time limit per test:1 second memory limit per test:64 megabytes input:standard ...
随机推荐
- 【java】控制台实现贪吃蛇小游戏-LinkedList、Scanner
package com.myproj.snake; public class Node { private int i,j; public Node(){} public Node(int i, in ...
- JAVA多线程统计日志计数时的线程安全及效率问题
最近工作上遇到一个需求:需要根据nginx日志去统计每个域名的qps(Query Per Second,每秒查询率)数据. 解决了日志读取等问题之后,为了写一个尽可能高效的统计模块,我决定用多线程去计 ...
- go defer (go延迟函数)
go defer (go延迟函数) Go语言的defer算是一个语言的新特性,至少对比当今主流编程语言如此.根据GO LANGUAGE SPEC的说法: A "defer" sta ...
- bzoj 4898: [Apio2017]商旅
Description 在广阔的澳大利亚内陆地区长途跋涉后,你孤身一人带着一个背包来到了科巴.你被这个城市发达而美丽的市场所 深深吸引,决定定居于此,做一个商人.科巴有个集市,集市用从1到N的整数编号 ...
- Nginx 错误处理方法: bind() to 0.0.0.0:80 failed
Nginx 错误处理方法: bind() to 0.0.0.0:80 failed 今天启动window上的nginx总是报错 错误信息是bind() to 0.0.0.0:80 failed (10 ...
- 正则表达式与grep
一.回溯引用 1.将页面中合法的标题找出来,使用回溯引用匹配 (需要使用 -E 或 -P 来扩展grep语法支持) 2.查找连续出现的单词 二.前后查找 (grep 只能使用 -P 选项) 1. 向前 ...
- 【算法设计与分析基础】24、kruskal算法详解
首先我们获取这个图 根据这个图我们可以得到对应的二维矩阵图数据 根据kruskal算法的思想,首先提取所有的边,然后把所有的边进行排序 思路就是把这些边按照从小到大的顺序组装,至于如何组装 这里用到并 ...
- Xamarin~Android篇~监听返回键,单击返回某个webView,双击退出
https://www.cnblogs.com/lori/p/5088627.html DateTime? lastBackKeyDownTime; public override bool OnKe ...
- jQuery Ajax post多个值传参
http://blog.csdn.net/wang8559422/article/details/42394839 data:'id='+data+'&val='+val 加&符 ...
- C#-判断Shift,Alt,Ctrl是否被按下,确定所按下的组合键
在创建接受用户击键的应用程序时,您还可能希望监视 SHIFT.ALT 和 CTRL 键等组合键.当一个组合键与其他键同时按下,或在单击鼠标的同时按下时,您的应用程序能够做出适当响应:字母 S 可能仅导 ...