Title Link:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511
The main topic: In a 8*8 board, give you a starting position and an end location, but also give you a trap location, ask you from the beginning to bypass the trap to the end of the shortest distance. (You can walk up and down or you can walk diagonally)
Problem-solving ideas: can be directly with the search, can also use mathematical knowledge to solve, we have learned before, two points directly between the shortest, so when the trap is not in this line, we do not have to consider the trap, is directly max (ABS (X1-Y1), ABS (X2-Y2)) The final result,
But if the trap is between two straight lines, you only need Max (ABS (X1-Y1), ABS (X2-Y2)) +1, it is important to note that if the line formed at three points is parallel to the x-axis or y-axis, it is also a trap to ignore.
AC Code:
#include <cstdio>#include<iostream>#include<cstdlib>using namespacestd;intMain () {intX1,y1,x2,y2,x3,y3,ca=1; while(SCANF ("%d%d%d%d%d%d", &x1,&y1,&x2,&y2,&x3,&y3)! =EOF) { intdis=0; if(((Y3-Y2==X3-X2) && (y3-y1==x3-x1) | | ((y3-y2==x2-x3) && (y3-y1==x1-x3))) && (X3<max (x1,x2) &&x3>min (x1,x2)) Dis=max (ABS (X2-X1), ABS (Y2-Y1)) +1; ElseDis=max (ABS (X1-X2), ABS (y1-y2)); printf ("Case %d:%d\n", ca++, dis); } return 0;}
CSU 1511 mutilated chessboard tenth session of Hunan Province game problem