poj~1915(bfs)
1760 단어 bfs
제목: 기점에서 종점까지 최소한 몇 걸음은 걸어야 한다.
위 양방향 검색 코드:
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int n,sx,sy,ex,ey;
int dirx[]={-2,-2,-1,-1,1,1,2,2}; // ,8
int diry[]={1,-1,2,-2,2,-2,1,-1};
typedef struct point{
int x,y;
}point;
int step[305][305]; //
int vis[305][305]; //
void bfs(){
memset(step,0,sizeof(step));
memset(vis,0,sizeof(vis));
point start,end;
start.x=sx,start.y=sy;
end.x=ex,end.y=ey;
queue <point > q;
q.push(start); //
q.push(end); //
vis[sx][sy]=1; // 1
vis[ex][ey]=2; // 2
while(!q.empty()){
point temp=q.front();
q.pop();
for(int i=0;i<8;++i){
point next=temp;
next.x+=dirx[i];
next.y+=diry[i];
if(next.x<0 || next.y<0 || next.x>=n || next.y>=n) continue; //
if(vis[next.x][next.y]==0){ //
q.push(next);
vis[next.x][next.y]=vis[temp.x][temp.y];
step[next.x][next.y]=step[temp.x][temp.y]+1;
}
else if(vis[next.x][next.y]!=vis[temp.x][temp.y]){ // ,
printf("%d
",step[next.x][next.y]+step[temp.x][temp.y]+1);
return ;
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
scanf("%d %d",&sx,&sy);
scanf("%d %d",&ex,&ey);
if(sx==ex && sy==ey) printf("0
");
else bfs();
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
말의 주법 SDUTTime Limit: 1000ms Memory limit: 65536K여기를 누르세요^_^ 4*5의 바둑판에서 말의 초기 위치 좌표(종횡) 위치는 키보드에 입력되어 말이 초기 위치의 모든 다른 주법의 총수를 되돌릴 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.