Wannafly挑战赛17 A走格子

#include <bits/stdc++.h>
using namespace std;
struct node{
int x,y;
int step;
};
int dir[][2]={1,0, 0,1, -1,0, 0,-1};
int n,m;
int ansx,ansy;
int book[1001][1001];
void bfs(int x,int y,int step)
{
int i=0;
  queue<node> p;
  node q;
  q.x=x; q.y=y; q.step=step; 
  p.push(q);
    while(!p.empty())
 {
  node z=p.front();
  if(z.step==m)
 {ansx=z.x; ansy=z.y; return;
 }
  p.pop();
int tx=z.x+dir[i][0];
int ty=z.y+dir[i][1];
while(!(tx<=n&&tx>0&&ty>0&&ty<=n&&!book[tx][ty]))
      {
             i++; i=i%4;
      tx=z.x+dir[i][0];
            ty=z.y+dir[i][1];
  }
  book[tx][ty]=1;
    node tp; tp.x=tx; tp.y=ty;  tp.step=z.step+1;
    p.push(tp);
      }
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
memset(book,0,sizeof(book));
        book[1][1]=1;
bfs(1,1,0);
printf("%d %d\n",ansx,ansy);
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41603898/article/details/80627369
今日推荐