hdu 1372 本来开了个无用的数组,结果无数次ML

来源:互联网 发布:超星软件 编辑:程序博客网 时间:2024/04/27 14:04
#include <iostream>#include <cstdio>#include <queue>#include <cstdlib>#include <string>using namespace std;int dist[8][2] = {{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};int x,y,xx,yy;int a,b;struct P{int a,b,len;};queue<P> G;P temp={0,0,0};P ri={0,0,0};int search(int x,int y){   P dui={x,y,0};if(x==xx&&y==yy)return 0;while(!G.empty())G.pop();G.push(dui);while(!G.empty()){temp = G.front();G.pop();//if(temp.a==xx&&temp.b==yy) return temp.len+1;for(int i=0;i<8;i++){ri.a = temp.a+dist[i][0];ri.b= temp.b+dist[i][1];ri.len = temp.len + 1;if(ri.a==xx&&ri.b==yy)return ri.len;if(ri.a>=0&&ri.b>=0&&ri.a<8&&ri.b<8){G.push(ri);}}}return 0;}int main(){string a,b;while(cin>>a>>b){x = a[1]-'0'-1;y=a[0]-'a';xx = b[1]-'0'-1;yy = b[0]-'a'; cout<<"To get from "<<a<<" to "<<b<<" takes "<<search(x,y)<<" knight moves."<<endl;}   return 0;}


原创粉丝点击