ZOJ-3894-Chessgame【区间dp】
2015-10-03 15:58
330 查看
ZOJ-3894-Chessgame
[code] 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
以下是代码:
[code]#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; }
相关文章推荐
- html5移动开发--js温馨提示
- iOS_UIAlertController
- UVA Poker Hands题解
- 上下Div固定,中间div高度自适应(随浏览器高度变化而变化)
- synchronized关键字实例解析
- Android开发之基础------------------网络编程(一)
- equals ()与 hashcode() 与 == 解析
- Python学习笔记<list,tuple,range再探>
- 5-4 是否同一棵二叉搜索树 (25分)
- 友盟统计不生效问题查找与解决
- Python学习笔记<list,tuple,range再探>
- 142. Linked List Cycle II (List; Two-Pointers)
- pat1017Queueing at Bank (25)
- Wow! Such Doge!---hdu4847(字符串水题)
- 【kmp】算法总结
- 141. Linked List Cycle (List; Two-Pointers)
- Count Complete Tree Nodes
- [摘要]Effective Objective-C 2.0(四)
- 对于JVM的浅解
- ZOJ-3897-Fiddlesticks