poj 1915 Knight Moves

/* Name:poj 1915 Knight Moves Author:Unimen Date: 1/05/2011 22:13 Description:BFS */ /* 解题报告:广搜、最短路径 */ #include <iostream> #include <queue> #include <cstring> using namespace std; const int MAXN = 310; int foot[8][2] = {{1,2}, {2,1}, {2,-1}, {1,-2}, {-1,-2}, {-2,-1}, {-2,1}, {-1, 2}}; struct Knight { int x, y; int step; }; Knight start, finish; int visited[MAXN][MAXN]; int nCase, nBoard; void bfs() { queue<Knight> que; que.push(start); visited[start.x][start.y] = 1; while(!que.empty()) { Knight temp1 = que.front(); que.pop(); if(temp1.x==finish.x && temp1.y==finish.y) { cout<<temp1.step<<endl; } for(int i=0; i<8; ++i) { Knight temp2; if(!visited[temp1.x+foot[i][0]][temp1.y+foot[i][1]] && temp1.x+foot[i][0]>=0&&temp1.x+foot[i][0]<nBoard && temp1.y+foot[i][1]>=0&&temp1.y+foot[i][1]<nBoard) { visited[temp1.x+foot[i][0]][temp1.y+foot[i][1]] = 1; temp2.x = temp1.x+foot[i][0]; temp2.y = temp1.y+foot[i][1]; temp2.step = temp1.step + 1; que.push(temp2); } } } } int main() { int i; while(cin>>nCase) { for(i=0; i<nCase; ++i) { cin>>nBoard; cin>>start.x>>start.y>>finish.x>>finish.y; start.step = 0; memset(visited, 0, sizeof(visited)); bfs(); } } return 0; }

你可能感兴趣的:(Date,struct)