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 |
谁来告诉我 我哪里错了 在vc过了啊#include<iostream> #include<queue> #include<string> using namespace std; int move[8][2] = {{1, 2}, {1, -2}, {-1, 2}, {-1, -2}, {2, 1}, {2, -1}, {-2, 1}, {-2, -1}}; int tour(); struct chess{int x,y,step;}; chess m,n; string str0; string str1; int visit[8][8]; int step; int main() { while(!cin.eof()) { cin>>str0; cin>>str1; for(int i=0;i<8;i++) { for(int j=0;j<8;j++) visit[i][j]=0; } m.x=str0[0]-'a'; m.y=str0[1]-'1'; n.x=str1[0]-'a'; n.y=str1[1]-'1'; m.step=0; visit[m.x][m.y]=1; if(tour()) cout<<"To get from "<<str0<<" to "<<str1<<" takes "<<step<<" knight moves."<<endl; } return 0; } int tour() { chess i,j; queue<chess> q; q.push(m); while(!q.empty()) { i=q.front(); if(i.x==n.x&&i.y==n.y) { step=i.step; return 1; } q.pop(); for(int k=0;k<8;k++) { j.x=i.x+move[k][0]; j.y=i.y+move[k][1]; if(j.x>=0&&j.y<8&&j.y>=0&&j.x<8&&visit[j.x][j.y]==0) { j.step=i.step+1; visit[j.x][j.y]=1; q.push(j); } } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator