感觉海星的一个模拟,注意细节就好
给几个例子都过了应该就ac了,注意数组大小(答案与答案之间应该有空格我这里没有,不能省略)
/*
1
2
output:2
2
1 5
3 7
output:1537
3
1 5 3
3 7 5
9 4 6
output:153973546
4
1 5 3 9
3 7 5 6
9 4 6 4
7 3 1 3
output:1539739547366413
5
1 5 3 9 2
3 7 5 6 3
9 4 6 4 4
7 3 1 3 5
4 3 2 1 7
output:1539739547436623413234517
6
1 5 3 9 2 9
3 7 5 6 3 8
9 4 6 4 4 7
7 3 1 3 5 6
4 3 2 1 7 5
3 4 5 6 8 1
output:153973954743662934133423487515676581
*/
acode
#include <bits/stdc++.h>
#define MAX 505
using namespace std;
int n, m[MAX][MAX];
void show(int x, int y)
{
int xx = x, yy = y, flag = 0;
cout << m[xx][yy];
while (xx >= 1 && xx <= n && yy >= 1 && yy <= n) {
yy += 1;
if (yy > n) {
cout << endl;
break;
}
cout << " " << m[xx][yy];
xx += 1; yy -= 1;
while (xx >= 1 && xx <= n && yy >= 1 && yy <= n) {
cout << " " << m[xx][yy];
xx += 1; yy -= 1;
}
if (xx >= n && yy >= n) break;
yy += 1; //回退
if (xx > n) { //从底部出去
xx -= 1;
yy += 1;
cout << " " << m[xx][yy];
}
else {
cout << " " << m[xx][yy];
}
xx -= 1; yy += 1;
while (xx >= 1 && xx <= n && yy >= 1 && yy <= n) {
cout << " " << m[xx][yy];
xx -= 1; yy += 1;
}
if (xx >= n && yy >= n) break;
xx += 1; //回退
if (yy > n) { //从右侧出去
yy -= 2; xx += 1;
}
else {
yy -= 1;
}
}
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> m[i][j];
show(1, 1);
return 0;
}