NYOJ 58 최소 걸음 수
1802 단어 OJ
고전적 인 미로 문제,,, BFS 로 궁 거:
#include <iostream>
#include<queue>
#include<utility>
#include<cstdio>
#define N 9
using namespace std;
const int INF=1000000;
typedef pair<int,int> pt;
int dx[5]={1,0,-1,0};
int dy[5]={0,1,0,-1};
int sx,sy;
int gx,gy;
int d[10][10];
int maze[9][9]=
{
{1,1,1,1,1,1,1,1,1}
,{1,0,0,1,0,0,1,0,1}
,{1,0,0,1,1,0,0,0,1}
,{1,0,1,0,1,1,0,1,1}
,{1,0,0,0,0,1,0,0,1}
,{1,1,0,1,0,1,0,0,1}
,{1,1,0,1,0,1,0,0,1}
,{1,1,0,1,0,0,0,0,1}
,{1,1,1,1,1,1,1,1,1}
};
int bfs()
{
int i,j;
int nx,ny;
queue <pt> que;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
d[i][j]=INF;
que.push(pt(sx,sy));
d[sx][sy]=0;
while(que.size())
{
pt p=que.front();
que.pop();
if(p.first==gx&&p.second==gy) //
break;
for(i=0; i<4; i++)
{
nx=p.first+dx[i];
ny=p.second+dy[i];
if(nx>=0&&ny>=0&&nx<N&&ny<N&&d[nx][ny]==INF&&maze[nx][ny]==0)
{
que.push(pt(nx,ny));
d[nx][ny]=d[p.first][p.second]+1;
}
}
}
return d[gx][gy];
}
int main()
{
freopen("1.txt","r",stdin);
int t;
cin>>t;
int ans;
while(t--)
{
cin>>sx>>sy>>gx>>gy;
ans=bfs();
cout<<ans<<endl;
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
hdoj1006--Tick and TickA hand is happy if it is at least D degrees from any of the rest. Input The input is terminated with a D of -1. For each...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.