社区讨论
我有一个疑问
B4335 [中山市赛 2023] 互质参与者 2已保存回复 3
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 3 条
- 当前快照
- 1 份
- 快照标识符
- @mhj1yw2d
- 此快照首次捕获于
- 2025/11/03 19:24 4 个月前
- 此快照最后确认于
- 2025/11/03 19:24 4 个月前
为什么第1个代码过不了但第2个能过
CPP#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 15;
int n;
int a[maxn][maxn];
bool vis[maxn][maxn];
struct Node{
int x, y, sum;
}d[1500];
int cnt, ans = -1;
void dfs(int tmp, int s)
{
if(tmp > cnt)
{
ans = max(ans, s);
return ;
}
int x = d[tmp].x;
int y = d[tmp].y;
if(vis[x - 1][y - 1] || vis[x - 1][y] || vis[x - 1][y + 1] || vis[x][y - 1] || vis[x][y] || vis[x][y + 1] || vis[x + 1][y - 1] || vis[x + 1][y] || vis[x + 1][y + 1])
return ;
dfs(tmp + 1, s);
vis[x - 1][y - 1] = vis[x - 1][y] = vis[x - 1][y + 1] = vis[x][y - 1] = vis[x][y] = vis[x][y + 1] = vis[x + 1][y - 1] = vis[x + 1][y] = vis[x + 1][y + 1] = true;
dfs(tmp + 1, s + d[tmp].sum);
vis[x - 1][y - 1] = vis[x - 1][y] = vis[x - 1][y + 1] = vis[x][y - 1] = vis[x][y] = vis[x][y + 1] = vis[x + 1][y - 1] = vis[x + 1][y] = vis[x + 1][y + 1] = false;
}
signed main()
{
cin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cin >> a[i][j];
for(int i = 2; i < n; i++)
{
for(int j = 2; j < n; j++)
{
// cout << i << ' ' << j << endl;
// cout << a[i - 1][j - 1] <<' '<< a[i - 1][j] <<' ' << a[i - 1][j + 1] <<'\n';
// cout <<a[i][j - 1] <<' '<< a[i][j] <<' '<< a[i][j + 1] <<'\n';
// cout <<a[i + 1][j - 1] <<' '<< a[i + 1][j] <<' '<< a[i + 1][j + 1]<<'\n';
d[++cnt].sum = a[i - 1][j - 1] + a[i - 1][j] + a[i - 1][j + 1] +
a[i][j - 1] + a[i][j] + a[i][j + 1] +
a[i + 1][j - 1] + a[i + 1][j] + a[i + 1][j + 1];
d[cnt].x = i;
d[cnt].y = j;
}
}
dfs(1, 0);
cout << ans;
return 0;
}
CPP#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 15;
int n;
int a[maxn][maxn];
bool vis[maxn][maxn];
struct Node{
int x, y, sum;
}d[1500];
int cnt, ans = -1;
void dfs(int tmp, int s)
{
if(tmp > cnt)
{
ans = max(ans, s);
return ;
}
int x = d[tmp].x;
int y = d[tmp].y;
dfs(tmp + 1, s);
if(vis[x - 1][y - 1] || vis[x - 1][y] || vis[x - 1][y + 1] || vis[x][y - 1] || vis[x][y] || vis[x][y + 1] || vis[x + 1][y - 1] || vis[x + 1][y] || vis[x + 1][y + 1])
return ;
vis[x - 1][y - 1] = vis[x - 1][y] = vis[x - 1][y + 1] = vis[x][y - 1] = vis[x][y] = vis[x][y + 1] = vis[x + 1][y - 1] = vis[x + 1][y] = vis[x + 1][y + 1] = true;
dfs(tmp + 1, s + d[tmp].sum);
vis[x - 1][y - 1] = vis[x - 1][y] = vis[x - 1][y + 1] = vis[x][y - 1] = vis[x][y] = vis[x][y + 1] = vis[x + 1][y - 1] = vis[x + 1][y] = vis[x + 1][y + 1] = false;
}
signed main()
{
cin >> n;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cin >> a[i][j];
for(int i = 2; i < n; i++)
{
for(int j = 2; j < n; j++)
{
// cout << i << ' ' << j << endl;
// cout << a[i - 1][j - 1] <<' '<< a[i - 1][j] <<' ' << a[i - 1][j + 1] <<'\n';
// cout <<a[i][j - 1] <<' '<< a[i][j] <<' '<< a[i][j + 1] <<'\n';
// cout <<a[i + 1][j - 1] <<' '<< a[i + 1][j] <<' '<< a[i + 1][j + 1]<<'\n';
d[++cnt].sum = a[i - 1][j - 1] + a[i - 1][j] + a[i - 1][j + 1] +
a[i][j - 1] + a[i][j] + a[i][j + 1] +
a[i + 1][j - 1] + a[i + 1][j] + a[i + 1][j + 1];
d[cnt].x = i;
d[cnt].y = j;
}
}
dfs(1, 0);
cout << ans;
return 0;
}
中间只是把
dfs(tmp + 1, s);的位置交换了一下,为什么就过不了了,有没有人给解释一下回复
共 3 条回复,欢迎继续交流。
正在加载回复...