-
个人简介
天佑吾皇,常胜利,沐荣光! 菜鸡一枚!! 如果你敢当My son,我就不介意成为Your father!!! 《关于我用个十六中账号冲到LV10这件事》 命运,永远站在我这边!
//最完整版头文件 #include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stack> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <complex.h> #include <fenv.h> #include <inttypes.h> #include <stdbool.h> #include <stdint.h> #include <tgmath.h> #include <windows.h>
/*贪吃蛇/ /*2012-11-20/
#include <iostream> #include <cstdio> #include <cstdlib> #include <ctime> #include <conio.h> #include <cmath> #include <windows.h> using namespace std;
/*** 光标定位 ***/ HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE); COORD coord;
void locate(int x,int y) { coord.X=y; coord.Y=x; SetConsoleCursorPosition(hout,coord); };
/*** 隐藏光标 ***/ void hide() { CONSOLE_CURSOR_INFO cursor_info={1,0}; SetConsoleCursorInfo(hout, &cursor_info); }
/*** 生成随机数 ***/ double random(double start, double end) { return start+(end-start)*rand()/(RAND_MAX + 1.0); }
/*** 定义地图的长宽,蛇的坐标,长度,方向,食物的位置 ***/ int m,n;
struct node { int x,y; }snake[1000];
int snake_length,dir; node food; int direct[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
/*** 输出墙 ***/ void print_wall() { cout << " "; for (int i=1;i<=n;i++) cout << "-"; cout << endl; for (int j=0;j<=m-1;j++) { cout << "|"; for (int i=1;i<=n;i++) cout << " "; cout << "|" << endl; } cout << " "; for (int i=1;i<=n;i++) cout << "-"; }
/*** 首次输出蛇,其中snake[0]代表头 **/ void print_snake() { locate(snake[0].x,snake[0].y); cout << "@"; for (int i=1;i<=snake_length-1;i++) { locate(snake[i].x,snake[i].y); cout << ""; } }
/*** 判断是否撞墙或者自撞 ***/ bool is_correct() { if (snake[0].x0 || snake[0].y0 || snake[0].xm+1 || snake[0].yn+1) return false; for (int i=1;i<=snake_length-1;i++) { if (snake[0].xsnake[i].x && snake[0].ysnake[i].y) return false; } return true; }
/*** 随机生成并输出食物位置 ***/ bool print_food() { srand((unsigned)time(0)); bool e; while (1) { e=true; int i=(int) random(0,m)+1,j=(int) random(0,n)+1; food.x=i;food.y=j; for (int k=0;k<=snake_length-1;k++) { if (snake[k].xfood.x && snake[k].yfood.y) { e=false;break; } } if (e) break; } locate(food.x,food.y); cout << "$"; return true; }
/*** 蛇的前进 / bool go_ahead() { node temp; bool e=false; temp=snake[snake_length-1]; for (int i=snake_length-1;i>=1;i--) snake[i]=snake[i-1]; snake[0].x+=direct[dir][0]; snake[0].y+=direct[dir][1]; locate(snake[1].x,snake[1].y); cout << ""; /* 吃到了食物 / if (snake[0].xfood.x && snake[0].yfood.y) { snake_length++; e=true; snake[snake_length-1]=temp; } / 输出此时蛇状态 / if (!e) { locate(temp.x,temp.y); cout << " "; } else print_food(); locate(snake[0].x,snake[0].y); cout << "@"; / 如果自撞 ***/ if (!is_correct()) { system("cls"); cout << "You lose!" << endl << "Length: " << snake_length << endl; return false; } return true; }
/*** 主函数 / int main() { cout << "--------------------贪吃蛇---------------------" << endl; cout << "请注意窗口大小,以免发生错位.建议将窗口调为最大." << endl; cout << "先选择难度.请在1-10中输入1个数,1最简单,10则最难" << endl; cout << "然后进入游戏画面,以方向键控制方向.祝你游戏愉快!" << endl; cout << "-----------------------------------------------" << endl; m=25; n=40; if (m<10 || n<10 || m>25 || n>40) { cout << "ERROR" << endl; system("pause"); return 0; } int hard; cin >> hard; if (hard<=0 || hard>100) { cout << "ERROR" << endl; system("pause"); return 0; } / 数据全部初始化,包括蛇长,位置,方向 / snake_length=5; clock_t a,b; char ch; double hard_len; for (int i=0;i<=4;i++) { snake[i].x=1; snake[i].y=5-i; } dir=3; / 输出初始地图,蛇与食物 / system("cls"); hide(); print_wall(); print_food(); print_snake(); locate(m+2,0); cout << "Now length: "; / 开始游戏 / while (1) { / 难度随长度增加而提高 / hard_len=(double)snake_length/(double) (mn); /* 调节时间,单位是ms / a=clock(); while (1) { b=clock(); if (b-a>=(int)(400-30hard)(1-sqrt(hard_len))) break; } /** 接受键盘输入的上下左右,并以此改变方向 / if (kbhit()) { ch=getch(); if (ch==-32) { ch=getch(); switch(ch) { case 72: if (dir2 || dir3) dir=0; break; case 80: if (dir2 || dir3) dir=1; break; case 75: if (dir0 || dir1) dir=2; break; case 77: if (dir0 || dir1) dir=3; break; } } } / 前进 / if (!go_ahead()) break; / 在最后输出此时长度 ***/ locate(m+2,12); cout << snake_length; } system("pause"); return 0; }
-
通过的题目
- P1
- P2
- P3
- P5
- P6
- P7
- P8
- P10
- P11
- P12
- P13
- P14
- P15
- P16
- P17
- P18
- P19
- P20
- P21
- P22
- P23
- P24
- P25
- P26
- P27
- P28
- P29
- P30
- P31
- P32
- P33
- P34
- P35
- P36
- P37
- P38
- P39
- P41
- P43
- P45
- P46
- P47
- P48
- P49
- P50
- P52
- P53
- P54
- P55
- P56
- P57
- P58
- P59
- P60
- P61
- P63
- P65
- P66
- P67
- P68
- P69
- P70
- P72
- P73
- P75
- P76
- P77
- P78
- P79
- P80
- P83
- P84
- P85
- P86
- P87
- P88
- P90
- P91
- P92
- P93
- P94
- P95
- P98
- P99
- P100
- P101
- P102
- P103
- P104
- P105
- P106
- P107
- P108
- P109
- P110
- P111
- P112
- P113
- P114
- P115
- P116
- P117
- P118
- P119
- P121
- P122
- P125
- P130
- P132
- P137
- P142
- P143
- P144
- P164
- P168
- P173
- P182
- P183
- P186
- P188
- P194
- P195
- P196
- P197
- P198
- P213
- P222
- P241
- P250
- P297
- P323
- P378
- P407
- P468
- P477
- P502
- P518
- P545
- P554
- P561
- P569
- P577
- P584
- P585
- P589
- P596
- P602
- P641
- P649
- P657
- P659
- P660
- P661
- P665
- P672
- P676
- P677
- P684
- P691
- P692
- P694
- P696
- P697
- P699
- P701
- P704
- P708
- P711
- P712
- P713
- P714
- P715
- P718
- P724
- P726
- P749
- P754
- P765
- P769
- P785
- P800
- P811
- P813
- P814
- P815
- P816
- P817
- P818
- P819
- P820
- P821
- P822
- P823
- P824
- P825
- P826
- P827
- P828
- P829
- P830
- P831
- P832
- P833
- P834
- P835
- P836
- P837
- P838
- P839
- P840
- P841
- P842
- P843
- P844
- P845
- P846
- P847
- P848
- P849
- P850
- P851
- P852
- P853
- P854
- P855
- P856
- P857
- P858
- P859
- P860
- P861
- P862
- P863
- P864
- P865
- P866
- P867
- P868
- P869
- P870
- P871
- P872
- P873
- P874
- P875
- P876
- P877
- P878
- P879
- P880
- P881
- P882
- P883
- P884
- P885
- P886
- P887
- P888
- P889
- P890
- P891
- P892
- P894
- P895
- P896
- P897
- P898
- P899
- P900
- P901
- P902
- P903
- P904
- P905
- P906
- P907
- P908
- P909
- P910
- P911
- P912
- P913
- P914
- P915
- P916
- P917
- P918
- P919
- P920
- P921
- P922
- P923
- P924
- P925
- P926
- P927
- P928
- P929
- P930
- P931
- P932
- P933
- P934
- P935
- P936
- P937
- P938
- P939
- P940
- P941
- P942
- P943
- P944
- P946
- P947
- P948
- P949
- P951
- P952
- P953
- P954
- P955
- P956
- P957
- P958
- P959
- P960
- P961
- P962
- P963
- P964
- P967
- P970
- P972
- P974
- P975
- P977
- P978
- P980
- P981
- P982
- P983
- P984
- P985
- P986
- P987
- P988
- P989
- P990
- P991
- P993
- P994
- P995
- P996
- P997
- P998
- P999
- P1000
- P1001
- P1002
- P1003
- P1004
- P1006
- P1007
- P1008
- P1012
- P1014
- P1015
- P1016
- P1017
- P1018
- P1019
- P1020
- P1021
- P1023
- P1024
- P1025
- P1026
- P1027
- P1028
- P1030
- P1031
- P1034
- P1035
- P1036
- P1038
- P1040
- P1042
- P1043
- P1044
- P1048
- P1049
- P1052
- P1055
- P1057
- P1058
- P1062
- P1063
- P1064
- P1068
- P1071
- P1073
- P1075
- P1078
- P1079
- P1080
- P1084
- P1085
- P1089
- P1090
- P1091
- P1092
- P1093
- P1094
- P1095
- P1098
- P1099
- P1102
- P1107
- P1110
- P1114
- P1115
- P1118
- P1120
- P1128
- P1132
- P1134
- P1135
- P1138
- P1140
- P1153
- P1162
- P1163
- P1166
- P1178
- P1182
- P1184
- P1185
- P1186
- P1187
- P1188
- P1189
- P1190
- P1191
- P1193
- P1194
- P1195
- P1196
- P1197
- P1198
- P1200
- P1201
- P1202
- P1218
- P1219
- P1222
- P1223
- P1224
- P1225
- P1230
- P1237
- P1238
- P1239
- P1242
- P1243
- P1259
- P1261
- P1280
- P1282
- P1283
- P1284
- P1285
- P1295
- P1296
- P1300
- P1304
- P1325
- P1326
- P1328
- P1341
- P1345
- P1352
- P1353
- P1355
- P1357
- P1358
- P1359
- P1363
- P1364
- P1365
- P1366
- P1367
- P1368
- P1384
- P1399
- P1401
- P1402
- P1404
- P1405
- P1407
- P1408
- P1412
- P1429
- P1433
- P1483
- P1489
- P1490
- P1501
- P1502
- P1505
- P1509
- P1538
- P1551
- P1553
- P1675
- P1676
- P1677
- P1684
- P1701
- P1763
- P1828
- P1848
- P1929
- P1954
- P1966
- P2000
- P2275
- P2303
- P2474
- P2554
- P2555
- P2585
- P2607
- P2627
- P2670
- P2720
- P2721
- P2722
- P2723
- P2800
- P2910
- P2948
- P2953
- P3022
- P3047
- P3070
- P3074
- P3086
- P3088
- P3130
- P3142
- P0168
- P3172
- P3173
- P3181
- P3191
- P3199
- P3222
题目标签
- 语言基础
- 135
- 循环语句
- 91
- 竞赛
- 56
- 搜索
- 45
- NOIP
- 43
- 字符串
- 42
- 动态规划
- 41
- 选择语句
- 41
- 其他
- 38
- 一维数组
- 38
- 语言入门
- 36
- 字符数组
- 29
- 贪心
- 28
- 高精度
- 25
- 年份
- 24
- 数学知识
- 22
- 二维数组
- 22
- 数据结构
- 21
- 普及组
- 19
- 模拟
- 17