測資生成器
#include <iostream>
#include <math.h>
#include <string.h>
#include <stdio.h>
using namespace std;
int N = 49;
int main(){
freopen("10.in","w",stdout);
srand( time(NULL) );
int d = rand() % 4;
printf("%d\n",N);
printf("%d\n",d);
for ( int i = 0 ; i < N ; i++ ){
for ( int j = 0 ;j < N ; j++ ){
if ( j != 0 ) putchar(' ');
printf("%d",rand()%10);
}
printf("\n");
}
return 0;
}
解答
#include <iostream>
#include <math.h>
#include <string.h>
#include <stdio.h>
using namespace std;
int arr[50][50], N;
int dx[]={-1,0,1,0}, dy[]={0,-1,0,1};
void Go( int y , int x , int dir , int now , int ma , bool second ){
if ( y >= N || x >= N || x < 0 || y < 0 ) return;
printf("%d",arr[y][x]);
x += dx[dir];
y += dy[dir];
if ( now == ma-1 ){
if ( !second )
Go( y , x , (dir+1)%4 , 0 , ma , true );
else{
Go( y , x , (dir+1)%4 , 0 , ma+1 , false );
}
}
else{
Go( y , x , dir , now+1, ma , second );
}
}
int main(){
freopen("10.in","r",stdin);
freopen("10.out","w",stdout);
int dir;
scanf("%d",&N);
scanf("%d",&dir);
for ( int i = 0 ; i < N ; i++ )
for ( int j = 0 ; j < N ; j++ )
scanf("%d",&arr[i][j]);
Go( (N) >> 1, (N) >> 1, dir, 0 , 1 , false );
return 0;
}