uva10401Injured Queen Problem(递推)
题目:uva10401Injured Queen Problem(递推)
题目大意:依然是在棋盘上放皇后的问题,这些皇后是受伤的皇后,攻击范围缩小了。攻击范围在图中用阴影表示(题目)。然后给出棋盘的现状,???3?4:在一个6*6的棋盘上,由于皇后是能够列向攻击的,所以一列仅仅能放一个皇后,所以第一个?代表第一列的皇后放的行未知,这样3的意思就是第4列皇后在第三行。也就是确定了第4列皇后的位置。
要求棋盘上的皇后不互相攻击。问这种棋盘能够有多少不同的放法。
解题思路:一開始想状态。想把不同的棋盘作为状态,可是发现这种状态太多了。
后面才发现由于是求总共的数目,那么在放第i个皇后的时候,事实上前面的i - 1个皇后我们仅仅须要知道有多少种放法就能够,并不须要将i - 1个皇后在棋盘上不同的情况都记录下来。依据皇后的攻击范围写出状态方程:d【i】【j】代表第i列的皇后放在第j行。d【i】【j】 = sum (d【i - 1】【k】)
k >= 0 && k < n(棋盘的行或列) && abs (k - j ) > 1.
代码:
#include <cstdio>
#include <cstring>
#include <cstdlib> typedef long long ll;
const int N = 20; ll dp[N][N];
char str[N];
int len; void init () { memset (dp, 0, sizeof (dp));
for (int i = 0; i < len; i++)
dp[0][i] = 1;
} int translate (int i) { if (str[i] >= '1' && str[i] <= '9')
return str[i] - '1';
else { switch (str[i]) {
case 'A' : return 9;
case 'B' : return 10;
case 'C' : return 11;
case 'D' : return 12;
case 'E' : return 13;
case 'F' : return 14;
}
}
} void handle (int i, int j) { for (int k = 0; k < len; k++) {
if (abs (k - j) > 1)
dp[i][j] += dp[i - 1][k];
}
} int main () { while (scanf ("%s" , str) != EOF) { len = strlen (str);
init (); for (int i = 1; i < len; i++) { if (str[i - 1] == '?') {
for (int j = 0; j < len; j++)
handle(i, j); } else { int k = translate (i - 1);
for (int j = 0; j < len; j++) { if (abs (j - k) > 1)
dp[i][j] += dp[i - 1][k];
}
}
} ll ans;
if (str[len - 1] != '?')
ans = dp[len - 1][translate(len - 1)];
else { ans = 0;
for (int i = 0; i < len; i++)
ans += dp[len - 1][i];
} printf ("%lld\n", ans);
}
return 0;
}
uva10401Injured Queen Problem(递推)的更多相关文章
- 递推DP HDOJ 5328 Problem Killer
题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- 【主席树维护mex】 【SG函数递推】 Problem H. Cups and Beans 2017.8.11
Problem H. Cups and Beans 2017.8.11 原题: There are N cups numbered 0 through N − 1. For each i(1 ≤ i ...
- HDU 1757 A Simple Math Problem 【矩阵经典7 构造矩阵递推式】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1757 A Simple Math Problem Time Limit: 3000/1000 MS (J ...
- EOJ Problem #3249 状态压缩+循环周期+反向递推
限量供应 Time limit per test: 4.0 seconds Time limit all tests: 4.0 seconds Memory limit: 256 megabytes ...
- hdu3483 A Very Simple Problem 非线性递推方程2 矩阵快速幂
题目传送门 题目描述:给出n,x,mod.求s[n]. s[n]=s[n-1]+(x^n)*(n^x)%mod; 思路:这道题是hdu5950的进阶版.大家可以看这篇博客hdu5950题解. 由于n很 ...
- ACM学习历程—SNNUOJ 1116 A Simple Problem(递推 && 逆元 && 组合数学 && 快速幂)(2015陕西省大学生程序设计竞赛K题)
Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N- ...
- hdu 1757 A Simple Math Problem (构造矩阵解决递推式问题)
题意:有一个递推式f(x) 当 x < 10 f(x) = x.当 x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + ...
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
随机推荐
- Hibernate 过滤查询(hibernate过滤器的使用)
我们在开发过程中过滤查询使用的还是挺多的,今天来学习一下hibernate的过滤器的使用,首先学习在配置文件中如何使用,然后再介绍如何使用注解配置. 1.使用配置文件配置过滤器 1)首先我们使用my ...
- HDU 1213(并查集)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- dp洋洋散散的知识+code
/*在数轴上有0-N的位置 从0出发每次可以向右走 2 23 233步*/ // 1 总共的方案数 f[i]=f[i-]+f[i-]+f[i-]; f[]=; ;a<=n;a++) { ) f[ ...
- [BZOJ4009][HNOI2015]接水果(整体二分)
[HNOI2015]接水果 时间限制:60s 空间限制:512MB 题目描述 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果. 由于她已经DT FC 了The b ...
- 去除List中的重复元素
/** * 去重list中的重复元素 * @param list * @return */ public static <T> List<T> removeRepeat(Lis ...
- Dubbo整合SpringCloud图片显示问题
Dubbo整合SpringCloud图片显示问题 Tips:公司项目,记录一点经验吧,理解的不对的地方欢迎大神指点 问题:商品图片上传功能(公司没有专门文件服务器)写的保存目录直接是保存在docker ...
- phpRedis函数使用总结
/*1.Connection*/ $redis = new Redis(); $redis->connect('127.0.0.1',6379,1);//短链接,本地host,端口为6379,超 ...
- Single transistor provides short-circuit protection
In certain dc/dc-converter applications, on-chip, cycle-by-cycle current limit may be insufficient p ...
- Hyper-v: Snapshot merge
我有一个Hyper-V上的虚拟机, 在使用的过程中我给这个虚拟机创建了多个snapshots. 有一天我把整个的snapshots tree从root删掉了(delete snapshot with ...
- Effective C++:条款29:为“异常安全”而努力是值得的
(一)先看以下这些代码: class PrettyMenu { public: void changeBackground(istream& imgSrc); private: Mutex m ...