【u108】取数游戏
2017-10-04 18:45
127 查看
Time Limit: 1 second
Memory Limit: 128 MB
【问题描述】
一个N×M的由非负整数构成的数字矩阵,你需要在其中取出若干个数字,使得取出的任意两个数字不相邻(若一个数字在另外一个数字相邻8个格子中的一个即认为这两个数字相邻),求取出数字和最大是多少。
【输入格式】
输入文件number.in的第1行有一个正整数T,表示了有T组数据。 对于每一组数据,第1行有两个正整数N和M,表示了数字矩阵为N行M列。 接下来N行,每行M个非负整数,描述了这个数字矩阵。 。
【输出格式】
输出文件number.out包含T行,每行一个非负整数,输出所求得的答案。
【数据规模】
对于20%的数据,N, M≤3; 对于40%的数据,N, M≤4; 对于60%的数据,N, M≤5; 对于100%的数据,N, M≤6,T≤20。
Sample Input1
3
4 4
67 75 63 10
29 29 92 14
21 68 71 56
8 67 91 25
2 3
87 70 85
10 3 17
3 3
1 1 1
1 99 1
1 1 1
Sample Output1
271
172
99
【样例说明】
对于第1组数据,取数方式如下:
*67 75 63 10 29 29 *92 14 *21 68 71 56 8 67 *91 25
【题目链接】:http://noi.qz5z.com/viewtask.asp?id=u108
【题解】
dfs
n和m都不大、一格一格地枚举该格要不要取数字就好;
从左到右从上到下;
如果该格取数了,直接往右跳2格;没取数就往右跳1格;
这样我们在判断这个地方能不能取数字的时候就只要看
左上、上、右上这3个格子就可以了;(下面不可能有格子被取,我们还没搜到那里呢!);
【完整代码】
#include <cstdio> #include <cstdlib> #include <cmath> #include <set> #include <map> #include <iostream> #include <algorithm> #include <cstring> #include <queue> #include <vector> #include <stack> #include <string> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long #define rep1(i,a,b) for (int i = a;i <= b;i++) #define rep2(i,a,b) for (int i = a;i >= b;i--) #define mp make_pair #define pb push_back #define fi first #define se second typedef pair<int,int> pii; typedef pair<LL,LL> pll; void rel(LL &r) { r = 0; char t = getchar(); while (!isdigit(t) && t!='-') t = getchar(); LL sign = 1; if (t == '-')sign = -1; while (!isdigit(t)) t = getchar(); while (isdigit(t)) r = r * 10 + t - '0', t = getchar(); r = r*sign; } void rei(int &r) { r = 0; char t = getchar(); while (!isdigit(t)&&t!='-') t = getchar(); int sign = 1; if (t == '-')sign = -1; while (!isdigit(t)) t = getchar(); while (isdigit(t)) r = r * 10 + t - '0', t = getchar(); r = r*sign; } const int MAXN = 10; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1}; const int dy[9] = {0,0,0,-1,1,-1,1,-1,1}; const double pi = acos(-1.0); int n,m; LL a[MAXN][MAXN],ans; bool bo[MAXN][MAXN]; void dfs(int x,int y,LL now) { if (y>m) x=x+1,y = 1; if (x==n+1) { ans = max(ans,now); return; } bool j1,j2,j3; if (x-1<1 || y-1<1) j1 = true; else j1 = bo[x-1][y-1]; if (x-1<1) j2 = true;else j2 = bo[x-1][y]; if (x-1<1 || y+1>m) j3 = true;else j3 = bo[x-1][y+1]; if (j1&&j2&&j3) { bo[x][y] = false; dfs(x,y+2,now+a[x][y]); bo[x][y] = true; } dfs(x,y+1,now); } int main() { //freopen("F:\\rush.txt","r",stdin); int T; rei(T); while (T--) { ans = 0; memset(bo,true,sizeof(bo)); rei(n);rei(m); rep1(i,1,n) rep1(j,1,m) rel(a[i][j]); dfs(1,1,0); cout << ans << endl; } return 0; }
相关文章推荐
- 【区间DP+高精】codevs1166 矩阵取数游戏题解
- bjoi 2010 取数游戏 动态规划
- 【CodeVS 1037】取数游戏
- 洛谷 P1288 取数游戏II
- [CODEVS1037]取数游戏
- fzu 1927 取数游戏 【最小费用流】
- P1288 取数游戏II
- 洛谷Oj-P1288 取数游戏II-博弈
- BZOJ 1978: [BeiJing2010]取数游戏 game
- BZOJ 1978: [BeiJing2010]取数游戏 game( dp )
- 2017年浙江工业大学大学生程序设计迎新赛决赛—网络同步赛 G 取数游戏2【区间DP】
- 洛谷 P1288 取数游戏II
- 洛谷 P1123 取数游戏
- 【CodeVS1037】取数游戏
- 洛谷 P1288 取数游戏II
- 算法学习——贪心算法之取数游戏(显示两端数字)
- 【贪心】取数游戏
- 蓝桥杯-取数游戏
- noi 9265 取数游戏
- 一个关于“取数问题”的游戏