约瑟夫环-我的解答与迷宫
1 #include<iostream> 2 using namespace std; 3 4 typedef int ElemType; 5 typedef struct LNode 6 { 7 ElemType data1; 8 ElemType data2; 9 struct LNode *next; 10 }LNode,*LinkList; 11 LinkList CreateList(int n) 12 { 13 LinkList head,s; 14 s=head=(LinkList)malloc(sizeof(LNode)); 15 for(int i=1;i<n;i++) 16 { 17 s->data1=i; 18 cout<<"Please input cipher:"; 19 cin>>s->data2; 20 if(s->data2<=0) 21 { 22 cout<<"Please input the cipher again:"<<endl; 23 cin>>s->data2; 24 } 25 s->next=(LinkList)malloc(sizeof(LNode)); 26 s=s->next; 27 } 28 s->data1=i; 29 cout<<"Please input cipher:"<<endl; 30 cin>>s->data2; 31 s->next=head; 32 return s; 33 } 34 35 LinkList findLNode(int m,LinkList s) 36 { 37 for(int i=1;i<m;i++) 38 s=s->next; 39 return s; 40 } 41 42 void main() 43 { 44 int m,n; 45 cout<<"Please input n:"<<endl; 46 cin>>n; 47 cout<<"Please input m:"<<endl; 48 cin>>m; 49 LinkList p,q; 50 p=CreateList(n); 51 52 for(int i=0;i<n;i++) 53 { 54 p=findLNode(m,p); 55 cout<<p->next->data1<<" "; 56 m=p->next->data2; 57 q=p->next; 58 p->next=q->next; 59 //p->next=q->next; 60 free(q); 61 //p=p->next; 62 63 } 64 }
1 #include<stdio.h> 2 #include <stdlib.h> 3 4 int visit(int, int); 5 6 int maze[7][7] = {{2, 2, 2, 2, 2, 2, 2}, 7 {2, 0, 0, 0, 0, 0, 2}, 8 {2, 0, 2, 0, 2, 0, 2}, 9 {2, 0, 0, 2, 0, 2, 2}, 10 {2, 2, 0, 2, 0, 2, 2}, 11 {2, 0, 0, 0, 0, 0, 2}, 12 {2, 2, 2, 2, 2, 2, 2}}; 13 14 int startI = 1, startJ = 1; // 入口 15 int endI = 5, endJ = 5; // 出口 16 int success = 0; 17 18 int main(void) { 19 int i, j; 20 21 printf("顯示迷宮:\n"); 22 for(i = 0; i < 7; i++) { 23 for(j = 0; j < 7; j++) 24 if(maze[i][j] == 2) 25 printf("█"); 26 else 27 printf(" "); 28 printf("\n"); 29 } 30 31 if(visit(startI, startJ) == 0) 32 printf("\n沒有找到出口!\n"); 33 else { 34 printf("\n顯示路徑:\n"); 35 for(i = 0; i < 7; i++) { 36 for(j = 0; j < 7; j++) { 37 if(maze[i][j] == 2) 38 printf("█"); 39 else if(maze[i][j] == 1) 40 printf("◇"); 41 else 42 printf(" "); 43 } 44 printf("\n"); 45 } 46 } 47 48 return 0; 49 } 50 51 int visit(int i, int j) { 52 maze[i][j] = 1; 53 54 if(i == endI && j == endJ) 55 success = 1; 56 57 if(success != 1 && maze[i][j+1] == 0) visit(i, j+1); 58 if(success != 1 && maze[i+1][j] == 0) visit(i+1, j); 59 if(success != 1 && maze[i][j-1] == 0) visit(i, j-1); 60 if(success != 1 && maze[i-1][j] == 0) visit(i-1, j); 61 62 if(success != 1) 63 maze[i][j] = 0; 64 65 return success; 66 }
1 #include<stdio.h> 2 #include <stdlib.h> 3 int visit(int, int); 4 int maze[7][7] = {{2, 2, 2, 2, 2, 2, 2}, 5 {2, 0, 0, 0, 0, 0, 2}, 6 {2, 0, 2, 0, 2, 0, 2}, 7 {2, 0, 0, 2, 0, 2, 2}, 8 {2, 2, 0, 2, 0, 2, 2}, 9 {2, 0, 0, 0, 0, 0, 2}, 10 {2, 2, 2, 2, 2, 2, 2}}; 11 12 int startI = 1, startJ = 1; // 入口 13 int endI = 5, endJ = 5; // 出口 14 int success = 0; 15 16 int main(void) { 17 int i, j; 18 19 printf("顯示迷宮:\n"); 20 for(i = 0; i < 7; i++) { 21 for(j = 0; j < 7; j++) 22 if(maze[i][j] == 2) 23 printf("■"); 24 else 25 printf(" "); 26 printf("\n"); 27 } 28 29 if(visit(startI, startJ) == 0) 30 printf("\n沒有找到出口!\n"); 31 else { 32 printf("\n顯示路徑:\n"); 33 for(i = 0; i < 7; i++) { 34 for(j = 0; j < 7; j++) { 35 if(maze[i][j] == 2) 36 printf("■"); 37 else if(maze[i][j] == 1) 38 printf("▲"); 39 else 40 printf(" "); 41 } 42 printf("\n"); 43 } 44 } 45 46 return 0; 47 } 48 49 int visit(int i, int j) { 50 maze[i][j] = 1; 51 52 if(i == endI && j == endJ) 53 success = 1; 54 55 if(success != 1 && maze[i][j+1] == 0) visit(i, j+1); 56 if(success != 1 && maze[i+1][j] == 0) visit(i+1, j); 57 if(success != 1 && maze[i][j-1] == 0) visit(i, j-1); 58 if(success != 1 && maze[i-1][j] == 0) visit(i-1, j); 59 60 if(success != 1) 61 maze[i][j] = 0; 62 63 return success; 64 }