http://www.acmicpc.sdnu.edu.cn/problem/show/1027
泪奔。第一次做费时费力还不对。第二次做,问了下同学,整理了下思路。一边AC。好感动。
原来想用memset给整数数值 赋初值1,可是测试错了,百度了下,memset貌似不能赋除了0的数。
还有pair的使用,好像比struct方便一些呢。
这次主要优化了这100步怎么计算。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int ditu[2005][2005];
int main()
{
int xx[]={-1,1,-2,2,-2,2,-1,1};
int yy[]={-2,-2,-1,-1,1,1,2,2};
memset(ditu,0,sizeof(ditu));
int max,may,yanx,yany;
bool flag=false;
cin>>max>>may>>yanx>>yany;
typedef pair<int ,int>pa;
queue<pa> que;
que.push(pa(max,may));
pa temptemp;
while(!que.empty())
{
pa temppa=que.front();
que.pop();
if (temppa.first==yanx&&temppa.second==yany)
{
flag=true;
break;
}
if (ditu[temppa.first][temppa.second]<=199)
{
for (int i = 0 ; i < 8; ++i)
{
if(temppa.first+xx[i]>0&&temppa.first+xx[i]<2000&&temppa.second+yy[i]>0&&temppa.second+yy[i]<2000)
{
temptemp.first=temppa.first+xx[i];
temptemp.second=temppa.second+yy[i];
ditu[temptemp.first][temptemp.second]+=ditu[temppa.first][temppa.second]+1;
que.push(temptemp);
}
}
}
}
if (flag)
{
cout<<"Y"<<endl;
}else
{
cout<<"N"<<endl;
}
return 0;
}