马踏飞燕(续)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值