Gym 100952E E. Arrange Teams dfs、剪枝
2016-08-11 01:14
465 查看
E - E
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice Gym
100952E
Description
standard input/output
Statements
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.
Input
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.
Output
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".
Sample Input
Input
Output
Hint
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.
Source
UESTC 2016 Summer Training #21
Gym 100952E
My Solution
dfs、剪枝
首先用 pai[][]布尔数组双向的记录 那些 pair
然后void dfs(int k) 表示当前正在处理 队伍 k, 然后遍历 所有 i, j 如果没有访问过, 并且满足条件 则dfs(k + 1)
直到顺利的把所以的t个队伍都填进去了, 那一个分枝才ans++; return;
剪枝以后的复杂度不大算的出来, 嘿嘿, 但看数据大小 (1 ≤ n,m ≤ 11) and (1 ≤ t ≤ 10) 这样做一般可以pass the tests
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 16;
int n, m, t, ans, vis[maxn][maxn];
bool pai[maxn][maxn];
void dfs(int k)
{
if(k == t + 1) {ans++; return;}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(vis[i][j]) continue;
if(!pai[vis[i-1][j]][k] && !pai[vis[i+1][j]][k]
&& !pai[vis[i][j-1]][k] && !pai[vis[i][j+1]][k]){
vis[i][j] = k;
dfs(k + 1);
vis[i][j] = 0;
}
}
}
}
int main()
{
#ifdef LOCAL
freopen("a.txt", "r", stdin);
//freopen("b.txt", "w", stdout);
#endif // LOCAL
int T, q, x, y;
scanf("%d", &T);
while(T--){
ans = 0;
memset(pai, false, sizeof pai);
memset(vis, 0, sizeof vis);
scanf("%d%d%d%d", &n, &m, &t, &q);
//!前面少打了一个 %d 然后运行了60多秒才输出结果, 关键是有些答案还是正确的, 又是一条智障的经验啊 (┬_┬)
for(int i = 0; i < q; i++){
scanf("%d%d", &x, &y);
pai[x][y] = true;
pai[y][x] = true;
}
dfs(1);
if(ans != 0) printf("%d\n", ans);
else printf("impossible\n");
}
return 0;
}
Thank you!
------from ProLights
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice Gym
100952E
Description
standard input/output
Statements
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.
Input
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.
Output
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".
Sample Input
Input
2 1 3 2 1 1 2 2 2 4 2 1 2 1 3
Output
2 impossible
Hint
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.
Source
UESTC 2016 Summer Training #21
Gym 100952E
My Solution
dfs、剪枝
首先用 pai[][]布尔数组双向的记录 那些 pair
然后void dfs(int k) 表示当前正在处理 队伍 k, 然后遍历 所有 i, j 如果没有访问过, 并且满足条件 则dfs(k + 1)
直到顺利的把所以的t个队伍都填进去了, 那一个分枝才ans++; return;
剪枝以后的复杂度不大算的出来, 嘿嘿, 但看数据大小 (1 ≤ n,m ≤ 11) and (1 ≤ t ≤ 10) 这样做一般可以pass the tests
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 16;
int n, m, t, ans, vis[maxn][maxn];
bool pai[maxn][maxn];
void dfs(int k)
{
if(k == t + 1) {ans++; return;}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(vis[i][j]) continue;
if(!pai[vis[i-1][j]][k] && !pai[vis[i+1][j]][k]
&& !pai[vis[i][j-1]][k] && !pai[vis[i][j+1]][k]){
vis[i][j] = k;
dfs(k + 1);
vis[i][j] = 0;
}
}
}
}
int main()
{
#ifdef LOCAL
freopen("a.txt", "r", stdin);
//freopen("b.txt", "w", stdout);
#endif // LOCAL
int T, q, x, y;
scanf("%d", &T);
while(T--){
ans = 0;
memset(pai, false, sizeof pai);
memset(vis, 0, sizeof vis);
scanf("%d%d%d%d", &n, &m, &t, &q);
//!前面少打了一个 %d 然后运行了60多秒才输出结果, 关键是有些答案还是正确的, 又是一条智障的经验啊 (┬_┬)
for(int i = 0; i < q; i++){
scanf("%d%d", &x, &y);
pai[x][y] = true;
pai[y][x] = true;
}
dfs(1);
if(ans != 0) printf("%d\n", ans);
else printf("impossible\n");
}
return 0;
}
Thank you!
------from ProLights
相关文章推荐
- 【Gym - 101164I】Cubes(dfs,剪枝)
- Gym 100952E&&2015 HIAST Collegiate Programming Contest E. Arrange Teams【DFS+剪枝】
- POJ 1724 ROADS【DFS+剪枝】
- HDU OJ 1010 dfs+剪枝
- ACM-ICPC 2017 Asia Xi'an LOL dfs剪枝|dp
- hdoj1242(dfs 剪枝 解法)
- UVA - 307 Sticks(dfs + 剪枝)
- 一个简单的DFS(深度优先搜索)算法加剪枝技术的程序演示
- Game Show Math +uva+dfs+剪枝
- 朴素搜索dfs, 简单的剪枝搜索
- hdu1010 dfs+路径剪枝
- D. Lizards and Basements 2(DFS+最优性剪枝)
- 天梯赛L3 015 - 球队“食物链” (DFS剪枝)
- URAL 1156 Two Rounds (DFS二分染色 + DFS枚举 + 剪枝)
- HDU 1518 Square (DFS+剪枝)
- POJ 2362 dfs 剪枝
- Codeforces Gym 101174 K. Balls and Needles (DFS)
- poj 1011-小白算法练习 Sticks 从最简单的做起 dfs+剪枝
- 【DFS&&搜索剪枝】CODE[VS] 3498 小木棍
- hdu 1010 Tempter of the Bone (dfs + 剪枝)