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 ...
随机推荐
- 「Luogu4321」随机游走
「Luogu4321」随机游走 题目描述 有一张 \(n\) 个点 \(m\) 条边的无向图,\(Q\) 组询问,每次询问给出一个出发点和一个点集 \(S\) ,求从出发点出发随机游走走遍这个点集的期 ...
- Codeforces 622 F. The Sum of the k-th Powers
\(>Codeforces \space 622\ F. The\ Sum\ of\ the\ k-th\ Powers<\) 题目大意 : 给出 \(n, k\),求 \(\sum_{i ...
- [CF1043G]Speckled Band
题意:给定字符串$s_{1\cdots n}$,多次询问它的一个子串$s_{l\cdots r}$能否被切割成多个部分,使得至少有一个部分出现两次,且切出来的本质不同字符串数最少 做一道题学了两个算法 ...
- 【DP】BZOJ1592-[Usaco2008 Feb]Making the Grade 路面修整
我活着从期中考试回来了!!!!!!!!!备考NOIP!!!!!!!!! [题目大意] 给出n个整数a1~an,修改一个数的代价为修改前后差的绝对值,问修改成不下降序列或者不上升序列的最小总代价. [思 ...
- Codeforces Beta Round #8 C. Looking for Order 状压
C. Looking for Order 题目连接: http://www.codeforces.com/contest/8/problem/C Description Girl Lena likes ...
- 关于收到谷歌邮件 Googlebot can't access your site 的解决方法
最近一段时间一直都收到谷歌的邮件,而且“新锐工作室”的关键字在谷歌收录及排名都没有了.下面图为谷歌蜘蛛无法抓取网站的截图,如果你在谷歌网管工具里收到类似消息,说明也中招了.[Webmaster Too ...
- CentOS 6.9/7通过yum安装指定版本的PostgreSQL扩展PostGIS
一.安装PostGIS扩展插件(24_10) // 安装EPEL源 # rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-rele ...
- keystone 命令简要说明
catalog: keystone catalog 可以显示所有已有的service keystone catalog --service service-type 显示某个service信息 end ...
- Druid 常见问题
https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
- (转)h264中avc和flv数据的解析
计算 AVCDecoderConfigurationRecord 得到 CodecPrivateData 数据(只有第一帧需要): 计算 NALUs 得到帧数据. 计算 AVCDecoderConf ...