回复 10楼 lin5161678
赞同!但迷信的人多了,现在的编译器不得不加入了一段优化,就是将这些“利用异或交换变量”的代码片段更换为正常方法。
if ( m[new_self[0]][new_self[1]] & 0b010 ) move(m,com,new_self);
*t2 |= ( (*t1) & 0b011 ); *t1 &= 0b100;
#include<stdio.h> #include<stdlib.h> #include<conio.h> #define W 15 #define H 15 enum COMD { EMPTY, UP, DOWN, LEFT, RIGHT }; unsigned g_f = 0; unsigned g_nf = 0; char M[H][W] = { {8,8,8,8,8,8,8,8,8}, {8,8,0,0,8,8,8,8,8}, {8,8,2,0,0,0,2,0,8}, {8,8,0,4,8,4,0,0,8}, {8,8,0,8,8,8,0,8,8}, {8,0,1,4,8,4,0,8,8}, {8,0,2,0,0,0,2,8,8}, {8,8,0,8,8,0,0,8,8}, {8,8,8,8,8,8,8,8,8} }; void find_id( char[][],int[] ); void fun( char[][], int[] ); int get_com( char[][],int[] ); int move( char[][],int[],int[] ); int judge_move (char[][],int[],int[] ); int check_win( char[][] ); void print( char[][] ); int main( void ) { int point[2] = {0,0}; find_id(M,point); print(M); if (!check_win(M)) while ( !fun(M,point)); puts("恭喜过关!"); return 0; } void fun( char m[H][W], int self[2] ) { int com[2] = {0,0}; int new_self[2] = {0,0}; if (get_com(M,com) == 0 ) return ; new_self[0] = self[0] + com[0]; new_self[1] = self[1] + com[1]; if ( m[new_self[0]][new_self[1]] & 0b010 ) move(m,com,new_self); if ( !move(m,com,self)) return ; self[0] = new_self[0]; self[1] = new_self[1]; print(M); return check_win(M); } int get_com( char m[H][W],int re_com[] ) { static char C[256] = {0}; C['w'] = UP; C['s'] = DOWN; C['a'] = LEFT; C['d'] = RIGHT; C['W'] = UP; C['S'] = DOWN; C['A'] = LEFT; C['D'] = RIGHT; C[72] = UP; C[80] = DOWN; C[75] = LEFT; C[77] = RIGHT; int com[2] = {0,0}; char ch = 0; ch= getch(); unsigned flag = 1; switch (C[ch]) { case UP: com[0] = -1;break; case DOWN: com[0] = 1;break; case LEFT: com[1] = -1;break; case RIGHT: com[1] = 1;break; default: flag = 0; break; } re_com[0] = com[0]; re_com[1] = com[1]; return flag; } int move( char m[H][W],int com[],int self[] ) { int new_self[2] ={0,0}; char* t1 = NULL; char* t2 = NULL; if (!judge_move(m,com,self)) return 0; new_self[0] = self[0] + com[0]; new_self[1] = self[1] + com[1]; t1=&m[self[0]][self[1]]; t2=&m[new_self[0]][new_self[1]]; *t2 |= ( (*t1) & 0b011 ); if ( *t2 == 0b110 ) ++g_nf; if ( *t1 == 0b110 ) --g_nf; *t1 &= 0b100; return 1; } int judge_move ( char m[H][W],int com[],int self[] ) { static int j_c[20] = {1,0,0,0,1,0,0,0,0,0,0}; int new_self[2]; new_self[0] = self[0] + com[0]; new_self[1] = self[1] + com[1]; return j_c[m[new_self[0]][new_self[1]]]; } void find_id( char m[H][W],int self[] ) { size_t i; size_t j; unsigned flag = 0; for ( i = 0; i != H; ++i ) for ( j = 0; j != W; ++ j ) if ( ( m[i][j] & 0b001 ) && flag <=1 ) { self[0] = i; self[1] = j; ++flag; } else if ( m[i][j]& 0b100 ) ++g_f; if ( flag == 1 ) return ; else if (flag == 0 ) puts("没有起点!"); else puts("起点不止一个"); exit(EXIT_FAILURE); } int check_win( char m[H][W] ) { return g_f ==g_nf; } void print( char m[H][W] ) { static char s_c[] = { ' ','O','@','?','X','S','$','?','#','#','#','#','#','#'}; size_t i; size_t j; system("cls"); for ( i = 0; i != H; ++i ) for ( j = 0; j != W + 1; ++ j ) putchar(j != W?s_c[m[i][j]]:'\n'); puts(""); }
[此贴子已经被作者于2021-5-30 23:08编辑过]
#include<stdio.h> #include<stdlib.h> #include<conio.h> #define H 10 #define W 15 char M[H][W] = { {8,8,8,8,8,8,8,8,8,8,8,8}, {8,8,5,8,8,8,8,8,8,8,8,8}, {8,5,5,5,5,5,5,5,5,8,8,8}, {8,8,8,5,5,5,5,8,5,5,5,8}, {8,8,8,5,5,7,8,8,8,8,5,8}, {8,5,5,5,5,5,5,5,5,5,5,8}, {8,5,8,8,8,5,5,8,5,5,8,8}, {8,5,8,8,8,8,5,8,8,5,8,8}, {8,5,5,5,6,8,5,5,5,5,5,8}, {8,8,8,8,8,8,8,8,8,8,8,8} }; void find_id( char[][],int*,int* ); void fun( char [][],int,int ); int judge_move( char[][],int,int,int ); void re_way( char[][],int[][],int,int ); void print( char[][] ); int main( void ) { int x; int y; find_id(M,&x,&y); fun(M,x,y); return 0; } void fun( char m[H][W],int x,int y ) { int com[4][2] = { {0,-1},{-1,0},{0,1},{1,0} }; int t_x = x; int t_y = y; int div = 0; int mark = 0; int flag; while (1) { const int t_div = ( div + 3 ) % 4; const int d_x = com[t_div][0]; const int d_y = com[t_div][1]; flag = judge_move( m,x + d_x, y + d_y,t_div ); if ( flag == 0 ) { m[x][y] = div = t_div; x += d_x; y += d_y; } else if ( flag == 1) div = (++div % 4); else if ( flag == 2) { m[x][y] = t_div; break; } if ( x == t_x && y == t_y && ( ++mark == 4 ) ) break; } if ( flag == 2 ) re_way(m,com,t_x,t_y); else { m[x][y] = 6; print(m); puts("没路!"); } } int judge_move( char m[H][W],int x,int y,int t_div ) { #define ABS(n) ((n) >= 0 ? (n) : (-(n))) if ( m[x][y] == 7 ) return 2; if ( m[x][y] == 5 ) return 0; return ( m[x][y] == 8 || ( ABS(m[x][y] - t_div) != 2 )); #undef ABS } void re_way( char m[H][W],int com[4][2],int x,int y ) { while ( m[x][y] != 7 ) { const int t = m[x][y]; m[x][y] +=10; x += com[t][0]; y += com[t][1]; print(m); getch(); } } void print( char m[H][W] ) { static char s_c[] = { ' ',' ',' ',' ','*',' ','O','X','#','$','<','^','>','v'}; size_t i; size_t j; system("cls"); for ( i = 0; i != H; ++i ) for ( j = 0; j != W + 1; ++ j ) putchar(j != W?s_c[m[i][j]]:'\n'); } void find_id( char m[H][W], int* x,int* y ) { size_t i; size_t j; unsigned flag = 0; for ( i = 0; i != H; ++i ) for ( j = 0; j != W; ++ j ) if ( ( m[i][j] ==6 ) && flag <=1 ) { *x = i; *y = j; ++flag; } if ( flag == 1 ) return ; else if (flag == 0 ) puts("没有起点!"); else puts("起点不止一个"); exit(EXIT_FAILURE); }
[此贴子已经被作者于2021-6-1 00:25编辑过]
char*f="char*f=%c%s%c;main(){printf(f,34,f,34,10);}%c";main(){printf(f,34,f,34,10);}