ZOJ-3894-Chessgame【区间dp】
来源:互联网 发布:港口历年数据 编辑:程序博客网 时间:2024/06/05 03:53
ZOJ-3894-Chessgame
Time Limit: 4 Seconds Memory Limit: 65536 KB
Today, Bob wants to play a game on a chessboard of 1*n.
At first, n chessmen, painted in either White or Black, are placed on the chessboard. Every time, Bob can choose a chessman. If the left chessman and the right chessman of the chosen one(ignore the blank cause by previous removing) are of the same color, Bob can remove the two chessmen(except the chessman he choose) from the chessboard and get two points.
For example, in the following picture, if Bob choose the black chessman in the third cell, he can remove the two white chessmen and get two points. But the chessman he choose is still at the chessboard.
With different ways of choosing, Bob can get different points. Now, Bob wants to get as much points as possible. Can you tell him the most points he can get.
Input
The first line of input is a integer T, the number of games Bob plays. For each game he plays, the first line is the length of the chessboard n(n<=200) and the second line is a string of ‘B’ and ‘W’ which represent the color of the chessmen.
Output
For each game Bob plays, output the most points he can get in a line.
Sample Input
2
5
BWBBW
3
BBB
Sample Output
4
2
Hint
For the first game, Bob can first choose the white chessman in the second cell and remove the chessmen in the first and third cell, the chessboard become #W#BW. Then, he can choose the black chessman in the forth cell and remove the two white chessman.
题目链接:ZOJ 3894
题目思路:区间dp
以下是代码:
#include <vector>#include <map>#include <set>#include <algorithm>#include <iostream>#include <cstdio>#include <cmath>#include <cstdlib>#include <string>#include <cstring>using namespace std;int dp[205][205][205];int ans[205];int main(){ int t; cin >> t; while(t--) { int n; cin >> n; string s; cin >> s; memset(dp,0,sizeof(dp)); memset(ans,0,sizeof(ans)); for (int i = 0; i < n; i++) { if (s[i] == 'B') dp[i][i][0] = 1; else dp[i][i][1] = 1; } for (int j = 0; j < n; j++) { for (int i = j; i>=0; --i) { for (int u = i; u <= j; u++) { for (int v = u+2; v <= j; v++) { if (dp[i][j][0] == 0) dp[i][j][0] = ((dp[i][u][0] && dp[v][j][0]) || (dp[i][u][1] && dp[v][j][1])) && (dp[u+1][v-1][0]); if (dp[i][j][1] == 0) dp[i][j][1] = ((dp[i][u][0] && dp[v][j][0]) || (dp[i][u][1] && dp[v][j][1])) && (dp[u+1][v-1][1]); } } } } for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { if (dp[j][i][0] || dp[j][i][1]) ans[i] = max(ans[i],ans[j] + (i - j + 1) - 1); else ans[i] = max(ans[i],ans[j]); } } cout << ans[n - 1] << endl; } return 0;}
- ZOJ-3894-Chessgame【区间dp】
- Zoj 3537 区间DP
- ZOJ 3160 区间DP
- Zoj 3537(区间DP)
- ZOJ 3469 区间dp
- ZOJ 3537 区间DP
- zoj 3469 区间dp
- ZOJ 3469(区间DP)
- ZOJ 3537 区间dp
- ZOJ 3469 区间DP
- ZOJ 2744 Palindromes 区间DP
- zoj 3160(区间dp 简单)
- ZOJ Problem Set - 3469(区间dp)
- ZOJ 3469 Food Delivery / 区间DP
- ZOJ 3469 Food Delivery (区间dp)
- zoj 3469 Food Delivery(区间dp)
- zoj 3469 Food Delivery(区间dp)
- ZOJ 3469 - Food Delivery(区间DP)
- 求一个最长的串使得该串不包含任何禁止串为子串 AC自动机+DP +dfs判环 UVA 1399 - Puzzle
- 上下Div固定,中间div高度自适应(随浏览器高度变化而变化)
- C语言 switch和if..else的区别 以及枚举的作用
- UVA Poker Hands题解
- [深入理解Java虚拟机]第一章 走进Java
- ZOJ-3894-Chessgame【区间dp】
- C++primer第五版笔记-第七章类
- 笔记:学习 Android -Services 基础
- 算法中可以使用不等式变换避免溢出,leetcode:Sqrt(x) ,BigInteger处理溢出
- 直到谷歌重组,我才明白谷歌和百度不止隔着一个太平洋
- CF#321-D - Kefa and Dishes-状压DP+bitmasks
- Android开发之基础-------------Get请求和Post请求
- hdu 1241 Oil Deposits(dfs)
- 从酷狗的网络红歌说起