aaaaaaa
2024-06-20 21:12:34
发布于:广东
#include <iostream>
using namespace std;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}, ddx[4] = {0,1,0,-1}, ddy[4] = {-1,0,1,0}, ddx2[4]={-1,0,1,0}, ddy2[4]={0,-1,0,1}, ddx3[4]={1,0,-1,0}, ddy3[4]={0,-1,0,1}, ddx4[4]={0,-1,0,1}, ddy4[4]={1,0,-1,0}, ddx6[4] = {-1,0,1,0}, ddy6[4] = {0,1,0,-1}, ddx7[5] = {0,1,0,-1,0}, ddy7[5] = {-1,0,1,0,1};
int a[20][20]={0}, vis[20][20]={0};
int main() {
int n, x, y, f, dir = 0, num = 1, c = 0;
cin >> n >> x >> y >> f;
a[x][y] = vis[x][y] = num++;
if(f == -1&& y!=0&&y!=n&&x!=n){
for(int i = 0; i < 4; i++){
dx[i] = ddx[i];
dy[i] = ddy[i];
}
}
else if(f == -1&& yn){
for(int i = 0; i < 4; i++){
dx[i] = ddx2[i];
dy[i] = ddy2[i];
}
}
else if(f == -1&& xn){
for(int i = 0; i < 4; i++){
dx[i] = ddx4[i];
dy[i] = ddy4[i];
}
}
else if(f == -1&& y1){
for(int i = 0; i < 4; i++){
dx[i] = ddx4[i];
dy[i] = ddy4[i];
}
}
else if(f == 1&& yn){
for(int i = 0; i < 4; i++){
dx[i] = ddx3[i];
dy[i] = ddy3[i];
}
}
else if(f == 1&& xn){
for(int i = 0; i < 4; i++){
dx[i] = ddx7[i];
dy[i] = ddy7[i];
}
}
if(f == 1&& y1){
for(int i = 0; i < 4; i++){
dx[i] = ddx6[i];
dy[i] = ddy6[i];
}
}
if(f == 1&& y3&&xn||y2&&xn&&f==1){
while(c <= 4){
int nx = x + ddx7[dir], ny = y + ddy7[dir];
if(vis[nx][ny] || nx < 1 || nx > n || ny < 1 || ny > n){
dir++;
c++;
}else{
c = 0;
a[nx][ny] = num++;
vis[nx][ny] = 1;
x = nx, y = ny;
}
dir %= 5;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
printf("%3d",a[i][j]);
}
cout << endl;
}
return 0;
}
while(c <= 4){
int nx = x + dx[dir], ny = y + dy[dir];
if(vis[nx][ny] || nx < 1 || nx > n || ny < 1 || ny > n){
dir++;
c++;
}else{
c = 0;
a[nx][ny] = num++;
vis[nx][ny] = 1;
x = nx, y = ny;
}
dir %= 4;
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
printf("%3d",a[i][j]);
}
cout << endl;
}
return 0;
}
全部评论 1
为了保证代码的可读性,请使用markdown的代码块语法来包裹你的代码!!
语法:
```cpp
在此粘贴你的代码
```
效果:
print("Hello World")
2024-06-21 来自 浙江
0
有帮助,赞一个