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 |
Runtime error! please helpI try ti use the BFS, and got RE, i had checked carefully, but i don't know why it got RE. Please help me. PS: would you please write in E, plz. Here my code: #include <cstdlib> #include <iostream> #include <fstream> using namespace std; const int cmaxr = 1000; const int cmaxn = 15000; bool ch[cmaxn]; int a[cmaxr][6]; int bd[cmaxr]; void init(){ memset(ch,false,sizeof(ch)); bd[0] = a[0][0] = a[0][1] = a[0][2] = a[0][3] = a[0][4] = a[0][5] = 1; int r = 0; while (bd[r] < 11000){ r++; bd[r] = a[r-1][5]+1; a[r][0] = bd[r] + r-1; ch[a[r][0]] = true; a[r][1] = a[r][0] + r; ch[a[r][1]] = true; a[r][2] = a[r][1] + r; ch[a[r][2]] = true; a[r][3] = a[r][2] + r; ch[a[r][3]] = true; a[r][4] = a[r][3] + r; ch[a[r][4]] = true; a[r][5] = a[r][4] + r; ch[a[r][5]] = true; // cout <<r <<" bd = " <<bd[r] <<" a = " <<a[r][0] <<" " <<a[r][1] <<" " <<a[r][2] <<" " // <<a[r][3] <<" " <<a[r][4] <<" " <<a[r][5] <<" " <<endl; } } int findR(int x){ int r = 0; while (a[r][5] < x) r++; return r; } int findC(int x, int r){ int c = 0; while (a[r][c] < x) c++; return c; } int next(int x,int r){ if (x == a[r][5]) return bd[r]; else return x+1; } int pre(int x,int r){ if (x == bd[r]) return a[r][5]; else return x-1; } //void findAdj(int x,int[] &y1,int &y2,int &y3,int &y4,int &y5,int &y6){ int adjX[6]; void findAdj(int x){ if (x==1){ adjX[0] = 2; adjX[1] = 3; adjX[2] = 4; adjX[3] = 5; adjX[4] = 6; adjX[5] = 7; return; } int xr = findR(x); int xc = findC(x,xr); if (ch[x]){ //goc adjX[0] = pre (x,xr); adjX[1] = next(x,xr); adjX[2] = a[xr-1][xc]; adjX[3] = a[xr+1][xc]; adjX[4] = pre (adjX[3],xr+1); adjX[5] = next(adjX[3],xr+1); } else { //canh int delta = a[xr][xc] - x; adjX[0] = pre(x,xr); adjX[1] = next(x,xr); adjX[2] = a[xr-1][xc]; for (int i = 1; i<=delta; i++) adjX[2] = pre (adjX[2],xr-1); adjX[3] = next(adjX[2],xr-1); adjX[4] = a[xr+1][xc]; for (int i = 1; i<=delta; i++) adjX[4] = pre (adjX[4],xr+1); adjX[5] = pre (adjX[4],xr+1); } } int q[cmaxn]; int kc[cmaxn]; int qf,ql; bool visit[cmaxn]; int bfs(int d, int c){ qf = 1; ql = 0; memset(visit,false,sizeof(visit)); q[++ql] = d; kc[ql] = 0; visit[d] = false; int x,kcx; while (qf<=ql){ kcx = kc[qf]; x = q[qf++]; findAdj(x); for (int i = 0; i<6; i++){ if (adjX[i] <= 10000) if (!visit[adjX[i]]){ if (adjX[i] == c) return kcx+1; visit[adjX[i]]; q[++ql] = adjX[i]; kc[ql] = kcx+1; } } } } int main(int argc, char *argv[]) { init(); //ifstream cin("bees.in"); //ofstream cout("bees.out"); int d,c; //cout <<" pre = " <<pre(3,findR(3)) <<" " <<next(3,findR(3)) <<endl; //cout <<" pre = " <<pre(8,findR(8)) <<" " <<next(8,findR(8)) <<endl; //x = 20; //findAdj(x); //cout <<" x = " <<x <<"-->" <<adjX[0] <<" " <<adjX[1] <<" " <<adjX[2] <<" " <<adjX[3] <<" " <<adjX[4] <<" " <<adjX[5] <<endl; //system("PAUSE"); // while (cin >>x){ // //int y[6]; // findAdj(x); // cout <<" x = " <<x <<"-->" <<adjX[0] <<" " <<adjX[1] <<" " <<adjX[2] <<" " <<adjX[3] <<" " <<adjX[4] <<" " <<adjX[5] <<endl; // } cin >>d >>c; while ((d >0)&&(c>0)){ cout <<"The distance between cells " <<d <<" and " <<c <<" is " <<bfs(d,c) <<"." <<endl; cin >>d >>c; } //cout.close(); //system("PAUSE"); return EXIT_SUCCESS; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator