Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
大家可以看下,这个代码我注释了,可以通过,应该可以好理解#include <stdio.h> #include <string.h> int a[30][30]; int top[30]; typedef struct { int value; int index_x; int index_y; }Point1; Point1 Point[30]; void MoveChange(char *str1,int num1,char *str2,int num2) { int From_x = Point[num1].index_x; int From_y = Point[num1].index_y; int To_x = Point[num2].index_x; int To_y = Point[num2].index_y; int i,j,k; int value_x; if (From_x == To_x) { return ; } // move a onto b if ( strcmp(str1,"move")==0 && strcmp(str2,"onto")==0 ) { i = From_x; j = From_y+1; while( a[i][j] != -1) { top[i]--; value_x = a[i][j] ; a[ value_x ] [ top[ value_x] ]= a[i][j]; Point[value_x].index_x = value_x ; Point[ value_x ].index_y = top[ value_x]; top[value_x ]++; a[i][j] = -1; j++; } i = To_x; j = To_y+1; while ( a[i][j] !=-1) { top[i]--; value_x = a[i][j] ; a[ value_x ] [ top[ value_x] ]= a[i][j]; Point[ value_x ].index_x = value_x ; Point[ value_x ].index_y = top[ value_x]; top[value_x ]++; a[i][j] =-1; j++; } a[ To_x][ To_y +1] = num1; Point[num1].index_x = To_x; Point[num1].index_y = To_y+1; top[To_x]++; top[From_x]--; a[From_x][From_y]=-1; return ; } // move a over b if ( strcmp(str1,"move")==0 && strcmp(str2,"over")==0 ) { i = From_x; j = From_y+1; while( a[i][j] != -1) { top[i]--; value_x = a[i][j] ; a[ value_x ] [ top[ value_x] ] = a[i][j]; Point[ value_x ].index_x = value_x ; Point[ value_x ].index_y = top[ value_x]; top[value_x ]++; a[i][j] = -1; j++; } a[To_x][top[To_x]] = num1; Point[num1].index_x = To_x; Point[num1].index_y = top[To_x]; top[To_x]++; top[From_x]--; a[From_x][From_y] = -1; return ; } // pile a onto b if ( strcmp(str1,"pile")==0 && strcmp(str2,"onto")==0 ) { i = To_x; j = To_y+1; while ( a[i][j] !=-1) //把b上的模仿移动到原来位置上 { top[i]--; value_x = a[i][j] ; a[ value_x ] [ top[ value_x] ]= a[i][j]; Point[ value_x ].index_x = value_x ; Point[ value_x ].index_y = top[ value_x]; top[value_x ]++; a[i][j] =-1; j++; } i = From_x; j = From_y; k = To_y+1; while ( a[i][j]!=-1) { top[i]--; a[To_x][k++] = a[i][j]; top[To_x]++; Point[ a[i][j] ].index_x = To_x; Point[ a[i][j]].index_y = top[To_x] -1; a[i][j]=-1; j++; } return ; } // pile a over b if ( strcmp(str1,"pile")==0 && strcmp(str2,"over")==0 ) { i = From_x; j = From_y; while ( a[i][j]!=-1) { top[i]--; a[To_x][top[To_x]] = a[i][j]; top[To_x]++; Point[ a[i][j] ].index_x = To_x; Point[ a[i][j]].index_y = top[To_x] -1; a[i][j]=-1; j++; } return ; } return ; } void print(int n) { for (int i=0;i<n;i++ ) { printf("%d:",i); for (int j=0;j< top[i];j++) { printf(" "); printf("%d",a[i][j]); } printf("\n"); } } int main() { char str1[20],str2[20]; int n; int num1,num2; while(EOF!=scanf("%d",&n)) { memset(a,-1,sizeof(a)); for (int i=0;i<30;i++) { Point[i].value = i; Point[i].index_x=i; Point[i].index_y=0; a[i][0] = i; top[i] = 1; } while (1) { scanf("%s",str1); if(strcmp(str1,"quit") ==0) { break; } scanf("%d",&num1); scanf("%s",str2); scanf("%d",&num2); MoveChange(str1, num1,str2, num2); } print(n); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator