C - Easy Game

区间DP

Description

You are playing a two player game. Initially there are n integer numbers in an array and player A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and player A starts the game then how much more point can player A get than player B?

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the size of the array. The next line contains N space separated integers. You may assume that no number will contain more than 4 digits.

Output

For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.

Sample Input

2

 

4

4 -10 -20 7

 

4

1 2 3 4

Sample Output

Case 1: 7

Case 2: 10

给你n个数,A、B两个人从两端取数,每一次可以取连续的一段数,而且每个人都按让自己最优的方案去取数(即取的数的和尽可能大)

问最后A与B的差值最大是多少

解法:枚举区间长度,从小区间DP到大区间 

对于一段区间,枚举分隔点,要么取左边,要么取右边


#include<cstdio>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
int sum[110];
int dp[110][110];
int main()
{
	int r,n,ca=1;
	scanf("%d",&r);
	while(r--)
	{
		scanf("%d",&n);
		sum[0]=0;
	    memset(dp,0,sizeof(dp));
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&sum[i]);
			dp[i][i]=sum[i];           //
			sum[i]+=sum[i-1];
		}
		for(int len=1;len<n;len++)
		{
			for(int i=1;i+len<=n;i++)
			{
				dp[i][i+len]=sum[i+len]-sum[i-1];
				for(int j=i;j<i+len;j++)        //j不能到i+len 
				{
					dp[i][i+len]=max(dp[i][i+len],sum[j]-sum[i-1]-dp[j+1][i+len]);
					dp[i][i+len]=max(dp[i][i+len],sum[i+len]-sum[j]-dp[i][j]);       //
				}
			}
		}
		printf("Case %d: %d\n",ca++,dp[1][n]);
	}
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个简单的俄罗斯方块游戏的代码示例: ```c++ #include <iostream> #include <vector> #include <cstdlib> #include <ctime> #include <conio.h> #include <windows.h> using namespace std; const int ROW = 30; const int COL = 20; int score; int map[ROW][COL]; struct Point { int x, y; Point(int x = 0, int y = 0) :x(x), y(y) {} }; vector<Point> curShape; vector<vector<Point>> shapes; void initShapes() { shapes.push_back({ {0,0}, {0,1}, {0,2}, {0,3} }); shapes.push_back({ {0,0}, {0,1}, {0,2}, {1,2} }); shapes.push_back({ {0,0}, {0,1}, {1,0}, {1,1} }); shapes.push_back({ {0,0}, {0,1}, {0,2}, {1,1} }); shapes.push_back({ {0,0}, {0,1}, {1,1}, {1,2} }); shapes.push_back({ {0,0}, {1,0}, {1,1}, {2,1} }); shapes.push_back({ {0,1}, {1,1}, {2,0}, {2,1} }); } void newShape() { curShape.clear(); srand((unsigned)time(NULL)); int index = rand() % shapes.size(); for (int i = 0; i < 4; i++) { Point p = shapes[index][i]; curShape.push_back(Point(p.x + ROW / 2, p.y)); } } bool check(Point p) { if (p.x < 0 || p.x >= ROW || p.y >= COL) return false; if (map[p.x][p.y]) return false; return true; } bool isOver() { for (int i = 0; i < 4; i++) { if (!check(curShape[i])) return true; } return false; } void transform(Point& p) { int tmp = p.x; p.x = p.y; p.y = 3 - tmp; } void rotate() { Point center = curShape[1]; for (int i = 0; i < 4; i++) { if (i == 1) continue; int x = curShape[i].x - center.x; int y = curShape[i].y - center.y; Point t(x, y); transform(t); curShape[i].x = center.x + t.x; curShape[i].y = center.y + t.y; } } void updateMap() { for (int i = 0; i < 4; i++) { int x = curShape[i].x; int y = curShape[i].y; map[x][y] = 1; } } bool full(int r) { for (int c = 0; c < COL; c++) { if (map[r][c] == 0) return false; } return true; } void removeRow(int r) { for (int c = 0; c < COL; c++) { map[r][c] = 0; } for (int i = r; i >= 1; i--) { for (int c = 0; c < COL; c++) { map[i][c] = map[i - 1][c]; } } for (int c = 0; c < COL; c++) { map[0][c] = 0; } score++; } void checkFull() { for (int r = ROW - 1; r >= 0; r--) { if (full(r)) { removeRow(r); r++; } } } void refresh() { system("cls"); cout << "score: " << score << endl; for (int i = 0; i < ROW; i++) { for (int j = 0; j < COL; j++) { if (map[i][j]) cout << "*"; else cout << " "; } cout << endl; } } void moveLeft() { for (int i = 0; i < 4; i++) { Point p = curShape[i]; p.y--; if (!check(p)) { return; } } for (int i = 0; i < 4; i++) { curShape[i].y--; } } void moveRight() { for (int i = 0; i < 4; i++) { Point p = curShape[i]; p.y++; if (!check(p)) { return; } } for (int i = 0; i < 4; i++) { curShape[i].y++; } } void moveDown() { if (isOver()) { cout << "Game Over!" << endl; exit(0); } for (int i = 0; i < 4; i++) { Point p = curShape[i]; p.x++; if (!check(p)) { updateMap(); checkFull(); newShape(); return; } } for (int i = 0; i < 4; i++) { curShape[i].x++; } } int main() { initShapes(); newShape(); while (1) { refresh(); Sleep(300); if (_kbhit()) { int c = _getch(); switch (c) { case 'a': moveLeft(); break; case 'd': moveRight(); break; case 'w': rotate(); break; case 's': moveDown(); break; default: break; } } else { moveDown(); } } return 0; } ``` 注意:此代码是通过 C++ 开发,需要在 Windows 系统下使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值