博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
zoj 1091 Knight Moves (BFS)(情况用循环控制,值得学习啊)
阅读量:2221 次
发布时间:2019-05-08

本文共 6595 字,大约阅读时间需要 21 分钟。

刚开始看不懂题,后来查了查才明白这颗棋子是走“日”字的。。。。

接着,那就会出现8种情况。。。无语,不过,有了2165的模板,我居然慢悠悠的把这八种情况罗列出来并且AC了。。

        上一篇2165就想把这种相同部分写成函数来调用,可是又发现,相同部分里,if语句中有break;这怎么写到函数里啊?????如果仅仅是把else部分(就两句话)写成函数,感觉又没那个必要了。。。纠结中。。

#include
#include
typedef struct { int s1[5000]; int s2[5000];}queue;queue que;int head,rear;void In(int x,int y){ que.s1[rear]=x; que.s2[rear]=y; rear++;}void Out(int *x,int *y){ *x=que.s1[head]; *y=que.s2[head]; head++;}int isEmpty(){ if(head==rear) return 1; else return 0;} int main(){ int w,h,i,j,x1,x2,y1,y2,count[9][9],b[9][9],p1,p2; char temp,t1,t2; while(scanf("%c%d %c%d",&t1,&y1,&t2,&y2)!=EOF) { x1=t1-'a'+1; x2=t2-'a'+1; for(i=0;i<9;i++) for(j=0;j<9;j++) { b[i][j]=0; count[i][j]=0; } head=rear=0; In(x1,y1); b[x1][y1]=1; while(!isEmpty()) { Out(&p1,&p2); if(p1==x2&&p2==y2) { printf("To get from %c%d to %c%d takes %d knight moves.\n",t1,y1,t2,y2,count[p1][p2]); break; } else { i=p1-2,j=p2+1; if(i>=1&&j<=8&&b[i][j]==0) { count[i][j]=count[p1][p2]+1; if(i==x2&&j==y2) { printf("To get from %c%d to %c%d takes %d knight moves.\n",t1,y1,t2,y2,count[i][j]); break; } else { In(i,j); b[i][j]=1; } } i=p1-1,j=p2+2; if(i>=1&&j<=8&&b[i][j]==0) { count[i][j]=count[p1][p2]+1; if(i==x2&&j==y2) { printf("To get from %c%d to %c%d takes %d knight moves.\n",t1,y1,t2,y2,count[i][j]); break; } else { In(i,j); b[i][j]=1; } } i=p1+1,j=p2+2; if(i<=8&&j<=8&&b[i][j]==0) { count[i][j]=count[p1][p2]+1; if(i==x2&&j==y2) { printf("To get from %c%d to %c%d takes %d knight moves.\n",t1,y1,t2,y2,count[i][j]); break; } else { In(i,j); b[i][j]=1; } } i=p1+2,j=p2+1; if(i<=8&&j<=8&&b[i][j]==0) { count[i][j]=count[p1][p2]+1; if(i==x2&&j==y2) { printf("To get from %c%d to %c%d takes %d knight moves.\n",t1,y1,t2,y2,count[i][j]); break; } else { In(i,j); b[i][j]=1; } } i=p1+2,j=p2-1; if(i<=8&&j>=1&&b[i][j]==0) { count[i][j]=count[p1][p2]+1; if(i==x2&&j==y2) { printf("To get from %c%d to %c%d takes %d knight moves.\n",t1,y1,t2,y2,count[i][j]); break; } else { In(i,j); b[i][j]=1; } } i=p1+1,j=p2-2; if(i<=8&&j>=1&&b[i][j]==0) { count[i][j]=count[p1][p2]+1; if(i==x2&&j==y2) { printf("To get from %c%d to %c%d takes %d knight moves.\n",t1,y1,t2,y2,count[i][j]); break; } else { In(i,j); b[i][j]=1; } } i=p1-1,j=p2-2; if(i>=1&&j>=1&&b[i][j]==0) { count[i][j]=count[p1][p2]+1; if(i==x2&&j==y2) { printf("To get from %c%d to %c%d takes %d knight moves.\n",t1,y1,t2,y2,count[i][j]); break; } else { In(i,j); b[i][j]=1; } } i=p1-2,j=p2-1; if(i>=1&&j>=1&&b[i][j]==0) { count[i][j]=count[p1][p2]+1; if(i==x2&&j==y2) { printf("To get from %c%d to %c%d takes %d knight moves.\n",t1,y1,t2,y2,count[i][j]); break; } else { In(i,j); b[i][j]=1; } } } } getchar(); } return 0;}
我晕啊!!!!!!!弄个循环就把这8种情况搞定了!我靠lai!!!!这么先进,这么先进骂人。。。。。。忍了。。。。学习一下吧。。。。主要是那个循环控制8种情况,太纠结了。。。
#include 
#include
#include
int state[9][9]; int count[9][9]; int Queue[100000]; int step[8][2] = {1,2, 1,-2, -1,2, -1,-2, 2,1, 2,-1, -2,1, -2,-1}; int head,tail; int push(int x) { Queue[head++] = x; } int pop(void) { return Queue[tail++]; } int Qempty(void) { if( head == tail ) return 1; return 0; } void init(void) { head = 0; tail = 0; memset( state,0,sizeof(state) ); memset( count,0,sizeof(count) ); memset( Queue,0,sizeof(Queue) ); } int main(void) { int a,b,x,y,temp,tempa,tempx,ta,tx,i; char ch1,ch2,n; while( scanf("%c%d %c%d%c",&ch1,&x,&ch2,&y,&n)!=EOF ) //这点很纠结,因为有个回车,不再输入一个回车的话,会错 { init(); a = ch1 - 'a' + 1; b = ch2 - 'a' + 1; push(a); push(x); state[a][x] = 1; while( !Qempty() ) { tempa = pop(); tempx = pop(); if( tempa == b && tempx == y) break; for(i=0; i<8; i++)//8个方向,用循环一一调用,这点很值得学习! { ta = tempa + step[i][0]; tx = tempx + step[i][1]; if(state[ta][tx] == 0 && ta>=1 && ta<=8 && tx>=1 && tx<=8 ) { push(ta); push(tx); state[ta][tx] = 1; count[ta][tx] = count[tempa][tempx] + 1; } } } printf("To get from %c%d to %c%d takes %d knight moves./n",ch1,x,ch2,y,count[tempa][tempx]); } system("pause"); return 0; }

你可能感兴趣的文章
Oracle PL/SQL语言初级教程之异常处理
查看>>
Oracle PL/SQL语言初级教程之游标
查看>>
Oracle PL/SQL语言初级教程之操作和控制语言
查看>>
Oracle PL/SQL语言初级教程之过程和函数
查看>>
Oracle PL/SQL语言初级教程之表和视图
查看>>
Oracle PL/SQL语言初级教程之完整性约束
查看>>
PL/SQL学习笔记
查看>>
如何分析SQL语句
查看>>
结构化查询语言(SQL)原理
查看>>
SQL教程之嵌套SELECT语句
查看>>
日本語の記号の読み方
查看>>
计算机英语编程中一些单词
查看>>
JavaScript 经典例子
查看>>
判断数据的JS代码
查看>>
js按键事件说明
查看>>
AJAX 初次体验!推荐刚学看这个满好的!
查看>>
AJAX 设计制作 在公司弄的 非得要做出这个养的 真晕!
查看>>
Linux 查看文件大小
查看>>
Java并发编程:线程池的使用
查看>>
redis单机及其集群的搭建
查看>>