POJ 2358|URAL 1034|Queens in Peaceful Positions
来源:互联网 发布:linux sleep 1s 编辑:程序博客网 时间:2024/06/11 04:20
原文地址:http://acm.timus.ru/problem.aspx?space=1&num=1034
题目
一个
输入
输入第一行一个整数
接下来
输出
输出一行一个整数,表示有多少种改变皇后位置的方案满足条件(所有皇后是等价的,如果你只交换2个皇后,不算一种新方案)。
样例输入
42 11 33 44 2
样例输出
0
题解
考虑到每行每列都有一个皇后,改变3个皇后的位置的话,如果全改到一行里肯定是不合法的,因此我们只横向移动皇后(即改变y坐标,当然你只纵向移动也没问题,我们这里规定横向移动,这样竖向的移动其实是没有意义的,比如2行皇后调到4行,4行的皇后调到了2行【当然调到其他地方去就冲突不合法了】,相当于2行和4行的皇后横向调整)。然后因为N很小,所以我们枚举3个皇后,交换她们的横坐标(y坐标),这样横纵方向都是满足条件的,只要再判断斜方向是否冲突即可。
#include <cstdio>#include <cstring>#define FOR(i,j,k) for(i=j;i<=k;++i)#define rep(i,j,k) for(i=j;i<k;++i)int col[64], rb[256], rt[256];int n;inline void roll(int i, int j, int k) { int a = col[i], b = col[j], c = col[k]; col[i] = b; col[j] = c; col[k] = a;}inline bool judge() { int i; memset(rb, 0, sizeof rb); memset(rt, 0, sizeof rt); FOR(i,1,n) { if (rb[i + col[i] + 128]) return false; if (rt[i - col[i] + 128]) return false; rb[i + col[i] + 128] = true; rt[i - col[i] + 128] = true; } return true;}int main() { int x, y, i, j, k, s = 0; scanf("%d", &n); FOR(i,1,n) scanf("%d%d", &x, &y), col[x] = y; FOR(i,1,n) rep(j,1,i) rep(k,1,j) { roll(i, j, k); if (judge()) ++s; roll(i, j, k); if (judge()) ++s; roll(i, j, k); } printf("%d\n", s); return 0;}
阅读全文
0 0
- POJ 2358|URAL 1034|Queens in Peaceful Positions
- Codeforces Round #175 DIV2 E Positions in Permutations
- Codeforces Round #175 (Div. 2) E Positions in Permutations
- 20060610-Determining point positions in MRI peg phantom
- 7.5.2 Point-in-Time Recovery Using Event Positions
- Codeforces 285E - Positions in Permutations 【题解待补全】
- Codeforces 285E Positions in Permutations (dp + 容斥)
- URAL 1025. Democracy in danger
- ural 1025 Democracy in Danger
- ural 1905 Travel in Time
- URAL 1314. Chase in Subway
- Ural 1962 In Chinese Restaurant
- URAL 1534 Football in Gondor
- URAL 1785 Lost in Localization
- N-queens generator implementation in Racket
- 【Leetcode】:51. N-Queens 问题 in JAVA
- Queens
- poj 2354 || URAL 1030 Titanic
- 谈谈HTTP的流程
- Spring mvc注解配置的背后
- Spring Cache抽象-基于XML的配置声明(基于ConcurrentMap的配置)
- 小C语言--词法分析程序
- CodeForces 865D Buy Low Sell High(思维)
- POJ 2358|URAL 1034|Queens in Peaceful Positions
- 第四周项目4 猴子选大王
- Java并发编程 03 Java 线程状态图
- 最大子数组
- big (Trie)
- poj 3494 Largest Submatrix of All 1’s
- Spring Aop(五)——给Advice传递参数
- Git使用教程
- 51nod 1052 最大M子段和 (区间dp)