Go to diff to previous submission
#include <iostream> #include <queue> #include <limits.h> #include <stdio.h> using namespace std; int ** ARRAY; int SIZEX; int SIZEY; int leafx, leafy; bool ISEND; class item { public: item(int x,int y, int count); int X, Y, count; }; item::item(int x,int y, int count) { this->X=x; this->Y=y; this->count=count; } queue <item> fronta; void insert(int X, int Y, int count); void jump (item mItem) { if(mItem.X==(leafx-1) && mItem.Y==(leafy-1)) { ISEND=true; cout << mItem.count << endl; return; } insert(mItem.X+1,mItem.Y+2, mItem.count+1); insert(mItem.X+2,mItem.Y+1, mItem.count+1); insert(mItem.X+2,mItem.Y-1, mItem.count+1); insert(mItem.X+1,mItem.Y-2, mItem.count+1); insert(mItem.X-1,mItem.Y-2, mItem.count+1); insert(mItem.X-2,mItem.Y-1, mItem.count+1); insert(mItem.X-2,mItem.Y+1, mItem.count+1); insert(mItem.X-1,mItem.Y+2, mItem.count+1); } void insert(int X, int Y, int count) { if(X<0 || X>=SIZEX || Y<0 || Y>=SIZEY || count >= ARRAY[X][Y]) return; ARRAY[X][Y]=count; fronta.push(item(X, Y, count)); } int main() { int grex, grey; while(scanf("%d %d %d %d %d %d\n", &SIZEX, &SIZEY, &grex, &grey, &leafx, &leafy)==6) { ARRAY=new int *[SIZEX]; for (int i=0; i<SIZEX; i++) { ARRAY[i]=new int [SIZEY]; for (int j=0; j<SIZEY; j++) { ARRAY[i][j]=INT_MAX; } } ISEND=false; insert(grex-1, grey-1, 0); do { jump(fronta.front()); fronta.pop(); } while(!fronta.empty() && !ISEND); if(fronta.empty() && !ISEND) printf("impossible\n"); while(!fronta.empty()) { fronta.pop(); } } return 0; }
--- c4.s653.cteam009.grasshop.cpp.0.grasshop.cpp +++ c4.s980.cteam009.grasshop.cpp.0.grasshop5.cpp @@ -1,53 +1,63 @@ -#include <stdio.h> -#include <limits.h> #include <iostream> +#include <queue> +#include <limits.h> +#include <stdio.h> + +using namespace std; int ** ARRAY; int SIZEX; int SIZEY; -int MINCOUNT; int leafx, leafy; -/* -int min(int & X, int & Y) -{ - if(X>Y) return Y; - else return X; -} -*/ -void jump(int X, int Y, int count) -{ -if(X==(leafx-1) && Y==(leafy-1)) +bool ISEND; + +class item { + public: + item(int x,int y, int count); + int X, Y, count; +}; + +item::item(int x,int y, int count) { - if(count < MINCOUNT) - { - ARRAY[X][Y]=count; - MINCOUNT=count; - } - return; +this->X=x; +this->Y=y; +this->count=count; } -if(X<0 || X>=SIZEX || Y<0 || Y>=SIZEY || count >= ARRAY[X][Y] || count>=MINCOUNT) -return; +queue <item> fronta; -ARRAY[X][Y]=count; +void insert(int X, int Y, int count); -jump(X+1,Y+2, count+1); -jump(X+2,Y+1, count+1); -jump(X+2,Y-1, count+1); -jump(X+1,Y-2, count+1); -jump(X-1,Y-2, count+1); -jump(X-2,Y-1, count+1); -jump(X-2,Y+1, count+1); -jump(X-1,Y+2, count+1); +void jump (item mItem) { + if(mItem.X==(leafx-1) && mItem.Y==(leafy-1)) + { + ISEND=true; + cout << mItem.count << endl; + return; + } + insert(mItem.X+1,mItem.Y+2, mItem.count+1); + insert(mItem.X+2,mItem.Y+1, mItem.count+1); + insert(mItem.X+2,mItem.Y-1, mItem.count+1); + insert(mItem.X+1,mItem.Y-2, mItem.count+1); + insert(mItem.X-1,mItem.Y-2, mItem.count+1); + insert(mItem.X-2,mItem.Y-1, mItem.count+1); + insert(mItem.X-2,mItem.Y+1, mItem.count+1); + insert(mItem.X-1,mItem.Y+2, mItem.count+1); } -int main() +void insert(int X, int Y, int count) { + if(X<0 || X>=SIZEX || Y<0 || Y>=SIZEY || count >= ARRAY[X][Y]) + return; + ARRAY[X][Y]=count; + fronta.push(item(X, Y, count)); +} + +int main() { int grex, grey; while(scanf("%d %d %d %d %d %d\n", &SIZEX, &SIZEY, &grex, &grey, &leafx, &leafy)==6) { - MINCOUNT=INT_MAX; ARRAY=new int *[SIZEX]; for (int i=0; i<SIZEX; i++) @@ -59,10 +69,22 @@ } } - jump(grex-1,grey-1,0); - if(ARRAY[leafx-1][leafy-1]==INT_MAX) - printf("impossible\n"); - else printf("%d\n", ARRAY[leafx-1][leafy-1]); + ISEND=false; + insert(grex-1, grey-1, 0); + do + { + jump(fronta.front()); + fronta.pop(); + } while(!fronta.empty() && !ISEND); + if(fronta.empty() && !ISEND) printf("impossible\n"); + while(!fronta.empty()) + { + fronta.pop(); + } } -return 0; + return 0; } + + + +