?? poj1915 2.txt
字號:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct queue
{
int x,y;
int ste;
struct queue *next;
};
struct root
{
struct queue *pre;
struct queue *aft;
}root;
int t;
int n;
int stx,sty,enx,eny;
int flag,fail ;
int ans;
int d[8][2] = {{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1},{-2,1},{-1,2}};
int mark[302][302];
void solve();
void bfs(int x,int y,int ste);
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d%d",&n,&stx,&sty,&enx,&eny);
solve();
}
return 0;
}
void solve()
{
struct queue *p;
flag = 0;
fail = 0;
memset(mark,0,sizeof(mark));
p = (struct queue *)malloc(sizeof(struct queue));
p->x = stx;
p->y = sty;
p->ste = 0;
p->next = NULL;
root.pre = p;
root.aft = p;
bfs(stx,sty,0);
printf("%d\n",ans);
}
void bfs(int x,int y,int ste)
{
int i;
struct queue *p;
if(flag || fail)
return ;
if(x == enx && y == eny)
{
ans = ste;
flag = 1;
return ;
}
for(i = 0;i < 8; i++)
{
if( (x+d[i][0]>=0 && x+d[i][0] <n) && (y+d[i][1]>=0 && y+d[i][1]<n) && mark[x+d[i][0]][y+d[i][1]] == 0)
{
p = (struct queue *)malloc(sizeof(struct queue));
p->x = x+d[i][0];
p->y = y+d[i][1];
p->ste = ste+1;
p->next = NULL;
root.aft->next = p;
root.aft = p;
mark[x+d[i][0]][y+d[i][1]] = 1;
}
}
p = root.pre;
root.pre = p->next;
p->next = NULL;
free(p);
if(root.pre == NULL)
{
fail = 1;
return ;
}
bfs(root.pre->x,root.pre->y,root.pre->ste);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -