Quân mã (Thuật toán BFS)
package Advance;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Solution{
static int S;
static int K;
static int M; // col
static int N; // row
static int dem;
static int min;
static MyQueue myQueue;
static int table[][];
static int[][] K_move = { { -2, -1 }, { -2, 1 }, { -1, -2 }, { -1, 2 },
{ 1, -2 }, { 1, 2 }, { 2, -1 }, { 2, 1 } };
static class Location {
int x;
int y;
public Location(int a, int b) {
x = a;
y = b;
}
}
static class MyQueue {
Location ele[];
int front;
int rear;
public MyQueue() {
front = 0;
rear = 0;
ele = new Location[10000];
}
boolean isEmpty() {
if (front == rear)
return true;
else
return false;
}
void push(Location x) {
ele[rear] = x;
rear++;
if(rear == 10000) rear = 0;
}
Location pop() {
Location a = ele[front];
front++;
if(front == 10000) front = 0;
return a;
}
}
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++) {
N = sc.nextInt(); // row
M = sc.nextInt(); // col
table = new int[N + 1][M + 1];
myQueue = new MyQueue();
int R = sc.nextInt();
int C = sc.nextInt();
table[R][C] = 1;
S = sc.nextInt();
K = sc.nextInt();
duyetDiem(new Location(R, C));
System.out.println("Case #" + test_case + "\n" + (dem -1));
}
}
private static void duyetDiem(Location u) {
myQueue.push(u);
while(!myQueue.isEmpty())
{
u = myQueue.pop();
if(u.x == S && u.y == K){
dem = table[u.x][u.y];
return;
}
for (int i = 0; i < 8; i++) {
int uX = u.x + K_move[i][0];
int uY = u.y + K_move[i][1];
if(checkLenght(uX,uY))
if (table[uX][uY] == 0) {
table[uX][uY] = table[u.x][u.y] + 1;
myQueue.push(new Location(uX, uY));
}
}
}
}
private static boolean checkLenght(int uX, int uY) {
if(uX >= 1 && uX <= N && uY>=1 && uY <= M) return true;
else
return false;
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Solution{
static int S;
static int K;
static int M; // col
static int N; // row
static int dem;
static int min;
static MyQueue myQueue;
static int table[][];
static int[][] K_move = { { -2, -1 }, { -2, 1 }, { -1, -2 }, { -1, 2 },
{ 1, -2 }, { 1, 2 }, { 2, -1 }, { 2, 1 } };
static class Location {
int x;
int y;
public Location(int a, int b) {
x = a;
y = b;
}
}
static class MyQueue {
Location ele[];
int front;
int rear;
public MyQueue() {
front = 0;
rear = 0;
ele = new Location[10000];
}
boolean isEmpty() {
if (front == rear)
return true;
else
return false;
}
void push(Location x) {
ele[rear] = x;
rear++;
if(rear == 10000) rear = 0;
}
Location pop() {
Location a = ele[front];
front++;
if(front == 10000) front = 0;
return a;
}
}
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int test_case = 1; test_case <= T; test_case++) {
N = sc.nextInt(); // row
M = sc.nextInt(); // col
table = new int[N + 1][M + 1];
myQueue = new MyQueue();
int R = sc.nextInt();
int C = sc.nextInt();
table[R][C] = 1;
S = sc.nextInt();
K = sc.nextInt();
duyetDiem(new Location(R, C));
System.out.println("Case #" + test_case + "\n" + (dem -1));
}
}
private static void duyetDiem(Location u) {
myQueue.push(u);
while(!myQueue.isEmpty())
{
u = myQueue.pop();
if(u.x == S && u.y == K){
dem = table[u.x][u.y];
return;
}
for (int i = 0; i < 8; i++) {
int uX = u.x + K_move[i][0];
int uY = u.y + K_move[i][1];
if(checkLenght(uX,uY))
if (table[uX][uY] == 0) {
table[uX][uY] = table[u.x][u.y] + 1;
myQueue.push(new Location(uX, uY));
}
}
}
}
private static boolean checkLenght(int uX, int uY) {
if(uX >= 1 && uX <= N && uY>=1 && uY <= M) return true;
else
return false;
}
}
Nhận xét
Đăng nhận xét