1.链接地址:
http://bailian.openjudge.cn/practice/1915
http://poj.org/problem?id=1915
2.题目:
3.思路:
4.代码:
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
typedef struct
{
int row;
int col;
//int step;
}data;
int a[][];
int colStep[]={,,-,-,,,-,-};
int rowStep[]={,,-,-,-,-,,};
bool in(int row,int col,int size)
{
if(row<||row>=size) return false;
if(col<||col>=size) return false;
return true;
}
void initArray(int size)
{
int i,j;
for(i=;i<size;i++)
{
for(j=;j<size;j++)
{
a[i][j]=-;
}
}
}
int f(int i,int j,int m,int n,int size)
{
queue<data> q;
int k;
int newRow,newCol;
data start,aData;
start.row=i;
start.col=j;
initArray(size);
//start.step=1;
a[i][j]=;
q.push(start);
while(!q.empty())
{
aData=q.front();
if(aData.row==m&&aData.col==n)
{
return a[aData.row][aData.col];
}
else
{
for(k=;k<;k++)
{
newRow=aData.row+rowStep[k];
newCol=aData.col+colStep[k];
if(in(newRow,newCol,size))
{
if(a[newRow][newCol]==-)
{
data newData;
newData.col=newCol;
newData.row=newRow;
a[newRow][newCol]=a[aData.row][aData.col]+;
q.push(newData);
}
}
}
}
q.pop();
}
return ;
}
int main()
{
int sum,k;
int i,j,m,n,size;
cin>>sum;
for(k=;k<sum;k++)
{
cin>>size>>i>>j>>m>>n;
cout<<f(i,j,m,n,size)<<endl;
}
return ;
}