From 53dc1271eca45f0c3200c68c957c002ce651e779 Mon Sep 17 00:00:00 2001 From: carry Date: Fri, 7 Feb 2025 21:46:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=AF=8F=E6=97=A5=E4=B8=80?= =?UTF-8?q?=E9=A2=98=EF=BC=9A=E8=9E=BA=E6=97=8B=E7=9F=A9=E9=98=B5=20II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generateMatrix.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 generateMatrix.cpp diff --git a/generateMatrix.cpp b/generateMatrix.cpp new file mode 100644 index 0000000..ddd0e59 --- /dev/null +++ b/generateMatrix.cpp @@ -0,0 +1,81 @@ +#include +#include + +using namespace std; + +class Solution { +public: + int x = 0; + int y = 0; + int pos = 0; + int n = 0; + vector> result; + + void go(){ + switch(pos){ + case 0: + x++; + if(x==n || result[y][x] != 0){ + x--; + pos++; + } + else{ + break; + } + case 1: + y++; + if(y==n || result[y][x] != 0){ + y--; + pos++; + } + else{ + break; + } + case 2: + x--; + if(x==-1 || result[y][x] != 0){ + x++; + pos++; + } + else{ + break; + } + case 3: + y--; + if(y==-1 || result[y][x] != 0){ + y++; + pos=0; + x++; + } + else{ + break; + } + } + } + + vector> generateMatrix(int n) { + this->n = n; + int num = 1; + this->result = vector>(n,vector(n)); + this->result[0][0] = num; + while (true) + { + cout << x << "," << y << "," << pos << endl; + num++; + this->go(); + if(this->result[y][x] != 0){ + break; + + } + this->result[y][x] = num; + } + return this->result; + } +}; + +int main(){ + Solution s; + vector> output = s.generateMatrix(1); + cout << output[0][0]; + return 0; +} \ No newline at end of file