lifusheng @ 2024-03-29 18:25:36
我是把题干给的样例画出来然后输入3 3 3结果与样例一样(顺序不同) 我又输入了3 4 5,结果多出来2 6 2、3 5 2、4 4 3、5 3 3,少了5 4 2,求大佬帮助看看问题出在哪里 注意:这代码只能是C语言,C++好像无法运行
#include<stdio.h>
#define N 1025//题干要求1025
void f(int *a[N],int n)
{
int i,*p[N];
if(n<4)
return;
for(i=1;i<n/2;i++)
{
if(a[i][i]==0)
a[i][i]=4;
if(a[i][n+1-i]==0)
a[i][n+1-i]=3;
if(a[n+1-i][n+1-i]==0)
a[n+1-i][n+1-i]=1;
if(a[n+1-i][i]==0)
a[n+1-i][i]=2;
}
for(i=1;i<=n/2;i++)
p[i]=a[i];
f(p,n/2);
for(i=1;i<=n/2;i++)
p[i]=a[i+n/2];
f(p,n/2);
for(i=1;i<=n/2;i++)
p[i]=a[i]+n/2;
f(p,n/2);
for(i=1;i<=n/2;i++)
p[i]=a[i+n/2]+n/2;
f(p,n/2);
}
void fun(int *a[N],int x,int y,int n)
{
int *p[N],i;
if(n<4)
return;
if(x<=n/2 && y<=n/2)
{
for(i=0;i<=n/4;i++)
a[n/2+1-i][n/2+1-i]=1;
//a[n/4][n/4]=0;
for(i=1;i<=n/2;i++)
p[i]=a[i];
fun(p,x,y,n/2);
}
if(x<=n/2 && y>n/2)
{
for(i=0;i<=n/4;i++)
a[n/2-1+i][n/2+1-i]=3;
//a[3*n/4+1][n/4]=0;
for(i=1;i<=n/2;i++)
p[i]=a[i]+n/2;
fun(p,x,y-n/2,n/2);
}
if(x>n/2 && y<=n/2)
{
for(i=0;i<=n/4;i++)
a[n/2+1-i][n/2-1+i]=2;
//a[n/4][3*n/4+1]=0;
for(i=1;i<=n/2;i++)
p[i]=a[n/2+i];
fun(p,x-n/2,y,n/2);
}
if(x>n/2 && y>n/2)
{
for(i=0;i<=n/4;i++)
a[n/2-1+i][n/2-1+i]=4;
//a[3*n/4+1][3*n/4+1]=0;
for(i=1;i<=n/2;i++)
p[i]=a[n/2+i]+n/2;
fun(p,x-n/2,y-n/2,n/2);
}
}
int main()
{
int k,i,j,a[N][N],*p[N],n,x,y;
scanf("%d",&k);
for(i=0;i<N;i++)
p[i]=&a[i];
for(i=0;i<N;i++)
for(j=0;j<N;j++)
a[i][j]=0;
n=1<<k;
f(p,n);
a[n/2][n/2]=0;
a[n/2+1][n/2+1]=0;
a[n/2][n/2+1]=0;
a[n/2+1][n/2]=0;
scanf("%d%d",&x,&y);
fun(p,x,y,n);
a[x][y]=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(a[i][j])
printf("%d %d %d\n",i,j,a[i][j]);
return 0;
}
by 123huchenghao @ 2024-06-28 20:50:16
#include <bits/stdc++.h>
using namespace std;
int k, x, y;
void cover(int x1, int y1, int x2, int y2, int x, int y)
{
if(x2 - x1 == 1)
{
if(x1 == x && y1 == y) cout << x2 << " " << y2 << " " << 1 << endl;
else if(x1 == x && y2 == y) cout << x2 << " " << y1 << " " << 2 << endl;
else if(x2 == x && y1 == y) cout << x1 << " " << y2 << " " << 3 << endl;
else cout << x1 << " " << y1 << " " << 4 << endl;
return;
}
int len = x2 - x1 + 1;
int half = len / 2;
int xa = x1 + half - 1, ya = y1 + half - 1;
int xb = x1 + half - 1, yb = y1 + half;
int xc = x1 + half, yc = y1 + half - 1;
int xd = x1 + half, yd = y1 + half;
if(x >= x1 && x <= xa && y >= y1 && y <= ya)
{
cover(xa, ya, xd, yd, xa, ya);
xa = x, ya = y;
}
else if(x >= x1 && x <= xb && y >= yb && y <= y2)
{
cover(xa, ya, xd, yd, xb, yb);
xb = x, yb = y;
}
else if(x >= xc && x <= x2 && y >= y1 && y <= yc)
{
cover(xa, ya, xd, yd, xc, yc);
xc = x, yc = y;
}
else
{
cover(xa, ya, xd, yd, xd, yd);
xd = x, yd = y;
}
cover(x1, y1, x1 + half - 1, y1 + half - 1, xa, ya);
cover(x1, y1 + half, x1 + half - 1, y2, xb, yb);
cover(x1 + half, y1, x2, y1 + half - 1, xc, yc);
cover(x1 + half, y1 + half, x2, y2, xd, yd);
}
int main()
{
cin >> k >> x >> y;
cover(1, 1, 1 << k, 1 << k, x, y);
return 0;
}