下面这个C代码怎样
本帖最后由 蚊见蚊爱 于 2011-04-26 23:15 编辑
这个是个15数码的程序,大家评评代码写的如何?
- #define row(x) ((x) >>2)
- #define col(x) ((x) &0x3)
- #include <stdio.h>
- #include <time.h>
- char board[16];
- char start[16];
- short tim[100];
- char dir[100],pos[100];
- int timer;
- main(int argc,char*argv[])
- {
- register int j,k,s,t,l,d,p,q,del,piece,moves;
- if(argc!=2){
- fprintf(stderr,"Usage: %s startposition\n",argv[0]);
- exit(-1);
- }
- for(j= 0;k= argv[1][j];j++){
- if(k>='0'&&k<='9')k-= '0';
- else if(k>='a'&&k<='f')k-= 'a'-10;
- else{
- fprintf(stderr,
- "The start position should use only hex digits (0123456789abcdef)!\n");
- exit(-2);
- }
- if(start[k]){
- fprintf(stderr,"Your start position uses %x twice!\n",k);
- exit(-3);
- }
- start[k]= 1;
- }
- for(k= 0;k<16;k++)if(start[k]==0){
- fprintf(stderr,"Your start position doesn't use %x!\n",k);
- exit(-4);
- }
- for(del= j= 0;k= argv[1][j];j++){
- if(k>='0'&&k<='9')k-= '0';else k-= 'a'-10;
- start[j]= k-1;
- for(s= 0;s<j;s++)if(start[s]> start[j])del++;
- if(k==0)t= j;
- }
- if(((row(t)+col(t)+del)&0x1)==0){
- printf("Sorry ... the goal is unreachable from that start position!\n");
- exit(0);
- }
- for(j= moves= 0;j<16;j++)if(start[j]>=0){
- del= row(start[j])-row(j);
- moves+= (del<0?-del:del);
- del= col(start[j])-col(j);
- moves+= (del<0?-del:del);
- }
- if(moves==0)goto win;
- while(1){
- timer= time(0);
- t= moves;
- for(j= 0;j<16;j++){
- board[j]= start[j];
- if(board[j]<0)p= j;
- }
- pos[0]= 16,l= 1;
- newlevel:
- d= 0,tim[l]= t,pos[l]= p,q= pos[l-1];
- trymove:
- switch(d){
- case 0:
- if(col(p)<=2&&q!=p+1)/*7:*/
- {
- q= p+1,piece= board[q];
- del= (col(piece)<col(q)?0x1:0x100);
- break;
- }
- d++;
- case 1:
- if(row(p)>=1&&q!=p-4)/*8:*/
- {
- q= p-4,piece= board[q];
- del= (row(piece)> row(q)?0x1:0x100);
- break;
- }
- d++;
- case 2:
- if(col(p)>=1&&q!=p-1)/*9:*/
- {
- q= p-1,piece= board[q];
- del= (col(piece)> col(q)?0x1:0x100);
- break;
- }
- d++;
- case 3:
- if(row(p)<=2&&q!=p+4)/*10:*/
- {
- q= p+4,piece= board[q];
- del= (row(piece)<row(q)?0x1:0x100);
- break;
- }
- d++;
- case 4:
- goto backtrack;
- }
- if(t<=del){
- if(t==del)goto win;
- d++;goto trymove;
- }
- dir[l]= d,board[p]= board[q],t-= del,p= q,l++;
- goto newlevel;
- backtrack:
- if(l> 1){
- l--;
- q= pos[l],board[p]= board[q],p= q,q= pos[l-1],t= tim[l],d= dir[l]+1;
- goto trymove;
- }
- printf(" ... no solution with %d+%d moves (%d sec)\n",
- moves&0xff,moves>>8,time(0)-timer);
- moves+= 0x101;
- }
- win:
- pos[l+1]= q;
- printf("Solution in %d+%d moves: ",moves&0xff,moves>>8);
- if(moves> 0){
- for(j= 0;j<16;j++)board[j]= start[j];
- for(k= 1;k<=l;k++){
- printf("%x",board[pos[k+1]]+1);
- board[pos[k]]= board[pos[k+1]];
- }
- printf(" (%d sec)\n",time(0)-timer);
- }
- }
复制代码
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
不知道15数码是什么。
那一堆一个字母的变量看着晕,也没有注释,看着像学生写的
如果不是特殊情况,一个函数120多行,有点太长了。。。。
复制代码11个寄存器变量?寄存器一共有几个?
没注释没子函数 一律无视
只有一个函数,而且还有这么多全局变量
一不小心
居然看到了 goto trymove;
其余的看法同3,5,6楼
空格都没有,main前没int,也缺return。
就算有注释也看得晕。
不爽
不咋地,估计是学生,搞什么ACM的,在YY中