Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 33

bài 1 :Mình gửi trước 1 bài.. Mình gửi dần nhé..

Vì mình chưa viết kịp đề bài

0
/\
1 2
/\\
345
\
6
If the given level is larger than the maximum level of the tree, then print
"No such level" (without the quotes).
Sample Input:
Test 1: 2 (0 (1 (3 () ()) (4 ()())) (2 ()(5 ()(6 ()()))))
Test 2: 3 (0 (1 (3 () ()) (4 ()())) (2 ()(5 ()(6 ()()))))
Test 3: 10 (0 (1 (3 () ()) (4 ()())) (2 ()(5 ()(6 ()()))))
Sample Output:
In ra tổng tại một nút K nào đó (VD: K=2 thì in ra tổng tại nút 2)
Test 1: 12
Test 2: 6
Test 3: -1(nếu k không tồn tại tại nút đó in ra -1)

Thuật toán:

+cắt chuỗi lấy level cần tính.

+Duyệt chuỗi còn lại.

-Nếu gặp “(“ tăng bậc

-Nếu gặp “)” giảm bậc

-Nếu gặp số thì add vào data[bac]=data[bac]+ Số


import java.util.Scanner;

public class Main2 {


// DONE
public static void main(String[] args) {
Scanner mScanner = new Scanner(System.in);
int T = Integer.parseInt(mScanner.nextLine());
String s;
for (int i = 0; i < T; i++) {
s = mScanner.nextLine();
check(s);
}
}

public static void check(String s) {


// String s = "2 (0 (1 (33 () ()) (44 ()())) (2 ()(55 ()(6 ()
()))))";
int data[] = new int[1000];
s = s.replaceAll(" ", ""); //
String s1 = s.substring(0, s.indexOf("(")); //
String s2 = s.substring(s.indexOf("(")); //

int b = Integer.parseInt(s1); //
char[] a = s2.toCharArray();
int leng = a.length;
int bac = -1;
for (int i = 0; i < leng; i++) {
if (a[i] == '(') {
bac++;
} else if (a[i] == ')') {
bac--;
} else {
int valueI = Integer.parseInt(a[i] + "");
data[bac] = data[bac] + valueI;
}
}
if (b > 0 && data[b] == 0) {
System.out.println("" + -1);
} else {
System.out.println("" + data[b]);
}
}
}
Bài 2:
//3
//(a+(b*c))
//((a+b)*(z+x))
//((a+t)*((b+(a+c))^(c+d)))
//
//Output:
//abc*+
//ab+zx+*
//at+bac++cd+^*

Thuật toán:

Duyệt tư 1 đến hết:

Nếu là chữ cái(isLetter) thì in chữ ra

Nếu là dấu “(“ thì push vào stack

Nếu là dấu “)” thì lấy phần từ đầu in bằng method peek. Sau đó dung pop để lấy
ra khỏi stack
public class Main {
public static void main(String[] args) {
////DONE
Scanner mScanner = new Scanner(System.in);
int T = Integer.parseInt(mScanner.nextLine());
Stack<Character> mStack;
for (int i = 0; i < T; i++) {
mStack = new Stack<>();
String testCase = mScanner.nextLine();
char[] lengStr = testCase.toCharArray();
int size = lengStr.length;
for (int k = 0; k < size; k++) {
if (Character.isLetter(lengStr[k])) {
System.out.print(lengStr[k]);
} else if (lengStr[k] == ')') {
System.out.print(mStack.peek());
mStack.pop();
} else if (lengStr[k] != '(') {
mStack.push(lengStr[k]);
}
}
System.out.println();
}
}
}
Bài 3:
cho 1 ma trận cấp N với các giá trị cho trước không giống nhau.
tưởng tượng có một hòn bi, bạn để nó ở đỉnh cao thì sẽ lăn xuống nơi thấp hơn. coi độ cao của địa
hình tương đương giá trị của phần tử trong ma trận, và giả sử viên bi sẽ lăn từ điểm đặt tới điểm mà
thấp hơn nhất bên cạnh so với nó. (chỉ tính ngang dọc).
output : tìm quãng đường dài nhất mà viên bi có thể lăn được.
vd :
23 24 35 13 44 10
37 27 79 60 66 11
45 78 89 90 18 30
50 65 72 67 55 51
57 71 81 17 92 14
31 47 29 83 77 15
quãng đường dài nhất : 89 - 72 - 65 - 50 - 45 - 37 - 23 : 6 quãng đường.

Thuật toán:
Duyệt từng điểm của ma trận:
Tìm min của 4 đỉnh xung quanh điểm đang duyệt. lặp lại cho đến khi không tìm đc min.
Mỗi lần tìm đc min thì tăng biến đếm.

Với mỗi đỉnh thì kiểm tra xem giá trị tìm đc có lớn nhất không.
Cuối cùng in ra số lớn nhất:
public class Main2 {
static int[][] input;
static int n;
static int[] dx = { -1, 1, 0, 0 };
static int[] dy = { 0, 0, 1, -1 };
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int tc = sc.nextInt();
int value;
for (int testcase = 1; testcase <= tc; testcase++) {
int max = Integer.MIN_VALUE;
n = sc.nextInt();
input = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
input[i][j] = sc.nextInt();
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
value = duyet(i, j);
if (value > max)
max = value;
}
}
System.out.println("#" + testcase + " " + max);
}
}
private static int duyet(int i, int j) {
int mLength = 0;
int x2 = 0;
int min = Integer.MAX_VALUE;
int y2 = 0;
boolean check = false;
for (int u = 0; u < 4; u++) {
int x1 = i + dx[u];
int y1 = j + dy[u];
if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < n && input[x1][y1]
< input[i][j] && input[x1][y1] < min) {
min = input[x2][y2];
x2 = x1;
y2 = y1;
check = true;
}
}
if (check) {
mLength++;
mLength += duyet(x2, y2);
}
return mLength;
}

}
Bài 4:
Bruce Force's keyboard is broken, only a few keys are still working. Bruce has figured out he can
still type texts by switching the keyboard layout whenever he needs to type a letter which is
currently not mapped to any of the mworking keys of the keyboard.
You are given a text that Bruce wishes to type, and he asks you if you can tell him the maximum
number of consecutive characters in the text which can be typed without having to switch the
keyboard layout. For simplicity, we assume that each key of the keyboard will be mapped to
exactly one character, and it is not possible to type other characters by combination of different
keys. This means that Bruce wants to know the length of the largest substring of the text which
consists of at most m different characters.
Input
The input contains several test cases, each test case consisting of two lines. The first line of each
test case contains the number m (1 ≤ m ≤ 128), which specifies how many keys on the keyboard are
still working. The second line of each test case contains the text which Bruce wants to type. You
may assume that the length of this text does not exceed 1 million characters. Note that the input
may contain space characters, which should be handled like any other character.
The last test case is followed by a line containing one zero.
Output
For each test case, print one line with the length of the largest substring of the text which consists
of at most mdifferent characters.
Example
Input:
5
This can't be solved by brute force.
1
Mississippi
0
Output:
7
2

Thuật toán:

Tạo 1 biếm đếm để so sánh vs number key còn trên bàn phím

Tạo 1 array lưu các chữ cái đã đc duyệt

Tạo 1 biếm max để check max của cả chuỗi

Duyệt từng chữ cái 1. Tạo newmax để check max của lần duyệt dó

Nếu chữ cái chưa có trong array thì add vào array và tăng đếm. tăng max

Nếu chữ cái đã có trong array thì chỉ tăng max.

ở cả 2 trường hợp trên đều kiểm tra đem==number key thì break.

Sau khi break thì kiểm tra newmaxx>max thì gán max=newmax

Duyệt xong in ra max


public class Main {
static int numberKey;
static String mText;
static int max = 0;
static ArrayList<Character> arrText = new ArrayList<>();
public static void main(String[] args) {
Scanner mScanner = new Scanner(System.in);
numberKey = Integer.parseInt(mScanner.nextLine());
while (numberKey != 0) {
mText = mScanner.nextLine();
int value = checkNumberText();
System.out.println(value + "");
numberKey = Integer.parseInt(mScanner.nextLine());
}
}
private static int checkNumberText() {
max = 0;
int size = mText.length();
for (int i = 0; i < size; i++) {
arrText.clear();
int newmax = 0;
int dem = 0;
for (int j = i; j < size; j++) {
if (arrText.size() == 0) {
arrText.add(mText.charAt(j));
dem++;
newmax++;
if (dem == numberKey) {
if (j < mText.length() - 1) {
if (!arrText.contains(mText.charAt(j
+ 1))) {
break;
}
} else {
break;
}
}
} else {
if (arrText.contains(mText.charAt(j))) {
newmax++;
} else {
if (dem == numberKey) {
break;
}
arrText.add(mText.charAt(j));
newmax++;
dem++;
}
}
}
if (max < newmax) {
max = newmax;
}
}
return max;
}
}
//Bài 5
//Đầu vào là 1 ma trận vuông Mỗi phần tử nhận giá trị 0-1
//Tượng trưng cho 1 vùng đất
//+giá trị 0 là đất
//+ giá trị 1 là nước
//Người ta muốn ước tính lượng nước trong vùng đất ấy
//giờ bắt mình ước tính
//theo cách sau:
//Tại mỗi điểm là nước
//Tính khoảng cách nhỏ nhất đến bờ
//Theo 4 hướng đông tây nam bắc
//7
//0 0 0 0 0 0 0
//0 0 0 1 0 0 0
//0 0 0 1 0 0 0
//0 1 1 1 1 1 0
//0 0 0 1 0 0 0
//0 0 0 1 0 0 0
//0 0 0 0 0 0 0
//Input như vậy => output 11
//Giải thích :
//xét số 1 trên cùng nhé
//khoảng cách là 1
//-> lượng nước là 1
//tương tự số 1 ngay dưới
//khoảng cách là 1 ( theo hướng đông, tây)
//-> lượng nước là 1
//tương tự với các số 1 không phải ở giao điểm kia
//sẽ là 1
//có 8 số 1 lượng nước là 1
//và số 1 ở trung tâm: có lượng nước là 3( theo 4 hướng đều bằng 3)

Thuật toán:

Duyệt từng điểm 1:

Nếu gặp số 1 thì duyệt về 4 hướng cho tới khi gặp số 0. return min của 4 hướng

Sauk hi duyệt hết thì cộng tất cả các số return khi duyệt số 1
public class Main {
static int MT[][];
static int mWater;
static int mSize;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int t = 0; t < T; t++) {
mWater = 0;
mSize = sc.nextInt();
MT = new int[mSize][mSize];
for (int i = 0; i < mSize; i++) {
for (int j = 0; j < mSize; j++) {
MT[i][j] = sc.nextInt();
}
}
for (int i = 0; i < mSize; i++) {
for (int j = 0; j < mSize; j++) {
if (MT[i][j] == 1) {
mWater += check(i, j);
}
}
}
System.out.println("Output:"+mWater);
}
}
private static int check(int i, int j) {
int dsUp = 1, dsDown = 1, dsLeft = 1, dsRight = 1;
int iC = i, jC = j;
int min;
boolean checkR = false, checkL = false, checkU = false, checkD =
false;
while (iC > 0) {
checkU = true;
iC--;
if (MT[iC][j] == 1) {
dsUp++;
} else {
break;
}
}
iC = i;
while (iC + 1 < mSize) {
checkD = true;
iC++;
if (MT[iC][j] == 1) {
dsDown++;
} else {
break;
}
}
iC = i;
while (jC + 1 < mSize) {
checkR = true;
jC++;
if (MT[i][jC] == 1) {
dsRight++;
} else {
break;
}
}
jC=j;
while (jC > 0) {
checkL = true;
jC--;
if (MT[i][jC] == 1) {
dsLeft++;
} else {
break;
}
}
if (checkR && checkL && checkD && checkU) {
min = Math.min(Math.min(dsUp, dsDown), Math.min(dsRight,
dsLeft));
} else {
ArrayList<Integer> arrNumber = new ArrayList<>();
if (checkD) {
arrNumber.add(dsDown);
}
if (checkU) {
arrNumber.add(dsUp);
}
if (checkL) {
arrNumber.add(dsLeft);
}
if (checkR) {
arrNumber.add(dsRight);
}
if (arrNumber.size() == 1) {
min = arrNumber.get(0);
} else if (arrNumber.size() == 2) {
min = Math.min(arrNumber.get(0), arrNumber.get(1));
} else {
min = Math.min(arrNumber.get(0),
Math.min(arrNumber.get(1), arrNumber.get(2)));
}
}
return min;
}
}
Bài 6:

//Đếm số cụm và max số cụm


// A=
// 1 0 0 0 1
// 1 0 0 1 1
// 1 0 0 1 1
// 0 1 1 0 0
// 1 0 1 0 0

Tạo 1 mảng để lưu trữ ma trận và 1 mảng để lưu trữ xem nó đc duyệt chưa

Tạo 1 biến max để lưu trữ max sao.

Duyệt tưng điểm của ma trận.

Nếu gặp 1 thì tăng biến đếm số cụm, biến đếm số sao và gán vị trí mảng
visited=1 sau đó duyệt đệ quy về 4 hướng

Return dem:

Sau khi kết thúc hàm thì so sánh max có lớn hơn biến đếm số sao k. nếu k thì
gán max= đếm số sao

Sau khi duyệt xong in số cụm và số sao.


public class Main {
static int[][] maTran;
static int[][] visited;
static int numberCum;
static int sizeMT;
public static void main(String[] args) {
Scanner mScanner = new Scanner(System.in);
int T = mScanner.nextInt();
for (int i = 0; i < T; i++) {
sizeMT = mScanner.nextInt();
maTran = new int[sizeMT][sizeMT];
visited = new int[sizeMT][sizeMT];
for (int u = 0; u < sizeMT; u++) {
for (int v = 0; v < sizeMT; v++) {
maTran[u][v] = mScanner.nextInt();
}
}
int maxSao = 0;
for (int j = 0; j < sizeMT; j++) {
for (int k = 0; k < sizeMT; k++) {
if (maTran[j][k] == 1 && visited[j][k] == 0) {
numberCum++;
int resultCum = dequy(j, k);
if (resultCum > maxSao) {
maxSao = resultCum;
}
}
}
}
System.out.println("Số cụm:" + numberCum + " max sao:" +
maxSao);
}
}
private static int dequy(int j, int k) {
int dem = 0;
if (maTran[j][k] == 1 && visited[j][k] == 0) {
dem++;
visited[j][k] = 1;
if (j > 0) {
dem += dequy(j - 1, k);
}
if (k > 0) {
dem += dequy(j, k - 1);
}
if (j + 1 < sizeMT) {
dem += dequy(j + 1, k);
}
if (k + 1 < sizeMT) {
dem += dequy(j, k + 1);
}
}
return dem;
}
}
Bài 7:

4 chữ cái được định nghĩa bởi 4 ma trận 3x3 (size chữ luôn là 3x3) như hình. Các chữ cái có thể xoay 90-
180-270 ví dụ như chữ T bên dưới

Các chữ cái trong 1 ma trận N x N ( N <= 100) thỏa mãn các điều kiện:

1. Không chồng lấn và luôn cách nhau ít nhất 1 hàng 0 (trên/dưới/trái/phái)


2. Luôn có nghĩa (luôn là 1 trong 4 chữ cái trên)

InPut:

Dòng 1 : số test case

Mỗi testcase gồm N : độ lớn ma trận và ma trận bên dưới

Out put #testCase số chữ mỗi loại

Sample

0 0 0 0 0 0 0

1 0 1 0 0 0 0

1 1 1 0 0 0 0

1 0 1 0 1 0 0

0 0 0 0 1 0 0

0 0 0 0 1 1 1

0 0 0 0 0 0 0

Output #1 1 1 0 0 (1 chữ H, 1 chữ L, 0 chữ T, 0 chữ U)

Thuật toán:

Duyệt từng điểm 1:

Nếu gặp số 1 thì duyệt 9 điểm xung quanh xem nó là chữ gì. Vào chữ nào tăng
biến đếm chữ đó

Sau khi duyệt xong thì in ra


public class Main {
static int[][] maTran;

public static void main(String[] args) {


//DONE HTLU
//Đề bài:C:\Users\Doanp\Desktop\HTLU.docx
Scanner mScanner = new Scanner(System.in);
int numberTestCase = mScanner.nextInt();
for (int i = 0; i < numberTestCase; i++) {
int numberH = 0, numberT = 0, numberL = 0, numberU = 0;
int sizeMaTran = mScanner.nextInt();
maTran = new int[sizeMaTran][sizeMaTran];
for (int u = 0; u < sizeMaTran; u++) {
for (int v = 0; v < sizeMaTran; v++) {
maTran[u][v] = mScanner.nextInt();
}
}
for (int m = 1; m < sizeMaTran - 1; m++) {
for (int n = 1; n < sizeMaTran - 1; n++) {
if (checkH(m, n) == 1) {
numberH++;
}
if (checkT(m, n) == 1) {
numberT++;
}
if (checkL(m, n) == 1) {
numberL++;
}
if (checkU(m, n) == 1) {
numberU++;
}
}
}
System.out.println("#" + (i + 1) + " " + numberH + " " +
numberL + " " + numberT + " " + numberU);
}
}

private static int checkH(int m, int n) {


if ((maTran[m - 1][n - 1] == 1 && maTran[m - 1][n] == 0 &&
maTran[m - 1][n + 1] == 1 && maTran[m][n - 1] == 1
&& maTran[m][n] == 1 && maTran[m][n + 1] == 1 &&
maTran[m + 1][n - 1] == 1 && maTran[m + 1][n] == 0
&& maTran[m + 1][n + 1] == 1)
|| (maTran[m - 1][n - 1] == 1 && maTran[m - 1][n] == 1
&& maTran[m - 1][n + 1] == 1
&& maTran[m][n - 1] == 0 && maTran[m][n]
== 1 && maTran[m][n + 1] == 0
&& maTran[m + 1][n - 1] == 1 && maTran[m +
1][n] == 1 && maTran[m + 1][n + 1] == 1)) {
return 1;
}
return 0;
}

private static int checkT(int m, int n) {


if ((maTran[m - 1][n - 1] == 1 && maTran[m - 1][n] == 1 &&
maTran[m - 1][n + 1] == 1 && maTran[m][n - 1] == 0
&& maTran[m][n] == 1 && maTran[m][n + 1] == 0 &&
maTran[m + 1][n - 1] == 0 && maTran[m + 1][n] == 1
&& maTran[m + 1][n + 1] == 0)
|| (maTran[m - 1][n - 1] == 0 && maTran[m - 1][n] == 0
&& maTran[m - 1][n + 1] == 1
&& maTran[m][n - 1] == 1 && maTran[m][n]
== 1 && maTran[m][n + 1] == 1
&& maTran[m + 1][n - 1] == 0 && maTran[m +
1][n] == 0 && maTran[m + 1][n + 1] == 1)
|| (maTran[m - 1][n - 1] == 0 && maTran[m - 1][n] == 1
&& maTran[m - 1][n + 1] == 0
&& maTran[m][n - 1] == 0 && maTran[m][n]
== 1 && maTran[m][n + 1] == 0
&& maTran[m + 1][n - 1] == 1 && maTran[m +
1][n] == 1 && maTran[m + 1][n + 1] == 1)
|| (maTran[m - 1][n - 1] == 1 && maTran[m - 1][n] == 0
&& maTran[m - 1][n + 1] == 0
&& maTran[m][n - 1] == 1 && maTran[m][n]
== 1 && maTran[m][n + 1] == 1
&& maTran[m + 1][n - 1] == 1 && maTran[m +
1][n] == 0 && maTran[m + 1][n + 1] == 0)) {
return 1;
}
return 0;
}

private static int checkL(int m, int n) {


if ((maTran[m - 1][n - 1] == 1 && maTran[m - 1][n] == 0 &&
maTran[m - 1][n + 1] == 0 && maTran[m][n - 1] == 1
&& maTran[m][n] == 0 && maTran[m][n + 1] == 0 &&
maTran[m + 1][n - 1] == 1 && maTran[m + 1][n] == 1
&& maTran[m + 1][n + 1] == 1)
|| (maTran[m - 1][n - 1] == 1 && maTran[m - 1][n] == 1
&& maTran[m - 1][n + 1] == 1
&& maTran[m][n - 1] == 1 && maTran[m][n]
== 0 && maTran[m][n + 1] == 0
&& maTran[m + 1][n - 1] == 1 && maTran[m +
1][n] == 0 && maTran[m + 1][n + 1] == 0)
|| (maTran[m - 1][n - 1] == 1 && maTran[m - 1][n] == 1
&& maTran[m - 1][n + 1] == 1
&& maTran[m][n - 1] == 0 && maTran[m][n]
== 0 && maTran[m][n + 1] == 1
&& maTran[m + 1][n - 1] == 0 && maTran[m +
1][n] == 0 && maTran[m + 1][n + 1] == 1)
|| (maTran[m - 1][n - 1] == 0 && maTran[m - 1][n] == 0
&& maTran[m - 1][n + 1] == 1
&& maTran[m][n - 1] == 0 && maTran[m][n]
== 0 && maTran[m][n + 1] == 1
&& maTran[m + 1][n - 1] == 1 && maTran[m +
1][n] == 1 && maTran[m + 1][n + 1] == 1)) {
return 1;
}
return 0;
}
private static int checkU(int m, int n) {
if ((maTran[m - 1][n - 1] == 1 && maTran[m - 1][n] == 0 &&
maTran[m - 1][n + 1] == 1 && maTran[m][n - 1] == 1
&& maTran[m][n] == 0 && maTran[m][n + 1] == 1 &&
maTran[m + 1][n - 1] == 0 && maTran[m + 1][n] == 1
&& maTran[m + 1][n + 1] == 0)
|| (maTran[m - 1][n - 1] == 0 && maTran[m - 1][n] == 1
&& maTran[m - 1][n + 1] == 0
&& maTran[m][n - 1] == 0 && maTran[m][n]
== 1 && maTran[m][n + 1] == 1
&& maTran[m + 1][n - 1] == 0 && maTran[m +
1][n] == 1 && maTran[m + 1][n + 1] == 1)
|| (maTran[m - 1][n - 1] == 0 && maTran[m - 1][n] == 1
&& maTran[m - 1][n + 1] == 0
&& maTran[m][n - 1] == 1 && maTran[m][n]
== 0 && maTran[m][n + 1] == 1
&& maTran[m + 1][n - 1] == 1 && maTran[m +
1][n] == 0 && maTran[m + 1][n + 1] == 1)
|| (maTran[m - 1][n - 1] == 1 && maTran[m - 1][n] == 1
&& maTran[m - 1][n + 1] == 0
&& maTran[m][n - 1] == 0 && maTran[m][n]
== 0 && maTran[m][n + 1] == 1
&& maTran[m + 1][n - 1] == 1 && maTran[m +
1][n] == 1 && maTran[m + 1][n + 1] == 0)) {
return 1;
}
return 0;
}

}
Bài 8:

//Cho 1 xây ký tự kích thước N (4<= N <=128, N = 2^k). Hãy xây dựng 1 ma trân
đối xứng NxN với hàng đầu tiên là thứ tự của xâu đó.
//vd: input
//ABCD
//output:
//ABCD
//BADC
//CDAB
//DCBA

Biến chuỗi vừa nhập thành array char

Biến leng N là độ dài của chuỗi

Tạo ma trận [N][N] để lưu mảng đối xứng.

Duyệt for lồng..

Ma trận [i][j]=mảng char[i^j]

Sau khi xong thì duyệt lại mảng [N][N] để in ra


package com.dtv.main;

import java.util.Scanner;
public class MaTranDoiXung {

public static void main(String[] args) {


//DONE
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int tc = 0; tc < T; tc++) {
String str = sc.next();
char arrChars[] = str.trim().toCharArray();
int N = arrChars.length;
char mat[][] = new char[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
mat[i][j] = arrChars[i ^ j];
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (j != N - 1)
System.out.print(mat[i][j] + " ");
else
System.out.println(mat[i][j]);
}
}
}

}
Bài 9:

QKP tính số an toàn trên bàn cờ


Mỗi ma trận có 4 hàng. Hàng 1 là kích thước ma trận. hang 2 là số quân hậu và
vị trí trên bàn cơ. Hang 3 là số quân mã, hang 4 là số quân tốt.
Dừng nhập cho tới khi nhập kích thước ma trận = “0 0”

Example

4 4
2 1 4 2 4
1 1 2
1 2 3
2 3
1 1 2
1 1 1
0
1000 1000
1 3 3
0
0
0 0

Output:
Board 1 has 6 safe squares.
Board 2 has 0 safe squares.
Board 3 has 996998 safe squares.

Thuật toán: Với ma trận [N][N] thì ta sẽ đánh dấu các quân cờ lên. Quân hậu=1
quân mã =2 quân tốt =3

Còn lại sẽ là 0

Duyệt bước đi của quân hậu. nếu ăn đc mà k bị chặn bởi tốt thì đánh 4

Duyệt bước đi của quân mã. Nếu ăn đc mà k bị chặn bởi tốt thì đánh 5

//Chỉnh sửa lại đoạn đi của mã xem nó có bị chặn của tốt k và submit lại code
vào trang http://www.spoj.com/problems/QKP/
import java.util.ArrayList;
import java.util.Scanner;
class Main {
static int[][] maTranCo;
static int[][] mSafe;
static int mRow;
static int mCol;
public static void main(String[] args) throws java.lang.Exception {
//FIXME Sai trên spoj http://www.spoj.com/problems/QKP/
try {
Scanner mScanner = new Scanner(System.in);
String sizeMaTran = mScanner.nextLine();
ArrayList<QuanCo> arrQ = new ArrayList<>();
ArrayList<QuanCo> arrK = new ArrayList<>();
ArrayList<TestQPK> arrTestcase = new ArrayList<>();
while (!sizeMaTran.equals("0 0")) {
String a = mScanner.nextLine();
String b = mScanner.nextLine();
String c = mScanner.nextLine();
arrTestcase.add(new TestQPK(sizeMaTran, a, b, c));
sizeMaTran = mScanner.nextLine();
}
for (int d = 0; d < arrTestcase.size(); d++) {
String sizeMT=arrTestcase.get(d).getA();
String strQ = arrTestcase.get(d).getB();
String strK = arrTestcase.get(d).getC();
String strP = arrTestcase.get(d).getD();
arrQ.clear();
arrK.clear();
String[] sizeMTArr = sizeMT.split(" ");
mRow = Integer.parseInt(sizeMTArr[0]);
mCol = Integer.parseInt(sizeMTArr[1]);
maTranCo = new int[mRow][mCol];
mSafe = new int[mRow][mCol];
int numberQ = Integer.parseInt(strQ.substring(0, 1));
int numberK = Integer.parseInt(strK.substring(0, 1));
int numberP = Integer.parseInt(strP.substring(0, 1));
if (numberQ != 0) {
String[] vitriQ = strQ.split(" ");
int j = 0;
for (int i = 0; i < numberQ; i++) {
// Q =1
maTranCo[Integer.parseInt(vitriQ[j + 1]) -
1][Integer.parseInt(vitriQ[j + 2]) - 1] = 1;
mSafe[Integer.parseInt(vitriQ[j + 1]) - 1]
[Integer.parseInt(vitriQ[j + 2]) - 1] = 1;
arrQ.add(new
QuanCo(Integer.parseInt(vitriQ[j + 1]) - 1, Integer.parseInt(vitriQ[j + 2]) -
1));
j += 2;
}
}
if (numberK != 0) {
String[] vitriK = strK.split(" ");
int j = 0;
for (int i = 0; i < numberK; i++) {
// K = 2
maTranCo[Integer.parseInt(vitriK[j + 1]) -
1][Integer.parseInt(vitriK[j + 2]) - 1] = 2;
mSafe[Integer.parseInt(vitriK[j + 1]) - 1]
[Integer.parseInt(vitriK[j + 2]) - 1] = 2;
arrK.add(new
QuanCo(Integer.parseInt(vitriK[j + 1]) - 1, Integer.parseInt(vitriK[j + 2]) -
1));
j += 2;
}
}
if (numberP != 0) {
int j = 0;
String[] vitriP = strP.split(" ");
for (int i = 0; i < numberP; i++) {
// P =3
maTranCo[Integer.parseInt(vitriP[j + 1]) -
1][Integer.parseInt(vitriP[j + 2]) - 1] = 3;
mSafe[Integer.parseInt(vitriP[j + 1]) - 1]
[Integer.parseInt(vitriP[j + 2]) - 1] = 3;
j += 2;
}
}
int sizeQ = arrQ.size();
int sizeK = arrK.size();
for (int i = 0; i < sizeQ; i++) {
checkQ(arrQ.get(i).getA(), arrQ.get(i).getB());
}
for (int i = 0; i < sizeK; i++) {
checkK(arrK.get(i).getA(), arrK.get(i).getB());
}
int dem = 0;
for (int u = 0; u < mRow; u++) {
for (int v = 0; v < mCol; v++) {
if (mSafe[u][v] == 0) {
dem++;
}
}
}
System.out.println("Board " + (d + 1) + " has " + dem
+ " safe squares.");
}
mScanner.close();
} catch (Exception e) {

}
}

private static void checkK(int a, int b) {


int a1 = a, b1 = b;
while ((a1 - 2) >= 0 && (b1 + 1) < mCol) {
a1 = a1 - 2;
b1 = b1 + 1;
if (maTranCo[a1][b1] == 0 && (mSafe[a1][b1] == 0 ||
mSafe[a1][b1] == 4)) {
mSafe[a1][b1] = 5;
} else {
break;
}
}
a1 = a;
b1 = b;
while ((a1 - 2) >= 0 && (b1 - 1) >= 0) {
a1 = a1 - 2;
b1 = b1 - 1;
if (maTranCo[a1][b1] == 0 && (mSafe[a1][b1] == 0 ||
mSafe[a1][b1] == 4)) {
mSafe[a1][b1] = 5;
} else {
break;
}
}
a1 = a;
b1 = b;
while ((a1 + 2) < mRow && (b1 + 1) < mCol) {
a1 = a1 + 2;
b1 = b1 + 1;
if (maTranCo[a1][b1] == 0 && (mSafe[a1][b1] == 0 ||
mSafe[a1][b1] == 4)) {
mSafe[a1][b1] = 5;
} else {
break;
}
}
a1 = a;
b1 = b;
while ((a + 2) < mRow && (b1 - 1) >= 0) {
a1 = a1 + 2;
b1 = b1 - 1;
if (maTranCo[a1][b1] == 0 && (mSafe[a1][b1] == 0 ||
mSafe[a1][b1] == 4)) {
mSafe[a1][b1] = 5;
} else {
break;
}
}
}

private static void checkQ(int a, int b) {


int a1 = a, b1 = b;
while (a1 - 1 >= 0) {
a1--;
if (maTranCo[a1][b] == 0 && mSafe[a1][b] == 0) {
mSafe[a1][b] = 4;
} else {
break;
}
}
a1 = a;
while (a1 + 1 < mRow) {
a1++;
if (maTranCo[a1][b] == 0 && mSafe[a1][b] == 0) {
mSafe[a1][b] = 4;
} else {
break;
}
}
a1 = a;
while (b1 - 1 >= 0) {
b1--;
if (maTranCo[a][b1] == 0 && mSafe[a][b1] == 0) {
mSafe[a][b1] = 4;
} else {
break;
}
}
b1 = b;
while (b1 + 1 < mCol) {
b1++;
if (maTranCo[a][b1] == 0 && mSafe[a][b1] == 0) {
mSafe[a][b1] = 4;
} else {
break;
}
}
b1 = b;
while (a1 - 1 >= 0 && b1 - 1 >= 0) {
b1--;
a1--;
if (maTranCo[a1][b1] == 0 && mSafe[a1][b1] == 0) {
mSafe[a1][b1] = 4;
} else {
break;
}
}
b1 = b;
a1 = a;
while (a1 - 1 >= 0 && b1 + 1 < mCol) {
b1++;
a1--;
if (maTranCo[a1][b1] == 0 && mSafe[a1][b1] == 0) {
mSafe[a1][b1] = 4;
} else {
break;
}
}
b1 = b;
a1 = a;
while (a1 + 1 < mRow && b1 - 1 >= 0) {
b1--;
a1++;
if (maTranCo[a1][b1] == 0 && mSafe[a1][b1] == 0) {
mSafe[a1][b1] = 4;
} else {
break;
}
}
b1 = b;
a1 = a;
while (a1 + 1 < mRow && b1 + 1 < mCol) {
b1++;
a1++;
if (maTranCo[a1][b1] == 0 && mSafe[a1][b1] == 0) {
mSafe[a1][b1] = 4;
} else {
break;
}
}
}
}
class TestQPK {
String A, B, C, D;
public String getA() {
return A;
}
public void setA(String a) {
A = a;
}
public String getB() {
return B;
}
public void setB(String b) {
B = b;
}
public String getC() {
return C;
}
public void setC(String c) {
C = c;
}
public String getD() {
return D;
}
public void setD(String d) {
D = d;
}
public TestQPK(String a, String b, String c, String d) {
super();
A = a;
B = b;
C = c;
D = d;
}
public TestQPK() {
super();
}
}
class QuanCo {
int a, b;
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public QuanCo(int a, int b) {
super();
this.a = a;
this.b = b;
}
public QuanCo() {
super();
}
}
Bài 10
//There are N people standing in a Queue. You are given the height of each
person and the number of people who are taller and standing ahead of him. You
have to find the position of each person.
//
//Input
//
//First line conatins a single integer T, the number of test cases. It is
followed by T test cases each of which contains 3 lines. First line of each
test case contains a single integer N. Second line contains N integers
representing the heights of these N people. Third line also contains N
integers denoting the number of taller people standing ahead of him.
//
//Output
//
//Output one line for each test case which contains the heights of the N
people in the order in which they are standing.
//
//Constraints
//
//0 < T <= 100
//0 < N <= 1000
//
//Expected Time Complexity = O(N2)
//Example
//
//Input:
//1
//5
//33 11 22 44 55
//0 2 1 1 0
//
//Output:
//33 22 11 55 44

Thuật toán:
Sắp xếp 2 ma trận giá trị và số số lớn hơn đứng trước lại theo thứ tự từ bé
tới lớn

Tạo biến dem=0;

Tạo array=kích thước của ma trận số

Duyệt từng điểm 1.

Nếu array[dem]==0 thì đếm ++ và số số lớn hơn đứng trước --;

Nếu array[dem]!=0 thì đếm ++

Lặp lại bước trên nếu số lớn hơn đúng trước vẫn >=0

Sau khi duyệt từng số thì có được biến đêm.

Array[dem-1]=giá trị của số vừa duyệt.

Cuối cùng là duyệt lại array và in ra kết quả


import java.util.Scanner;

public class Main {


public static void main(String[] args) {
//DONE
Scanner mScanner = new Scanner(System.in);
int sizeTextCase = mScanner.nextInt();
for (int testcase = 0; testcase < sizeTextCase; testcase++) {
int number = mScanner.nextInt();
int[] height = new int[number];
int[] number_taller = new int[number];
for (int i = 0; i < number; i++) {
height[i] = mScanner.nextInt();
}
for (int i = 0; i < number; i++) {
number_taller[i] = mScanner.nextInt();
}
int array[] = new int[number];
for (int i = 0; i < number; i++) {
int minPos = i;
int minValue = height[i];
for (int j = i; j < number; j++) {
if (height[j] < minValue) {
minValue = height[j];
minPos = j;
}
}
if (minPos != i) {
int tg = minValue;
height[minPos] = height[i];
height[i] = tg;

tg = number_taller[minPos];
number_taller[minPos] = number_taller[i];
number_taller[i] = tg ;
}
int pos = number_taller[i];
int dem = 0;
do {
if (array[dem] == 0) {
dem++;
pos--;
} else
dem++;
} while (pos >= 0);
array[dem - 1] = height[i];
}
System.out.println();
for (int m = 0; m < number; m++) {
System.out.print(array[m] + " ");
}
}
}

}
BROKEN - Broken Keyboard:http://www.spoj.com/problems/BROKEN/(Time limit)
PALIN - The Next Palindrome:http://www.spoj.com/problems/PALIN/
TOANDFRO - To and Fro:http://www.spoj.com/problems/TOANDFRO/

HTLU:https://www.facebook.com/groups/1169889303023249/1173469379331908/?
hc_location=ufi(Done)
QUE1 - Queue (Rookie):http://www.spoj.com/problems/QUE1/(done)
CANTON - Count on Cantor:http://www.spoj.com/problems/CANTON/(done)
Đếm số cụm và số max sao trong cụm(Done)
ONP - Transform the Expression:http://www.spoj.com/problems/ONP/(done)
Dat nuoc:https://www.facebook.com/groups/1169889303023249/permalink/1171065656238947/
(done)
QKP:http://www.spoj.com/problems/QKP/ (done nhưng chưa chạy đúng trên server)

You might also like