problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
acwing::acwing756类 参考

AcWing 756. 蛇形矩阵 更多...

#include <acwing.h>

静态 Public 成员函数

static int main (istream &cin, ostream &cout)
 

详细描述

AcWing 756. 蛇形矩阵

在文件 acwing.h1105 行定义.

成员函数说明

◆ main()

int acwing::acwing756::main ( istream &  cin,
ostream &  cout 
)
static

在文件 acwing.cpp3515 行定义.

3515 {
3516 int n;
3517 int m;
3518 int arr[100][100] = {};
3519 int x = 0;
3520 int y = 0;
3521 int v = 1;
3522 int dir = 1;//0 上 1 右 2 下 3 左
3523 cin >> n >> m;
3524 while(v <= n * m) {
3525 arr[x][y] = v++;
3526 move:;
3527 if(v > m * n) {
3528 break;
3529 }
3530 switch(dir) {
3531 case 3://左
3532 {
3533 const int next_y = y - 1;
3534 if(arr[x][next_y] != 0 || next_y < 0) {
3535 //不能向上
3536 dir = (dir + 1) % 4;
3537 goto move;
3538 }
3539 y--;
3540 break;
3541 }
3542 case 2://下
3543 {
3544 const int next_x = x + 1;
3545 if(arr[next_x][y] != 0 || next_x >= n) {
3546 dir = (dir + 1) % 4;
3547 goto move;
3548 }
3549 x++;
3550 break;
3551 }
3552 case 1://右
3553 {
3554 const int next_y = y + 1;
3555 if(arr[x][next_y] != 0 || next_y >= m) {
3556 dir = (dir + 1) % 4;
3557 goto move;
3558 }
3559 y++;
3560 break;
3561 }
3562 case 0://上
3563 {
3564 const int next_x = x - 1;
3565 if(arr[next_x][y] != 0 || next_x < 0) {
3566 dir = (dir + 1) % 4;
3567 goto move;
3568 }
3569 x--;
3570 break;
3571 }
3572 }
3573 }
3574 for(int i = 0; i < n; i++) {
3575 for(int j = 0; j < m; j++) {
3576 cout << arr[i][j] << " ";
3577 }
3578 cout << endl;
3579 }
3580 return 0;
3581 }

被这些函数引用 acwing::TEST().


该类的文档由以下文件生成: