这题iostream改成cstdio后反而多t了一个点??
  • 板块P1123 取数游戏
  • 楼主GVNDN
  • 当前回复12
  • 已保存回复12
  • 发布时间2020/7/26 17:12
  • 上次更新2023/11/6 22:13:21
查看原帖
这题iostream改成cstdio后反而多t了一个点??
91975
GVNDN楼主2020/7/26 17:12

求大佬解释下,谢谢。 iostream:

#include<iostream>
#include<cstring>
using namespace std;

int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[] = {1, 0, -1, 1, -1, 1, 0, -1};
int k, n, m, pcs, ans, f[10][10], b[10][10];

void dfs(int x, int y){
	if(x == n + 1){
		if(pcs > ans)
			ans = pcs;
		return;
	}
	if(y == m + 1){
		dfs(x + 1, 1);
		return;
	}
	dfs(x, y + 1);
	if(!b[x][y]){
		for(int i = 0; i < 8; i++)
			for(int j = 0; j < 8; j++)
				b[x + dx[i]][y + dy[j]]++;
		pcs += f[x][y];
		dfs(x, y + 1);
		for(int i = 0; i < 8; i++)
			for(int j = 0; j < 8; j++)
				b[x + dx[i]][y + dy[j]]--;
		pcs -= f[x][y];
	}
}
	
int main(){
	cin >> k;
	for(int i = 0; i < k; i++){
		ans = pcs = 0;
		memset(f, 0, sizeof(f));
		memset(b, 0, sizeof(b));
		cin >> n >> m;
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++)
				cin >> f[i][j];
		dfs(1, 1);
		cout << ans << endl;
	}
	return 0;
}

cstdio:

#include<cstdio>
#include<cstring>
using namespace std;

int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[] = {1, 0, -1, 1, -1, 1, 0, -1};
int k, n, m, pcs, ans, f[10][10], b[10][10];

void dfs(int x, int y){
	if(x == n + 1){
		if(pcs > ans)
			ans = pcs;
		return;
	}
	if(y == m + 1){
		dfs(x + 1, 1);
		return;
	}
	dfs(x, y + 1);
	if(!b[x][y]){
		for(int i = 0; i < 8; i++)
			for(int j = 0; j < 8; j++)
				b[x + dx[i]][y + dy[j]]++;
		pcs += f[x][y];
		dfs(x, y + 1);
		for(int i = 0; i < 8; i++)
			for(int j = 0; j < 8; j++)
				b[x + dx[i]][y + dy[j]]--;
		pcs -= f[x][y];
	}
}
	
int main(){
	scanf("%d", &k);
	for(int i = 0; i < k; i++){
		ans = pcs = 0;
		memset(f, 0, sizeof(f));
		memset(b, 0, sizeof(b));
		scanf("%d%d", &n, &m);
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++)
				scanf("%d", &f[i][j]);
		dfs(1, 1);
		printf("%d\n", ans);
	}
	return 0;
}
2020/7/26 17:12
加载中...