Connect Pro

 avatar
quoc14
c_cpp
22 days ago
20 kB
4
Indexable
Never
Backtrack
Level 4
Connect processors
Bộ xử lý di động mới được phát triển Maxinos của Samsung có số lượng ô là N x N.

Trong 1 cell có thể có 1 lõi hoặc 1 dây dẫn điện.

Nguồn điện chạy dọc theo rìa Maxinos.

Dây kết nối lõi và nguồn điện chỉ có thể được lắp theo đường thẳng.

Các dây không thể giao nhau.

Tình trạng ban đầu của Maxinos trước khi kết nối dây được đưa ra như sau.

(Các Lõi ở rìa Maxinos được coi là đã được kết nối với nguồn điện.)

▶ Khi số lõi tối đa được kết nối với nguồn điện, hãy tìm tổng chiều dài của dây dẫn. 


   Tuy nhiên, nếu có nhiều lựa chọn khác nhau thì hãy tìm giá trị nhỏ nhất của tổng chiều dài sợi dây.

Đáp án của bài tập trên là 12.

[Hạn chế]




1. 7 ≤  N ≤ 12




2. Số lượng Core lớn hơn hoặc bằng 1 và nhỏ hơn hoặc bằng 12(1 ≤   N  ≤ 12).




3. Có thể có những Lõi không được kết nối với nguồn điện, mặc dù người ta đã cố gắng kết nối nó với nguồn điện nhiều nhất có thể.

 

 

[Đầu vào]
Dòng đầu tiên chứa T, số lượng trường hợp thử nghiệm.

Các trường hợp thử nghiệm được đưa ra bắt đầu từ dòng tiếp theo.

Dòng đầu tiên của mỗi trường hợp thử nghiệm có N, và N dòng tiếp theo là điều kiện ban đầu của Maxinos , được đưa ra trong mảng N x N.

Số 0 biểu thị ô trống, số 1 biểu thị Lõi và không có số nào khác được đưa ra.

[Đầu ra]
In ' #tc ' cho mỗi trường hợp thử nghiệm và để lại một khoảng trống rồi in ra câu trả lời.

(tc là số trường hợp thử nghiệm và bắt đầu bằng 1)


Input Example

3             // The number of test case, T = 3

7         // TC 1, N = 7

0 0 1 0 0 0 0

0 0 1 0 0 0 0

0 0 0 0 0 1 0

0 0 0 0 0 0 0

1 1 0 1 0 0 0

0 1 0 0 0 0 0

0 0 0 0 0 0 0

9         // TC 2, N = 9

0 0 0 0 0 0 0 0 0

0 0 1 0 0 0 0 0 1

1 0 0 0 0 0 0 0 0

0 0 0 1 0 0 0 0 0

0 1 0 0 0 0 0 0 0

0 0 0 0 0 0 1 0 0

0 0 0 1 0 0 0 0 0

0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 1

11             // TC 3, N = 11

0 0 1 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 1

0 0 0 1 0 0 0 0 1 0 0

0 1 0 1 1 0 0 0 1 0 0

0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 1 0 0 0

0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 1 0 0

0 0 0 0 0 0 1 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0

Output Example

#1 12           //1st TC’s Output

#2 10           //2nd TC’s Output

#3 24           //3rd TC’s Output

#1 12
#2 10
#3 24
#4 31
#5 25
#6 15
#7 27
#8 22
#9 25
#10 17
#11 13
#12 17
#13 19
#14 26
#15 26
#16 21
#17 20
#18 14
#19 21
#20 19
#21 21
#22 22
#23 21
#24 19
#25 21
#26 25
#27 19
#28 24
#29 20
#30 25
#31 20
#32 10
#33 17
#34 19
#35 22
#36 16
#37 25
#38 27
#39 10
#40 29
#41 26
#42 32
#43 23
#44 21
#45 21
#46 22
#47 22
#48 18
#49 26
#50 39
Time: 0.32800 s.
50
7
0 0 1 0 0 0 0
0 0 1 0 0 0 0
0 0 0 0 0 1 0
0 0 0 0 0 0 0
1 1 0 1 0 0 0
0 1 0 0 0 0 0
0 0 0 0 0 0 0
9
0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0
0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1
11
0 0 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0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 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 1 0
0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 1 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0 0 0
12
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 1 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0
0 0 1 0 1 0 0 1 0 0 0 0
0 0 0 0 1 0 0 0 0 0 0 0
    #include <iostream>
#include <time.h>
using namespace std;

int oo = 2000000000;

int T, n, result, maxx, m;
int mp[12][12], vs[12][12], core[144][2];


// up down left right
int dx[5] = {-1, 1, 0, 0, 0};
int dy[5] = {0, 0, -1, 1, 0};

bool inSize(int x, int y){
	return x >= 0 && y >= 0 && x < n && y < n;
}

void backtrack(int index, int cnt, int sum){
	if(m - maxx < index - cnt) return;
	if(index == m){
		if(cnt > maxx || (cnt == maxx && sum < result)){
			maxx = cnt, result = sum;
		}
		return;
	}
	for(int d = 0; d < 5; d++){
		int xx = core[index][0] + dx[d];
		int yy = core[index][1] + dy[d];
		int cc = 1;
		bool hasLine = false;
		while(inSize(xx, yy) && !vs[xx][yy]){
			vs[xx][yy]++;
			xx += dx[d];
			yy += dy[d];
			cc++;
		}
		if(!inSize(xx, yy)){
			hasLine = true;
		}

		if(hasLine) backtrack(index + 1, cnt+1, sum + cc - 1);
		else backtrack(index + 1, cnt, sum);

		xx -= dx[d];
		yy -= dy[d];
		while(inSize(xx, yy) && !(xx == core[index][0] && yy == core[index][1])){
			vs[xx][yy]--;
			xx -= dx[d];
			yy -= dy[d];
		}
	}
}

int main(){

	freopen("input.txt", "r", stdin);

	// Calc clock
	clock_t time_start, time_end; 
	time_start = clock(); 
	
	cin >> T;
	for(int tc = 1; tc <= T; tc++){
		// Initial && Input
		cin >> n;
		m = 0;
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++) {
				cin >> mp[i][j];
				vs[i][j] = mp[i][j];
				if(mp[i][j] == 1){
					core[m][0] = i, core[m++][1] = j;
				}
			}
		}
		result = oo;
		maxx = -oo;

		// Solve Problem	
		backtrack(0, 0, 0);
		
		// Output
		cout << "#" << tc << " " << result << endl;
	}

	// Calc Time
	time_end = clock();
	cout.setf(ios::fixed);
	cout.precision(5);
	cout << "Time: " << double (time_end - time_start) / double (CLOCKS_PER_SEC) << " s." << endl;
	
	return 0;
 }
Leave a Comment