From 8c2c64b579fd3c1ba349ac7cecb73f08a237db53 Mon Sep 17 00:00:00 2001 From: me1t0ut Date: Wed, 27 Feb 2019 20:39:17 +0800 Subject: [PATCH 01/16] Initial commit --- level1/p01_runningLetter/runningLetter.cpp | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 level1/p01_runningLetter/runningLetter.cpp diff --git a/level1/p01_runningLetter/runningLetter.cpp b/level1/p01_runningLetter/runningLetter.cpp new file mode 100644 index 00000000..f4c1794f --- /dev/null +++ b/level1/p01_runningLetter/runningLetter.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +using namespace std; + +void PrintStr(string s, int x, int y) +{ + HANDLE hd; + COORD pos; + pos.X = x; + pos.Y = y; + hd = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleCursorPosition(hd, pos); + cout << s; +} + +int main() +{ + string a = "a"; + const int EDGE = 120, SPEED = 10; + int x = 1, y = 0, dir = 1, len = a.size(); + PrintStr(a, 0, 0); Sleep(50); + while (1) + { + system("cls"); + if (x == 0 || x == EDGE - len) + { + dir *= -1; + y++; + } + x += dir; + PrintStr(a, x, y); + Sleep(SPEED); + } + return 0; +} From 1b8147bc31f7b8a877382ac8adbb36b862d54591 Mon Sep 17 00:00:00 2001 From: me1t0ut Date: Wed, 27 Feb 2019 20:54:54 +0800 Subject: [PATCH 02/16] isPrime down --- level1/p02_isPrime/isPrime.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 level1/p02_isPrime/isPrime.cpp diff --git a/level1/p02_isPrime/isPrime.cpp b/level1/p02_isPrime/isPrime.cpp new file mode 100644 index 00000000..13cffe57 --- /dev/null +++ b/level1/p02_isPrime/isPrime.cpp @@ -0,0 +1,24 @@ +#include +#include + +using namespace std; +typedef long long ll; + +bool isPrime(ll x) +{ + ll sq = sqrt(x); + for (int i = 2; i <= sq; ++i) + { + if (x%i == 0)return false; + } + return true; +} + +int main() +{ + ll num; + cin >> num; + if (isPrime(num))cout << "a prime"; + else cout << "not a prime"; + return 0; +} From 8423fb6f1cd26cb75de4d7afdab973c9cb393f67 Mon Sep 17 00:00:00 2001 From: me1t0ut Date: Wed, 27 Feb 2019 21:04:00 +0800 Subject: [PATCH 03/16] Diophantus down --- level1/p03_Diophantus/Diophantus.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 level1/p03_Diophantus/Diophantus.cpp diff --git a/level1/p03_Diophantus/Diophantus.cpp b/level1/p03_Diophantus/Diophantus.cpp new file mode 100644 index 00000000..3acba96a --- /dev/null +++ b/level1/p03_Diophantus/Diophantus.cpp @@ -0,0 +1,17 @@ +#include +#include + +using namespace std; + +int main() +{ + for (int age = 0; age <= 200; ++age) + { + if (age % 12 == 0 && age % 7 == 0 && age - age / 6 - age / 12 - age / 7 - 5 - 4 == age / 2) + { + printf("age = %d\n", age - 4); + break; + } + } + return 0; +} From 024c1199a7e8ba5f3bd2880e1d6a762118df8a18 Mon Sep 17 00:00:00 2001 From: me1t0ut Date: Wed, 27 Feb 2019 21:34:49 +0800 Subject: [PATCH 04/16] narcissus down --- level1/p04_ narcissus/narcissus.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 level1/p04_ narcissus/narcissus.cpp diff --git a/level1/p04_ narcissus/narcissus.cpp b/level1/p04_ narcissus/narcissus.cpp new file mode 100644 index 00000000..9b3ca884 --- /dev/null +++ b/level1/p04_ narcissus/narcissus.cpp @@ -0,0 +1,18 @@ +#include +#include + +using namespace std; + +int main() +{ + int x, y, z; + for (int i = 100; i < 1000; ++i) + { + x = i / 100; + y = (i - i / 100 * 100) / 10; + z = i % 10; + if (pow(x, 3) + pow(y, 3) + pow(z, 3) == i) + printf("%d\n", i); + } + return 0; +} From 63446cb73f26aa6587cb03246711ec119b6cfbce Mon Sep 17 00:00:00 2001 From: me1t0ut Date: Wed, 27 Feb 2019 22:22:33 +0800 Subject: [PATCH 05/16] allPrimes & Goldbach down --- level1/p05_allPrimes/allPrimes.cpp | 36 +++++++++++++++++++++++ level1/p06_Goldbach/Goldbach.cpp | 46 ++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 level1/p05_allPrimes/allPrimes.cpp create mode 100644 level1/p06_Goldbach/Goldbach.cpp diff --git a/level1/p05_allPrimes/allPrimes.cpp b/level1/p05_allPrimes/allPrimes.cpp new file mode 100644 index 00000000..ea246cf9 --- /dev/null +++ b/level1/p05_allPrimes/allPrimes.cpp @@ -0,0 +1,36 @@ +#include +#include + +using namespace std; +const int MAXN = 1000; + +int notprime[MAXN + 5], primes[MAXN + 5], len; + +void GetPrime() +{ + for (int i = 2; i <= MAXN; i++) + { + if (!notprime[i]) + { + primes[len++] = i; + printf("%d\n", i); + } + for (int j = 0; j < len; j++) + { + if (i*primes[j] > MAXN) break; + notprime[i*primes[j]] = true; + if (i%primes[j] == 0) break; + } + } +} + +int main() +{ + clock_t StartTime, FinishTime; + StartTime = clock(); + GetPrime(); + FinishTime = clock(); + printf("%.3lf seconds", (double)(FinishTime - StartTime) / CLOCKS_PER_SEC); + //system("pause"); + return 0; +} diff --git a/level1/p06_Goldbach/Goldbach.cpp b/level1/p06_Goldbach/Goldbach.cpp new file mode 100644 index 00000000..20542d14 --- /dev/null +++ b/level1/p06_Goldbach/Goldbach.cpp @@ -0,0 +1,46 @@ +#include +#include + +using namespace std; +const int MAXN = 100; + +int isprime[MAXN + 5], primes[MAXN + 5], len; + +void GetPrime() +{ + memset(isprime, true, sizeof(isprime)); + isprime[0] = isprime[1] = false; + for (int i = 2; i <= MAXN; i++) + { + if (isprime[i])primes[len++] = i; + for (int j = 0; j < len; j++) + { + if (i*primes[j] > MAXN) break; + isprime[i*primes[j]] = false; + if (i%primes[j] == 0) break; + } + } +} + +int main() +{ + GetPrime(); + bool flag; + for (int i = 4; i <= 100; i += 2) + { + flag = false; + for (int j = 0; j < len; ++j) + { + if (isprime[i - primes[j]]) + flag = true; + } + if (flag == false) + { + printf("false\n"); + return 0; + } + } + printf("true\n"); + //system("pause"); + return 0; +} From 0b4874c70fb43594a8ddbd661aed56146ddd44b3 Mon Sep 17 00:00:00 2001 From: me1t0ut Date: Thu, 28 Feb 2019 13:59:40 +0800 Subject: [PATCH 06/16] cpp->c --- .../{runningLetter.cpp => runningLetter.c} | 17 ++--- level1/p02_isPrime/isPrime.c | 24 +++++++ level1/p02_isPrime/isPrime.cpp | 24 ------- .../{Diophantus.cpp => Diophantus.c} | 8 +-- .../{narcissus.cpp => narcissus.c} | 6 +- .../{allPrimes.cpp => allPrimes.c} | 8 +-- .../p06_Goldbach/{Goldbach.cpp => Goldbach.c} | 20 +++--- level1/p07_encrypt_decrypt/encrypt_decrypt.c | 66 +++++++++++++++++++ 8 files changed, 117 insertions(+), 56 deletions(-) rename level1/p01_runningLetter/{runningLetter.cpp => runningLetter.c} (62%) create mode 100644 level1/p02_isPrime/isPrime.c delete mode 100644 level1/p02_isPrime/isPrime.cpp rename level1/p03_Diophantus/{Diophantus.cpp => Diophantus.c} (67%) rename level1/p04_ narcissus/{narcissus.cpp => narcissus.c} (78%) rename level1/p05_allPrimes/{allPrimes.cpp => allPrimes.c} (84%) rename level1/p06_Goldbach/{Goldbach.cpp => Goldbach.c} (67%) create mode 100644 level1/p07_encrypt_decrypt/encrypt_decrypt.c diff --git a/level1/p01_runningLetter/runningLetter.cpp b/level1/p01_runningLetter/runningLetter.c similarity index 62% rename from level1/p01_runningLetter/runningLetter.cpp rename to level1/p01_runningLetter/runningLetter.c index f4c1794f..859c7c93 100644 --- a/level1/p01_runningLetter/runningLetter.cpp +++ b/level1/p01_runningLetter/runningLetter.c @@ -1,10 +1,12 @@ -#include +#include #include -#include +#include -using namespace std; +#define MAXN 100 +#define EDGE 120 +#define SPEED 10 -void PrintStr(string s, int x, int y) +void PrintStr(char s[], int x, int y) { HANDLE hd; COORD pos; @@ -12,14 +14,13 @@ void PrintStr(string s, int x, int y) pos.Y = y; hd = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hd, pos); - cout << s; + printf("%s",s); } int main() { - string a = "a"; - const int EDGE = 120, SPEED = 10; - int x = 1, y = 0, dir = 1, len = a.size(); + char a[MAXN]="a"; + int x = 1, y = 0, dir = 1, len = strlen(a); PrintStr(a, 0, 0); Sleep(50); while (1) { diff --git a/level1/p02_isPrime/isPrime.c b/level1/p02_isPrime/isPrime.c new file mode 100644 index 00000000..688e2428 --- /dev/null +++ b/level1/p02_isPrime/isPrime.c @@ -0,0 +1,24 @@ +#include +#include + +typedef long long ll; + +int isPrime(ll x) +{ + if(x==1)return 0; + ll sq = sqrt(x); + for (int i = 2; i <= sq; ++i) + { + if (x%i == 0)return 0; + } + return 1; +} + +int main() +{ + ll num; + scanf("%I64d",&num); + if (isPrime(num))printf("a prime\n"); + else printf("not a prime\n"); + return 0; +} diff --git a/level1/p02_isPrime/isPrime.cpp b/level1/p02_isPrime/isPrime.cpp deleted file mode 100644 index 13cffe57..00000000 --- a/level1/p02_isPrime/isPrime.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include - -using namespace std; -typedef long long ll; - -bool isPrime(ll x) -{ - ll sq = sqrt(x); - for (int i = 2; i <= sq; ++i) - { - if (x%i == 0)return false; - } - return true; -} - -int main() -{ - ll num; - cin >> num; - if (isPrime(num))cout << "a prime"; - else cout << "not a prime"; - return 0; -} diff --git a/level1/p03_Diophantus/Diophantus.cpp b/level1/p03_Diophantus/Diophantus.c similarity index 67% rename from level1/p03_Diophantus/Diophantus.cpp rename to level1/p03_Diophantus/Diophantus.c index 3acba96a..0dcc733d 100644 --- a/level1/p03_Diophantus/Diophantus.cpp +++ b/level1/p03_Diophantus/Diophantus.c @@ -1,13 +1,11 @@ -#include -#include - -using namespace std; +#include +#include int main() { for (int age = 0; age <= 200; ++age) { - if (age % 12 == 0 && age % 7 == 0 && age - age / 6 - age / 12 - age / 7 - 5 - 4 == age / 2) + if (age % 12 == 0 && age % 7 == 0 && age - age / 6 - age / 12 - age / 7 - 5 - 4 == age / 2) { printf("age = %d\n", age - 4); break; diff --git a/level1/p04_ narcissus/narcissus.cpp b/level1/p04_ narcissus/narcissus.c similarity index 78% rename from level1/p04_ narcissus/narcissus.cpp rename to level1/p04_ narcissus/narcissus.c index 9b3ca884..fdcb658e 100644 --- a/level1/p04_ narcissus/narcissus.cpp +++ b/level1/p04_ narcissus/narcissus.c @@ -1,7 +1,5 @@ -#include -#include - -using namespace std; +#include +#include int main() { diff --git a/level1/p05_allPrimes/allPrimes.cpp b/level1/p05_allPrimes/allPrimes.c similarity index 84% rename from level1/p05_allPrimes/allPrimes.cpp rename to level1/p05_allPrimes/allPrimes.c index ea246cf9..b02d68f7 100644 --- a/level1/p05_allPrimes/allPrimes.cpp +++ b/level1/p05_allPrimes/allPrimes.c @@ -1,8 +1,6 @@ -#include +#include #include - -using namespace std; -const int MAXN = 1000; +#define MAXN 1000 int notprime[MAXN + 5], primes[MAXN + 5], len; @@ -18,7 +16,7 @@ void GetPrime() for (int j = 0; j < len; j++) { if (i*primes[j] > MAXN) break; - notprime[i*primes[j]] = true; + notprime[i*primes[j]] = 1; if (i%primes[j] == 0) break; } } diff --git a/level1/p06_Goldbach/Goldbach.cpp b/level1/p06_Goldbach/Goldbach.c similarity index 67% rename from level1/p06_Goldbach/Goldbach.cpp rename to level1/p06_Goldbach/Goldbach.c index 20542d14..cae12d3d 100644 --- a/level1/p06_Goldbach/Goldbach.cpp +++ b/level1/p06_Goldbach/Goldbach.c @@ -1,22 +1,22 @@ -#include +#include #include +#include -using namespace std; -const int MAXN = 100; +#define MAXN 100 int isprime[MAXN + 5], primes[MAXN + 5], len; void GetPrime() { - memset(isprime, true, sizeof(isprime)); - isprime[0] = isprime[1] = false; + memset(isprime, 1, sizeof(isprime)); + isprime[0] = isprime[1] = 0; for (int i = 2; i <= MAXN; i++) { if (isprime[i])primes[len++] = i; for (int j = 0; j < len; j++) { if (i*primes[j] > MAXN) break; - isprime[i*primes[j]] = false; + isprime[i*primes[j]] = 0; if (i%primes[j] == 0) break; } } @@ -25,16 +25,16 @@ void GetPrime() int main() { GetPrime(); - bool flag; + int flag; for (int i = 4; i <= 100; i += 2) { - flag = false; + flag = 0; for (int j = 0; j < len; ++j) { if (isprime[i - primes[j]]) - flag = true; + flag = 1; } - if (flag == false) + if (!flag) { printf("false\n"); return 0; diff --git a/level1/p07_encrypt_decrypt/encrypt_decrypt.c b/level1/p07_encrypt_decrypt/encrypt_decrypt.c new file mode 100644 index 00000000..38a4c3e8 --- /dev/null +++ b/level1/p07_encrypt_decrypt/encrypt_decrypt.c @@ -0,0 +1,66 @@ +#include +#include +#include +#include + +#define MAXN 1000005 + +int XorArray[MAXN],n; +char Message[MAXN],Cipher[MAXN*2],DecMess[MAXN]; + +char ToHex(int _oct) +{ + if(_oct<10)return _oct+'0'; + else return _oct-10+'a'; +} + +int ToOct(int _hex) +{ + if(_hex>='0' && _hex<='9')return _hex-'0'; + else return _hex-'a'+10; +} + +int GetRand(int LowerBound,int UpperBound) +{ + return rand()%(UpperBound-LowerBound+1)+LowerBound; +} + +void GenerateXorArray() +{ + n=GetRand(10,100); + for(int i=0;i Date: Fri, 1 Mar 2019 22:55:46 +0800 Subject: [PATCH 07/16] hanoi & maze done,enc/dec modified --- level1/p07_encrypt_decrypt/encrypt_decrypt.c | 66 +++++++------- level1/p08_hanoi/hanoi.c | 22 +++++ level1/p09_maze/maze.c | 96 ++++++++++++++++++++ 3 files changed, 151 insertions(+), 33 deletions(-) create mode 100644 level1/p08_hanoi/hanoi.c create mode 100644 level1/p09_maze/maze.c diff --git a/level1/p07_encrypt_decrypt/encrypt_decrypt.c b/level1/p07_encrypt_decrypt/encrypt_decrypt.c index 38a4c3e8..3f839bc2 100644 --- a/level1/p07_encrypt_decrypt/encrypt_decrypt.c +++ b/level1/p07_encrypt_decrypt/encrypt_decrypt.c @@ -5,62 +5,62 @@ #define MAXN 1000005 -int XorArray[MAXN],n; -char Message[MAXN],Cipher[MAXN*2],DecMess[MAXN]; +int XorArray[MAXN], n; +char Message[MAXN], Cipher[MAXN * 2], DecMess[MAXN]; char ToHex(int _oct) { - if(_oct<10)return _oct+'0'; - else return _oct-10+'a'; + if (_oct < 10)return _oct + '0'; + else return _oct - 10 + 'a'; } int ToOct(int _hex) { - if(_hex>='0' && _hex<='9')return _hex-'0'; - else return _hex-'a'+10; + if (_hex >= '0' && _hex <= '9')return _hex - '0'; + else return _hex - 'a' + 10; } -int GetRand(int LowerBound,int UpperBound) +int GetRand(int LowerBound, int UpperBound) { - return rand()%(UpperBound-LowerBound+1)+LowerBound; + return rand() % (UpperBound - LowerBound + 1) + LowerBound; } void GenerateXorArray() { - n=GetRand(10,100); - for(int i=0;i + +void ChangePosition(int _res, char _from, char _trans, char _to) +{ + if (_res == 1) + { + printf("No.%d moved from %c to %c\n", _res, _from, _to); + return; + } + ChangePosition(_res - 1, _from, _to, _trans); + printf("No.%d moved from %c to %c\n", _res, _from, _to); + ChangePosition(_res - 1, _trans, _from, _to); +} + +int main() +{ + int n; + scanf("%d", &n); + ChangePosition(n, 'A', 'B', 'C'); + printf("Done\n"); + return 0; +} diff --git a/level1/p09_maze/maze.c b/level1/p09_maze/maze.c new file mode 100644 index 00000000..c7fa35c2 --- /dev/null +++ b/level1/p09_maze/maze.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include + +#define MINN 10 +#define MAXN 30 + +char Maze[MAXN][MAXN]; +int ToX[4] = { 0,0,1,-1 }; +int ToY[4] = { 1,-1,0,0 }; +int h, w, Vis[MAXN][MAXN]; +int DesX, DesY, NowX, NowY; + +int GetRand(int LowerBound, int UpperBound) +{ + return rand() % (UpperBound - LowerBound + 1) + LowerBound; +} + +int Check(int x, int y) +{ + return Vis[x][y] == 0 && x*y != 0 && x <= h && y <= w && Maze[x][y] != (char)176; +} + +int Dfs(int x, int y) +{ + if (!Check(x, y))return 0; + if (x == DesX && y == DesY)return 1; + Vis[x][y] = 1; + for (int i = 0; i < 4; ++i) + { + if (Dfs(x + ToX[i], y + ToY[i])) + return 1; + } + return 0; +} + +void GenerateNewMaze() +{ + memset(Vis, 0, sizeof(Vis)); + h = GetRand(MINN, MAXN); + w = GetRand(MINN, MAXN); + if (h > w) { int tmp = h; h = w; w = tmp; } + for (int i = 1; i <= h; ++i) + { + for (int j = 1; j <= w; ++j) + { + if (GetRand(0, 2) == 0)Maze[i][j] = (char)176; + else Maze[i][j] = ' '; + } + } + DesX = h; DesY = w; + NowX = 1; NowY = 1; + Maze[DesX][DesY] = 'T'; + if (!Dfs(NowX, NowY)) + GenerateNewMaze(); +} + +void PrintMaze() +{ + for (int i = 1; i <= h; ++i) + { + for (int j = 1; j <= w; ++j) + { + if (i == NowX && j == NowY)printf("O"); + else printf("%c", Maze[i][j]); + } + printf("\n"); + } +} + +int main() +{ + char ch; + srand(time(NULL)); + GenerateNewMaze(); + PrintMaze(); + memset(Vis, 0, sizeof(Vis)); + while (1) + { + if (_kbhit()) + { + ch = _getch(); + if (ch == 72 && Check(NowX - 1, NowY))NowX--; + if (ch == 80 && Check(NowX + 1, NowY))NowX++; + if (ch == 75 && Check(NowX, NowY - 1))NowY--; + if (ch == 77 && Check(NowX, NowY + 1))NowY++; + system("cls"); PrintMaze(); + if (NowX == DesX && NowY == DesY)break; + } + } + printf("Done\n"); + return 0; +} From 047a13d904625e6a8ba94d555efffc42d812afc2 Mon Sep 17 00:00:00 2001 From: me1t0ut Date: Sat, 2 Mar 2019 21:38:12 +0800 Subject: [PATCH 08/16] pushBoxes done --- level1/p10_pushBoxes/maps/1.txt | 23 +++++ level1/p10_pushBoxes/maps/10.txt | 23 +++++ level1/p10_pushBoxes/maps/2.txt | 18 ++++ level1/p10_pushBoxes/maps/3.txt | 23 +++++ level1/p10_pushBoxes/maps/4.txt | 15 +++ level1/p10_pushBoxes/maps/5.txt | 15 +++ level1/p10_pushBoxes/maps/6.txt | 24 +++++ level1/p10_pushBoxes/maps/7.txt | 15 +++ level1/p10_pushBoxes/maps/8.txt | 17 +++ level1/p10_pushBoxes/maps/9.txt | 30 ++++++ level1/p10_pushBoxes/pushBoxes.c | 171 +++++++++++++++++++++++++++++++ 11 files changed, 374 insertions(+) create mode 100644 level1/p10_pushBoxes/maps/1.txt create mode 100644 level1/p10_pushBoxes/maps/10.txt create mode 100644 level1/p10_pushBoxes/maps/2.txt create mode 100644 level1/p10_pushBoxes/maps/3.txt create mode 100644 level1/p10_pushBoxes/maps/4.txt create mode 100644 level1/p10_pushBoxes/maps/5.txt create mode 100644 level1/p10_pushBoxes/maps/6.txt create mode 100644 level1/p10_pushBoxes/maps/7.txt create mode 100644 level1/p10_pushBoxes/maps/8.txt create mode 100644 level1/p10_pushBoxes/maps/9.txt create mode 100644 level1/p10_pushBoxes/pushBoxes.c diff --git a/level1/p10_pushBoxes/maps/1.txt b/level1/p10_pushBoxes/maps/1.txt new file mode 100644 index 00000000..f67461ef --- /dev/null +++ b/level1/p10_pushBoxes/maps/1.txt @@ -0,0 +1,23 @@ +19 25 +########################### +# # # # #### # +## # # # # # # # +# ## ## # # ## ## ## # +# # # # +## # ## # # # # # +## # ## # # ## # +# # ## # ### # ## # +# ### # # # ### +# ## # ## # ## # +# # # # # ## +# # ## ### # ## # # # +# # ## ##O# # # # # +# # # ### # # ## # # +# # ### # ##o## # ### +# # #### # # ## +# # # +#### ## # # # # ## +# # # # # ## ## # +# # # # ## ## ## +########################### +0 diff --git a/level1/p10_pushBoxes/maps/10.txt b/level1/p10_pushBoxes/maps/10.txt new file mode 100644 index 00000000..cfe2b3d0 --- /dev/null +++ b/level1/p10_pushBoxes/maps/10.txt @@ -0,0 +1,23 @@ +19 26 +############################ +# ## ## # ## # # +### # # #O ## # # +# ### # #### # # # # +## ## ## # ## ## +# # ## ### ## +## # # # # ## +# ### # # # # # # # +## # # # # # # +# ### # o ### # # # +# # # # ## # # ## # o # +# ## # # ## # ## +# # # ### # ### ## +## # # ## # # ## +## # # #### # +# ### ######## ### # # +# ##O## ##### # # +# ## # # # # ### # # +## # # # ## # ## # +## # # ## ##### # +############################ +0 \ No newline at end of file diff --git a/level1/p10_pushBoxes/maps/2.txt b/level1/p10_pushBoxes/maps/2.txt new file mode 100644 index 00000000..c9237946 --- /dev/null +++ b/level1/p10_pushBoxes/maps/2.txt @@ -0,0 +1,18 @@ +14 21 +########################### +# ## # # # # # +## # # ## # # ## # +## # ## # #### # +# # # # ## # +# ### # ### # # # +# # ## # +# ### ## # ## ## # # +# # # # # o # ### +# # # # # # #### # +##O## ## # ## # ## +##### # ### ## # # +# # ##### ### # +# # # # # # # # # +## # # # # # ### +######################## ## +0 \ No newline at end of file diff --git a/level1/p10_pushBoxes/maps/3.txt b/level1/p10_pushBoxes/maps/3.txt new file mode 100644 index 00000000..a572ceea --- /dev/null +++ b/level1/p10_pushBoxes/maps/3.txt @@ -0,0 +1,23 @@ +19 20 +###################### +# # # ## # # +# # # ## ## # +# # # # # # ## +# # # # ## ### ## +# # # # # # +# # # # # # ## +# ## # ## # +## ### ### ### ## +# # # ### # ### +# # ## # # # # +### #o# # # O# ## # +## ## +# ##### ## ## # # +# ## # # # #### # +## # ## ### +# # # # ## +## # # ### +# ## # ## # # +## ## # ## ### +###################### +0 \ No newline at end of file diff --git a/level1/p10_pushBoxes/maps/4.txt b/level1/p10_pushBoxes/maps/4.txt new file mode 100644 index 00000000..6f3055c2 --- /dev/null +++ b/level1/p10_pushBoxes/maps/4.txt @@ -0,0 +1,15 @@ +11 30 +################################ +# # # # ## ## # # # ###O# +#### O ## # # # +# ### ## # # ## # # +# # # ## ## #### ## # +# ## # # # # # ### # +# # # ## o # # # +## ## ## ### # ## ## # # +## ## # ## ## ### ### # # +# # # # # # ## # +# # ## o # ### # # +# # # # ## # # # # +################################ +0 diff --git a/level1/p10_pushBoxes/maps/5.txt b/level1/p10_pushBoxes/maps/5.txt new file mode 100644 index 00000000..549e861c --- /dev/null +++ b/level1/p10_pushBoxes/maps/5.txt @@ -0,0 +1,15 @@ +11 21 +####################### +# ## ## O# # +# # ## # # # # # +# ## # # # # +# # # ## ## +# # # # ### # # # +### ## o # # +# # # # # # +# # ### ## ## +## ## # # ## # +## # ## # +# # # # ## +####################### +0 diff --git a/level1/p10_pushBoxes/maps/6.txt b/level1/p10_pushBoxes/maps/6.txt new file mode 100644 index 00000000..fb6dfc17 --- /dev/null +++ b/level1/p10_pushBoxes/maps/6.txt @@ -0,0 +1,24 @@ +20 20 +###################### +# # # # # # +# # #### ## # +# o# # # # # # +# # # # ## +# ## # O # +## # # ## +# # ## ## # ## +# # #### # # +# #O# # # +# # ## # ## # # +## # ### # ## ## +# ## # # # ## +# ## ## # # +# # ## ## +# # # # ## ## # # +# ## ## # +# ## ### ### # +# # ## # # +## o ## # # ## +# ##### # ## # # +###################### +0 diff --git a/level1/p10_pushBoxes/maps/7.txt b/level1/p10_pushBoxes/maps/7.txt new file mode 100644 index 00000000..4e60bd60 --- /dev/null +++ b/level1/p10_pushBoxes/maps/7.txt @@ -0,0 +1,15 @@ +11 24 +########################## +# # # # # # # +### ## ## # # +# #o # # # # # +# # # # # # ## # +## # #O ## ## ### +## ### ## ## # +#### # # #### +# # ### ## # # +# o # # # # # # +## ### # O # +## # # # # +########################## +0 diff --git a/level1/p10_pushBoxes/maps/8.txt b/level1/p10_pushBoxes/maps/8.txt new file mode 100644 index 00000000..562bda9b --- /dev/null +++ b/level1/p10_pushBoxes/maps/8.txt @@ -0,0 +1,17 @@ +13 30 +################################ +# # ## # # ## ## # +# # ### # # # ## # +## ## ## # # +# # # #O # # # +# # ## # # # # ## # +## # # # ### ## ## # +## # ### #### # # ### # ## +# # ## ## # # +# # ## # # # ## +# # # # # ## ## +# # # o # # # ## # +## # ## # # ### # # +# ######## # # ## # ### # # +################################ +0 diff --git a/level1/p10_pushBoxes/maps/9.txt b/level1/p10_pushBoxes/maps/9.txt new file mode 100644 index 00000000..e487536c --- /dev/null +++ b/level1/p10_pushBoxes/maps/9.txt @@ -0,0 +1,30 @@ +26 26 +############################ +# # # ## ## # ### +# ## # # #### # # # +# # # # # ## o # # +## # ## ## # ## ### +# # # ## # # # # # +# # #### # # # # # +# # ## ## # # # +# # # # ## # +# # ## # ### ##### # # +# ### # ## # # ## # +## # ### # ## # +## # # ### ### # +## # ## # # # # # ## ## +## ## # # # # # +## ### ### ### ### # ## +## # ### #O # ## +# ## ## # # # # ## +# # ## # ## ## # +# # # # #### ### # +# # # ## # ### # +# # ## ### # ## # ## +# ## ##### # # # # +### # ## # # ## +# ## # # # # +# ## # ##### ###### +# # # # # ##### +############################ +0 diff --git a/level1/p10_pushBoxes/pushBoxes.c b/level1/p10_pushBoxes/pushBoxes.c new file mode 100644 index 00000000..e91634f2 --- /dev/null +++ b/level1/p10_pushBoxes/pushBoxes.c @@ -0,0 +1,171 @@ +#include +#include +#include +#include +#include + +#define MAXN 50 +#define CASES 10 + +char Maze[MAXN][MAXN]; +int h, w, NowX, NowY, Score, Record; +int AimNum, AimX[5], AimY[5]; + +int CheckStatus(int x, int y) +{ + if (Maze[x][y] == '#')return 0; + else if (Maze[x][y] == 'o')return 1; + return 2; +} + +void PrintMaze(int _case) +{ + int flag; + printf("Case#%d begins,your game record is %d\n", _case, Record); + for (int i = 0; i <= h + 1; ++i) + { + for (int j = 0; j <= w + 1; ++j) + { + if (i == NowX && j == NowY)printf("S"); + else if (Maze[i][j] != 'o') + { + flag = 0; + for (int k = 1; k <= AimNum; ++k) + { + if (i == AimX[k] && j == AimY[k]) + { + printf("O"); + flag = 1; + } + } + if (!flag)printf("%c", Maze[i][j]); + } + else printf("%c", Maze[i][j]); + } + printf("\n"); + } +} + +int GameFin() +{ + for (int i = 1; i <= AimNum; ++i) + { + if (Maze[AimX[i]][AimY[i]] != 'o') + return 0; + } + return 1; +} + +void ReadMapFromFile(int _case) +{ + FILE *fp; + unsigned char ch; + char FileName[20]; + sprintf(FileName, ".\\maps\\%d.txt", _case); + fp = fopen(FileName, "r"); + fscanf(fp, "%d %d%c", &h, &w, &ch); + for (int i = 0; i <= h + 1; ++i) + { + for (int j = 0; j <= w + 1; ++j) + { + fscanf(fp, "%c", &Maze[i][j]); + if (Maze[i][j] == 'O') + { + ++AimNum; + AimX[AimNum] = i; + AimY[AimNum] = j; + } + } + fscanf(fp, "%c", &ch); + } + fscanf(fp, "%d", &Record); + fclose(fp); +} + +void GetCopy(int _case) +{ + FILE *fp; + char FileName[20]; + sprintf(FileName, ".\\maps\\%d.txt", _case); + remove(FileName); + fp = fopen(FileName, "w"); + fprintf(fp, "%d %d\n", h, w); + for (int i = 0; i <= h + 1; ++i) + fprintf(fp, "%s\n", Maze[i]); + fclose(fp); +} + +void AddScore(int _case, int _score) +{ + FILE *fp; + char FileName[20]; + sprintf(FileName, ".\\maps\\%d.txt", _case); + fp = fopen(FileName, "a+"); + fprintf(fp, "%d", _score); + fclose(fp); +} + +int main() +{ + unsigned char ch; + int tx, ty, flag, Score; + for (int i = 1; i <= CASES; ++i) + { + NowX = NowY = 1; + Score = 300; AimNum = 0; + ReadMapFromFile(i); + GetCopy(i); + PrintMaze(i); + while (1) + { + if (_kbhit()) + { + ch = _getch(); + if (ch == 224)ch = _getch(); + if (ch == 80) { tx = 1; ty = 0; } + if (ch == 77) { tx = 0; ty = 1; } + if (ch == 72) { tx = -1; ty = 0; } + if (ch == 75) { tx = 0; ty = -1; } + if (ch == 27) + { + AddScore(i, Record); + return 0; + } + flag = CheckStatus(NowX + tx, NowY + ty); + if (flag == 1) + { + flag = CheckStatus(NowX + tx * 2, NowY + ty * 2); + if (flag == 2) + { + Maze[NowX + tx][NowY + ty] = ' '; + Maze[NowX + tx * 2][NowY + ty * 2] = 'o'; + NowX += tx; NowY += ty; + Score--; + } + } + else if (flag == 2) + { + NowX += tx; + NowY += ty; + Score--; + } + else continue; + system("cls"); + PrintMaze(i); + if (GameFin())break; + } + } + printf("Case#%d Done,your score is %d.\n", i, Score); + AddScore(i, max(Record, Score)); + while (1) + { + if (_kbhit()) + { + system("cls"); + ch = _getch(); + break; + } + } + } + return 0; +} From 0b579024d8bff849a766f73f0f9ffb22aea638b2 Mon Sep 17 00:00:00 2001 From: me1t0ut Date: Sat, 2 Mar 2019 21:43:42 +0800 Subject: [PATCH 09/16] maze simplified --- level1/p09_maze/maze.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/level1/p09_maze/maze.c b/level1/p09_maze/maze.c index c7fa35c2..51c170f7 100644 --- a/level1/p09_maze/maze.c +++ b/level1/p09_maze/maze.c @@ -21,7 +21,7 @@ int GetRand(int LowerBound, int UpperBound) int Check(int x, int y) { - return Vis[x][y] == 0 && x*y != 0 && x <= h && y <= w && Maze[x][y] != (char)176; + return Vis[x][y] == 0 && Maze[x][y] != '#'; } int Dfs(int x, int y) @@ -47,10 +47,12 @@ void GenerateNewMaze() { for (int j = 1; j <= w; ++j) { - if (GetRand(0, 2) == 0)Maze[i][j] = (char)176; + if (GetRand(0, 2) == 0)Maze[i][j] = '#'; else Maze[i][j] = ' '; } } + for (int i = 0; i <= h + 1; ++i)Maze[i][0] = Maze[i][w + 1] = '#'; + for (int i = 0; i <= w + 1; ++i)Maze[0][i] = Maze[h + 1][i] = '#'; DesX = h; DesY = w; NowX = 1; NowY = 1; Maze[DesX][DesY] = 'T'; @@ -60,11 +62,11 @@ void GenerateNewMaze() void PrintMaze() { - for (int i = 1; i <= h; ++i) + for (int i = 0; i <= h + 1; ++i) { - for (int j = 1; j <= w; ++j) + for (int j = 0; j <= w + 1; ++j) { - if (i == NowX && j == NowY)printf("O"); + if (i == NowX && j == NowY)printf("S"); else printf("%c", Maze[i][j]); } printf("\n"); From 7719ca9a365bd16ea2f12a3024ede8095ef4048f Mon Sep 17 00:00:00 2001 From: me1t0ut Date: Sat, 2 Mar 2019 21:48:32 +0800 Subject: [PATCH 10/16] fixed the problem of arrow keys --- level1/p09_maze/maze.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/level1/p09_maze/maze.c b/level1/p09_maze/maze.c index 51c170f7..eed145d0 100644 --- a/level1/p09_maze/maze.c +++ b/level1/p09_maze/maze.c @@ -51,8 +51,8 @@ void GenerateNewMaze() else Maze[i][j] = ' '; } } - for (int i = 0; i <= h + 1; ++i)Maze[i][0] = Maze[i][w + 1] = '#'; - for (int i = 0; i <= w + 1; ++i)Maze[0][i] = Maze[h + 1][i] = '#'; + for(int i=0;i<=h+1;++i)Maze[i][0]=Maze[i][w+1]='#'; + for(int i=0;i<=w+1;++i)Maze[0][i]=Maze[h+1][i]='#'; DesX = h; DesY = w; NowX = 1; NowY = 1; Maze[DesX][DesY] = 'T'; @@ -62,9 +62,9 @@ void GenerateNewMaze() void PrintMaze() { - for (int i = 0; i <= h + 1; ++i) + for (int i = 0; i <= h+1; ++i) { - for (int j = 0; j <= w + 1; ++j) + for (int j = 0; j <= w+1; ++j) { if (i == NowX && j == NowY)printf("S"); else printf("%c", Maze[i][j]); @@ -75,7 +75,7 @@ void PrintMaze() int main() { - char ch; + unsigned char ch; srand(time(NULL)); GenerateNewMaze(); PrintMaze(); @@ -85,6 +85,7 @@ int main() if (_kbhit()) { ch = _getch(); + if(ch==224)ch=_getch(); if (ch == 72 && Check(NowX - 1, NowY))NowX--; if (ch == 80 && Check(NowX + 1, NowY))NowX++; if (ch == 75 && Check(NowX, NowY - 1))NowY--; From f1da50175e9278e60ac2a074ce505e8666713cdf Mon Sep 17 00:00:00 2001 From: me1t0ut Date: Sat, 2 Mar 2019 23:48:01 +0800 Subject: [PATCH 11/16] rsa and xor edition added --- .../encrypt_decrypt(rsa&xor).c | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 level1/p07_encrypt_decrypt/encrypt_decrypt(rsa&xor).c diff --git a/level1/p07_encrypt_decrypt/encrypt_decrypt(rsa&xor).c b/level1/p07_encrypt_decrypt/encrypt_decrypt(rsa&xor).c new file mode 100644 index 00000000..434fc288 --- /dev/null +++ b/level1/p07_encrypt_decrypt/encrypt_decrypt(rsa&xor).c @@ -0,0 +1,130 @@ +#include +#include +#include +#include +#include + +#define MINN 1000 +#define MAXN 10000 + +typedef long long ll; + +ll n, p, q, e, d, x, y, phi, iv, cipher[MAXN]; +ll len, MesLen, primes[MAXN + 5], isprime[MAXN + 5]; +char Message[MAXN], DecMes[MAXN]; + +int GetRand(int LowerBound, int UpperBound) +{ + return rand() % (UpperBound - LowerBound + 1) + LowerBound; +} + +void GetPrime() +{ + memset(isprime, 1, sizeof(isprime)); + isprime[0] = isprime[1] = 0; + for (int i = 2; i <= MAXN; i++) + { + if (isprime[i])primes[len++] = i; + for (int j = 0; j < len; j++) + { + if (i*primes[j] > MAXN) break; + isprime[i*primes[j]] = 0; + if (i%primes[j] == 0) break; + } + } +} + +ll gcd(ll a, ll b) +{ + if (b == 0)return a; + return gcd(b, a%b); +} + +ll pow_mod(ll a, ll b, ll mod) +{ + ll ans = 1; + while (b) + { + if (b & 1)ans = ans * a%mod; + a = a * a%mod; b >>= 1; + } + return ans; +} + +ll exgcd(ll a, ll b) +{ + ll d = a, tmp; + if (b != 0) + { + tmp = x; x = y; y = tmp; + d = exgcd(b, a % b); + tmp = x; x = y; y = tmp; + y -= (a / b) * x; + } + else + { + x = 1; y = 0; + } + return d; +} + +ll mod_inverse(ll a, ll mod) +{ + exgcd(a, mod); + return (mod + x % mod) % mod; +} + +void init() +{ + srand(time(NULL)); + iv = GetRand(0, 255); + GetPrime(); + p = primes[GetRand(1000, len - 1)]; + q = primes[GetRand(1000, len - 1)]; + n = p * q; + phi = (p - 1)*(q - 1); + e = GetRand(10, phi - 1); + while (gcd(e, phi) != 1) + e = GetRand(10, phi - 1); + d = mod_inverse(e, phi); +} + +ll RSA_enc(ll x) +{ + x = pow_mod(x, e, n); + return x; +} + +ll RSA_dec(ll x) +{ + return (char)pow_mod(x, d, n); +} + +void get_enc() +{ + cipher[0] = iv; + for (int i = 1; i <= MesLen; ++i) + cipher[i] = RSA_enc(cipher[i - 1] ^ (ll)Message[i - 1]); +} + +void get_dec() +{ + cipher[0] = iv; + for (int i = 0; i < MesLen; ++i) + DecMes[i] = (char)RSA_dec(cipher[i + 1]) ^ cipher[i]; +} + +int main() +{ + init(); + scanf("%s", Message); + MesLen = strlen(Message); + get_enc(); + printf("cipher is : "); + for (int i = 1; i <= MesLen; ++i) + printf("%010I64d", cipher[i]); + printf("\n"); + get_dec(); + printf("Decrypted Message is : %s", DecMes); + return 0; +} From 2516c0907dac292947fda9b99638643bfcbfc8f1 Mon Sep 17 00:00:00 2001 From: me1t0ut Date: Tue, 5 Mar 2019 19:20:34 +0800 Subject: [PATCH 12/16] LinkedList & warehouse done --- level1/p11_linkedList/LinkedList.c | 105 ++++++++++++++++++ level1/p12_warehouse/warehouse.c | 164 +++++++++++++++++++++++++++++ 2 files changed, 269 insertions(+) create mode 100644 level1/p11_linkedList/LinkedList.c create mode 100644 level1/p12_warehouse/warehouse.c diff --git a/level1/p11_linkedList/LinkedList.c b/level1/p11_linkedList/LinkedList.c new file mode 100644 index 00000000..a45a1906 --- /dev/null +++ b/level1/p11_linkedList/LinkedList.c @@ -0,0 +1,105 @@ +#include +#include +#include + +#define MAXN 20 +#define MAXNUM 10 +#define LEN sizeof(struct LinkNode) + +struct LinkNode +{ + int Value; + struct LinkNode *Nxt; +}*Head, *Tail; + +void AddNode(int _val) +{ + struct LinkNode *_newnode; + _newnode = (struct LinkNode *)malloc(LEN); + _newnode->Value = _val; + _newnode->Nxt = NULL; + Tail->Nxt = _newnode; + Tail = _newnode; +} + +void Traversal()//2 +{ + struct LinkNode *Now = Head; + while (Now != NULL) + { + printf("%d ", Now->Value); + Now = Now->Nxt; + } + printf("\n"); +} + +void RevNode(struct LinkNode *Now)//3 +{ + if (Now->Nxt != NULL) + { + RevNode(Now->Nxt); + Now->Nxt->Nxt = Now; + } + else Head = Now; +} + +void Reverse()//3 +{ + Tail = Head; + RevNode(Head); + Tail->Nxt = NULL; +} + +int FindNode(int _val, int _t)//4 +{ + int id = 0; + struct LinkNode *Now = Head; + while (Now != NULL) + { + if (Now->Value == _val)_t--; + if (!_t)return id; + Now = Now->Nxt; id++; + } + return -1; +} + +int GetRand(int LowerBound, int UpperBound) +{ + return rand() % (UpperBound - LowerBound + 1) + LowerBound; +} + +int GetRandNum() +{ + if (GetRand(1, 4) == 1)return 5; + return GetRand(1, MAXNUM); +} + +void init() +{ + Head = Tail = NULL; + srand(time(NULL)); + int n = GetRand(5, MAXN); + for (int i = 0; i < n; ++i) + { + if (!i) + { + Head = Tail = (struct LinkNode *)malloc(LEN); + Head->Nxt = NULL; + Head->Value = GetRandNum(); + } + else AddNode(GetRandNum()); + } +} + +int main() +{ + init(); + printf("The list is:\n"); + Traversal(); + Reverse(); + printf("The reversed list is:\n"); + Traversal(); + printf("The serial number of the first node that has a value of 5 is %d(reversed)\n", FindNode(5, 1)); + printf("The serial number of the second node that has a value of 5 is %d(reversed)\n", FindNode(5, 2)); + return 0; +} diff --git a/level1/p12_warehouse/warehouse.c b/level1/p12_warehouse/warehouse.c new file mode 100644 index 00000000..2950bf05 --- /dev/null +++ b/level1/p12_warehouse/warehouse.c @@ -0,0 +1,164 @@ +#include +#include +#include +#include +#include + +#define MAXN 10000 + +char Message[11][50] = { "please input the type of goods\n","please input the quantity of goods\n","invalid input,please try again",\ +"Please choose:\n","1:Display the inventory list\n","2:Inbound\n","3:Outbound\n","4:Exit\n","done\n","No goods\n","input any key to continue\n" }; +char FileName_1[10] = "list.txt", FileName_2[10] = "list.swp"; + +struct goods +{ + int Quantity; + char Type[50]; +}Goods[MAXN + 5]; + +int TypeNum, OutNum; + +void Refresh() +{ + while (1) + { + if (_kbhit()) + { + _getch(); + break; + } + } +} + +void ReadFromFile() +{ + FILE *fp; + fp = fopen(FileName_1, "at+"); + fscanf(fp, "%d", &TypeNum); + for (int i = 0; i < TypeNum; ++i) + fscanf(fp, "%s%d", Goods[i].Type, &Goods[i].Quantity); + fclose(fp); +} + +void PrintToFile() +{ + FILE *fp; + fp = fopen(FileName_2, "w"); + fprintf(fp, "%d\n", TypeNum - OutNum); + for (int i = 0; i < TypeNum; ++i) + { + if (Goods[i].Quantity > 0) + fprintf(fp, "%s %d\n", Goods[i].Type, Goods[i].Quantity); + } + fclose(fp); + remove(FileName_1); + rename(FileName_2, FileName_1); +} + +void DisplayList() +{ + if (TypeNum == 0) + printf("%s", Message[9]); + for (int i = 0; i < TypeNum; ++i) + { + if (Goods[i].Quantity > 0) + printf("%s:%d\n", Goods[i].Type, Goods[i].Quantity); + } + printf("%s", Message[10]); +} + +void Inbound() +{ + int _num; + char _type[50]; + system("cls"); + printf("%s", Message[0]); + scanf("%s", _type); + printf("%s", Message[1]); + scanf("%d", &_num); + for (int i = 0; i < TypeNum; ++i) + { + if (strcmp(Goods[i].Type, _type) == 0) + { + Goods[i].Quantity += _num; + return; + } + } + strcpy(Goods[TypeNum].Type, _type); + Goods[TypeNum].Quantity = _num; + TypeNum++; + printf("%s", Message[8]); + printf("%s", Message[10]); +} + + +void Outbound() +{ + int _num; + char _type[50]; + system("cls"); + printf("%s", Message[0]); + scanf("%s", _type); + printf("%s", Message[1]); + scanf("%d", &_num); + for (int i = 0; i < TypeNum; ++i) + { + if (strcmp(Goods[i].Type, _type) == 0 && Goods[i].Quantity >= _num) + { + Goods[i].Quantity -= _num; + if (Goods[i].Quantity == 0)OutNum++; + printf("%s", Message[8]); + printf("%s", Message[10]); + return; + } + } + printf("%s", Message[2]); + printf("%s", Message[10]); +} + +void Menu() +{ + unsigned char ch; + do + { + system("cls"); + printf("%s", Message[3]); + printf("%s", Message[4]); + printf("%s", Message[5]); + printf("%s", Message[6]); + printf("%s", Message[7]); + while (1) + { + if (_kbhit()) + { + ch = _getch(); + switch (ch) + { + case '1': + DisplayList(); + break; + case '2': + Inbound(); + break; + case '3': + Outbound(); + break; + case '4': + PrintToFile(); + return; + default: + printf("%s", Message[2]); + } + Refresh(); + break; + } + } + } while (1); +} + +int main() +{ + ReadFromFile(); + Menu(); + return 0; +} From 4813c462b0e00b4c556a1f146a8d75f3f6426f06 Mon Sep 17 00:00:00 2001 From: me1t0ut Date: Tue, 5 Mar 2019 19:41:13 +0800 Subject: [PATCH 13/16] map modified --- level1/p10_pushBoxes/maps/2.txt | 2 +- level1/p10_pushBoxes/pushBoxes.c | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/level1/p10_pushBoxes/maps/2.txt b/level1/p10_pushBoxes/maps/2.txt index c9237946..ee0f4be6 100644 --- a/level1/p10_pushBoxes/maps/2.txt +++ b/level1/p10_pushBoxes/maps/2.txt @@ -1,4 +1,4 @@ -14 21 +14 25 ########################### # ## # # # # # ## # # ## # # ## # diff --git a/level1/p10_pushBoxes/pushBoxes.c b/level1/p10_pushBoxes/pushBoxes.c index e91634f2..d0d0e603 100644 --- a/level1/p10_pushBoxes/pushBoxes.c +++ b/level1/p10_pushBoxes/pushBoxes.c @@ -86,8 +86,7 @@ void GetCopy(int _case) { FILE *fp; char FileName[20]; - sprintf(FileName, ".\\maps\\%d.txt", _case); - remove(FileName); + sprintf(FileName, ".\\maps\\%d.swp", _case); fp = fopen(FileName, "w"); fprintf(fp, "%d %d\n", h, w); for (int i = 0; i <= h + 1; ++i) @@ -98,11 +97,14 @@ void GetCopy(int _case) void AddScore(int _case, int _score) { FILE *fp; - char FileName[20]; - sprintf(FileName, ".\\maps\\%d.txt", _case); - fp = fopen(FileName, "a+"); + char FileName_1[20], FileName_2[20]; + sprintf(FileName_1, ".\\maps\\%d.txt", _case); + sprintf(FileName_2, ".\\maps\\%d.swp", _case); + fp = fopen(FileName_2, "a+"); fprintf(fp, "%d", _score); fclose(fp); + remove(FileName_1); + rename(FileName_2, FileName_1); } int main() From 19b8b2d44c20c8243eb6e3660ea6627516897924 Mon Sep 17 00:00:00 2001 From: me1t0ut <45817393+me1t0ut@users.noreply.github.com> Date: Wed, 20 Mar 2019 20:15:52 +0800 Subject: [PATCH 14/16] pushBoxes modified --- level1/p10_pushBoxes/pushBoxes.c | 48 ++++++++++++++------------------ 1 file changed, 21 insertions(+), 27 deletions(-) diff --git a/level1/p10_pushBoxes/pushBoxes.c b/level1/p10_pushBoxes/pushBoxes.c index d0d0e603..492714b7 100644 --- a/level1/p10_pushBoxes/pushBoxes.c +++ b/level1/p10_pushBoxes/pushBoxes.c @@ -13,9 +13,18 @@ int AimNum, AimX[5], AimY[5]; int CheckStatus(int x, int y) { + if (x < 0 || x > h + 1 || y < 0 || y > w + 1)return -1; if (Maze[x][y] == '#')return 0; - else if (Maze[x][y] == 'o')return 1; - return 2; + if (Maze[x][y] == 'o')return 1; + else + { + for (int k = 1; k <= AimNum; ++k) + { + if (x == AimX[k] && y == AimY[k]) + return 2; + } + } + return 3; } void PrintMaze(int _case) @@ -27,19 +36,7 @@ void PrintMaze(int _case) for (int j = 0; j <= w + 1; ++j) { if (i == NowX && j == NowY)printf("S"); - else if (Maze[i][j] != 'o') - { - flag = 0; - for (int k = 1; k <= AimNum; ++k) - { - if (i == AimX[k] && j == AimY[k]) - { - printf("O"); - flag = 1; - } - } - if (!flag)printf("%c", Maze[i][j]); - } + else if (CheckStatus(i, j) == 2)printf("O"); else printf("%c", Maze[i][j]); } printf("\n"); @@ -110,7 +107,7 @@ void AddScore(int _case, int _score) int main() { unsigned char ch; - int tx, ty, flag, Score; + int tx, ty, flag[2], Score; for (int i = 1; i <= CASES; ++i) { NowX = NowY = 1; @@ -133,19 +130,16 @@ int main() AddScore(i, Record); return 0; } - flag = CheckStatus(NowX + tx, NowY + ty); - if (flag == 1) + flag[0] = CheckStatus(NowX + tx, NowY + ty); + flag[1] = CheckStatus(NowX + tx * 2, NowY + ty * 2); + if (flag[0] == 1 && flag[1] >= 2) { - flag = CheckStatus(NowX + tx * 2, NowY + ty * 2); - if (flag == 2) - { - Maze[NowX + tx][NowY + ty] = ' '; - Maze[NowX + tx * 2][NowY + ty * 2] = 'o'; - NowX += tx; NowY += ty; - Score--; - } + Maze[NowX + tx][NowY + ty] = ' '; + Maze[NowX + tx * 2][NowY + ty * 2] = 'o'; + NowX += tx; NowY += ty; + Score--; } - else if (flag == 2) + else if (flag[0] >= 2) { NowX += tx; NowY += ty; From 3b8489fc1e59688935f9c3e821b8c4d81b0667b2 Mon Sep 17 00:00:00 2001 From: me1t0ut <45817393+me1t0ut@users.noreply.github.com> Date: Wed, 20 Mar 2019 20:55:02 +0800 Subject: [PATCH 15/16] pushBoxes modified --- level1/p10_pushBoxes/pushBoxes.c | 58 ++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/level1/p10_pushBoxes/pushBoxes.c b/level1/p10_pushBoxes/pushBoxes.c index 492714b7..c69c9116 100644 --- a/level1/p10_pushBoxes/pushBoxes.c +++ b/level1/p10_pushBoxes/pushBoxes.c @@ -13,23 +13,22 @@ int AimNum, AimX[5], AimY[5]; int CheckStatus(int x, int y) { - if (x < 0 || x > h + 1 || y < 0 || y > w + 1)return -1; + if (x < 0 || x > h + 1 || y < 0 || y > w + 1)return -1; if (Maze[x][y] == '#')return 0; if (Maze[x][y] == 'o')return 1; else - { - for (int k = 1; k <= AimNum; ++k) - { - if (x == AimX[k] && y == AimY[k]) - return 2; - } - } + { + for (int k = 1; k <= AimNum; ++k) + { + if (x == AimX[k] && y == AimY[k]) + return 2; + } + } return 3; } void PrintMaze(int _case) { - int flag; printf("Case#%d begins,your game record is %d\n", _case, Record); for (int i = 0; i <= h + 1; ++i) { @@ -104,10 +103,32 @@ void AddScore(int _case, int _score) rename(FileName_2, FileName_1); } +int MoveCharactor(int tx, int ty) +{ + int flag[2]; + flag[0] = CheckStatus(NowX + tx, NowY + ty); + flag[1] = CheckStatus(NowX + tx * 2, NowY + ty * 2); + if (flag[0] == 1 && flag[1] >= 2) + { + Maze[NowX + tx][NowY + ty] = ' '; + Maze[NowX + tx * 2][NowY + ty * 2] = 'o'; + NowX += tx; NowY += ty; + Score--; + } + else if (flag[0] >= 2) + { + NowX += tx; + NowY += ty; + Score--; + } + else return 1; + return 0; +} + int main() { unsigned char ch; - int tx, ty, flag[2], Score; + int tx, ty, Score; for (int i = 1; i <= CASES; ++i) { NowX = NowY = 1; @@ -130,22 +151,7 @@ int main() AddScore(i, Record); return 0; } - flag[0] = CheckStatus(NowX + tx, NowY + ty); - flag[1] = CheckStatus(NowX + tx * 2, NowY + ty * 2); - if (flag[0] == 1 && flag[1] >= 2) - { - Maze[NowX + tx][NowY + ty] = ' '; - Maze[NowX + tx * 2][NowY + ty * 2] = 'o'; - NowX += tx; NowY += ty; - Score--; - } - else if (flag[0] >= 2) - { - NowX += tx; - NowY += ty; - Score--; - } - else continue; + if (MoveCharactor(tx, ty))continue; system("cls"); PrintMaze(i); if (GameFin())break; From 13cd1121a2d1106f336c944139243cc19e89cf9c Mon Sep 17 00:00:00 2001 From: me1t0ut <45817393+me1t0ut@users.noreply.github.com> Date: Wed, 20 Mar 2019 21:15:23 +0800 Subject: [PATCH 16/16] pushBoxes modified --- level1/p10_pushBoxes/pushBoxes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/level1/p10_pushBoxes/pushBoxes.c b/level1/p10_pushBoxes/pushBoxes.c index c69c9116..6d1536f2 100644 --- a/level1/p10_pushBoxes/pushBoxes.c +++ b/level1/p10_pushBoxes/pushBoxes.c @@ -103,7 +103,7 @@ void AddScore(int _case, int _score) rename(FileName_2, FileName_1); } -int MoveCharactor(int tx, int ty) +int MoveCharacter(int tx, int ty) { int flag[2]; flag[0] = CheckStatus(NowX + tx, NowY + ty); @@ -151,7 +151,7 @@ int main() AddScore(i, Record); return 0; } - if (MoveCharactor(tx, ty))continue; + if (MoveCharacter(tx, ty))continue; system("cls"); PrintMaze(i); if (GameFin())break;