Be sure to pay attention to the input and output later
For example, the least-given coordinate of the topic starts from (
Then decide if the array is out of bounds.
We need to pay special attention.
I know the truth.
And then because of this problem and when you subtract 1 from the input coordinates,
I counted the m,n in the accident.
And then you blew me up for one hours.
Mom, a chicken.
That's the first way to play rank today.
I thought about it when I got the title.
But did not consider the column
Fried 2h.
I've got a dog's heart in my mother's Day.
#include <stdio.h> #include <queue> #include <string.h> #include <stdlib.h>//#include < Iostream>using namespace Std;int n,m,sx,sy,ex,ey;int map[21][21];int dir[8][2]={{-1,2},{1,2},//right jump 0,1 { 2,1},{2,-1},//jump up 2,3 {1,-2},{-1,-2},//left jump 4,5 { -2,-1},{-2,1}//jump down 6,7};struct node{int x,y,step;}; int check (int x,int y) {if (x>=0&&x<m&&y>=0&&y<n&&map[x][y]==0) return 1; else return 0;} int BFS (int x,int y) {Node temp, next; Queue<node> Queue; Temp.x=x; Temp.y=y; Temp.step=0; Map[x][y]=1; Queue.push (temp); while (! Queue.empty ()) {Temp=queue.front (); Queue.pop (); if (Temp.x==ex&&temp.y==ey) {return temp.step; } else if (Temp.x==ex&&temp.y+1==ey)//up {for (int i=0; i<8;i++) {if (i==2| | i==3) Continue; NEXT.X=TEMP.X+DIR[I][0]; NEXT.Y=TEMP.Y+DIR[I][1]; if (check (NEXT.X,NEXT.Y) ==1) {next.step=temp.step+1; Map[next.x][next.y]=1; if (Next.x==ex&&next.y==ey) return next.step; Queue.push (next); }}} and else if (Temp.x==ex&&temp.y-1==ey)//down {for (int i=0; i<8;i++) {if (i==6| | I==7) continue; NEXT.X=TEMP.X+DIR[I][0]; NEXT.Y=TEMP.Y+DIR[I][1]; if (check (NEXT.X,NEXT.Y) ==1) {next.step=temp.step+1; Map[next.x][next.y]=1; if (Next.x==ex&&next.y==ey) return next.step; Queue.push (next); }}} and else if (Temp.x+1==ex&&temp.y==ey)//right {for (int i=0; i<8;i++) {if (i==0| | I==1) continue; NEXT.X=TEMP.X+DIR[I][0]; NEXT.Y=TEMP.Y+DIR[I][1]; if (check (NEXT.X,NEXT.Y) ==1) {next.step=temp.step+1; Map[next.x][next.y]=1; if (Next.x==ex&&next.y==ey) return next.step; Queue.push (next); }}} and else if (Temp.x-1==ex&&temp.y==ey)//left {for (int i=0; i<8;i++) {if (i==4| | I==5) continue; NEXT.X=TEMP.X+DIR[I][0]; NEXT.Y=TEMP.Y+DIR[I][1]; if (check (NEXT.X,NEXT.Y) ==1) {next.step=temp.step+1; Map[next.x][next.y]=1; if (Next.x==ex&&next.y==ey) return next.step; Queue.push (next); }}}} else for (int i=0; i<8;i++) {Next.x=teMP.X+DIR[I][0]; NEXT.Y=TEMP.Y+DIR[I][1]; if (check (NEXT.X,NEXT.Y) ==1) {next.step=temp.step+1; Map[next.x][next.y]=1; Queue.push (next); }}} return-1;} int main () {//int ex,ey,sx,sy; while (scanf ("%d%d%d%d%d", &m,&n,&sx,&sy,&ex,&ey)!=eof) {memset (map,0,sizeof (MAP)) ; sx--;sy--;ex--;ey--; printf ("%d\n", BFS (Sx,sy)); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
COJ 1224 ACM Group's Wacky Chess