| ||||||||||
| 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 | |||||||||
单向搜过了,17ms,双向却WA了,纳闷。。。#include <iostream>
#include <memory>
//#include <fstream>
using namespace std;
int chess[8][8],used[8][8];
int stx,sty,tgx,tgy;
int around[8][2]={{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1}};
int open1[100][2],head1,tail1;
int open2[100][2],head2,tail2;
int main()
{
int length;
char tmp1,tmp2;
// ifstream cin("data.txt");
while (cin>>tmp1>>sty>>tmp2>>tgy)
{
length=0;
stx=tmp1-'a';
tgx=tmp2-'a';
if(stx!=tgx||sty!=tgy)
{
memset(chess,0,sizeof(chess));
memset(used,0,sizeof(chess));
head1=head2=-1;
tail1=tail2=0;
open1[0][0]=stx;
open1[0][1]=--sty;
open2[0][0]=tgx;
open2[0][1]=--tgy;
used[stx][sty]=1;
used[tgx][tgy]=-1;
while (head1!=tail1&&head2!=tail2)
{
if(tail1<tail2)
{
head1++;
int aroundx,aroundy;
int x=open1[head1][0];
int y=open1[head1][1];
for (int i=0;i<8;i++)
{
aroundx=x+around[i][0];
aroundy=y+around[i][1];
if(aroundx>=0&&aroundx<8&&aroundy>=0&&aroundy<8&&used[aroundx][aroundy]!=1)
if (used[aroundx][aroundy]==-1)
{
length=chess[x][y]+1-chess[aroundx][aroundy];
break;
}
else
{
tail1++;
open1[tail1][0]=aroundx;
open1[tail1][1]=aroundy;
chess[aroundx][aroundy]=chess[x][y]+1;
used[aroundx][aroundy]=1;
}
}
}
else
{
head2++;
int aroundx,aroundy;
int x=open2[head2][0];
int y=open2[head2][1];
for (int i=0;i<8;i++)
{
aroundx=x+around[i][0];
aroundy=y+around[i][1];
if(aroundx>=0&&aroundx<8&&aroundy>=0&&aroundy<8&&used[aroundx][aroundy]!=-1)
if (used[aroundx][aroundy]==1)
{
length=chess[aroundx][aroundy]+1-chess[x][y];
break;
}
else
{
tail2++;
open2[tail2][0]=aroundx;
open2[tail2][1]=aroundy;
chess[aroundx][aroundy]=chess[x][y]-1;
used[aroundx][aroundy]=-1;
}
}
}
if (length)
break;
}
}
cout<<"To get from "<<char('a'+stx)<<sty+1<<" to "<<char('a'+tgx)<<tgy+1<<" takes "<<length<<" knight moves.\n";
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator