Problem
Problems that require minimum paths through some domain appear in many different areas of computer science. For example, one of the constraints in VLSI routing problems is minimizing wire length. The Traveling Salesperson Problem (TSP) — finding whether all the cities in a salesperson’s route can be visited exactly once with a specified limit on travel time — is one of the canonical examples of an NP-complete problem; solutions appear to require an inordinate amount of time to generate, but are simple to check. This problem deals with finding a minimal path through a grid of points while traveling only from left to right. Given an m×n matrix of integers, you are to write a program that computes a path of minimal weight. A path starts anywhere in column 1 (the first column) and consists of a sequence of steps terminating in column n (the last column). A step consists of traveling from column i to column i + 1 in an adjacent (horizontal or diagonal) row. The first and last rows (rows 1 and m) of a matrix are considered adjacent, i.e., the matrix “wraps” so that it represents a horizontal cylinder. Legal steps are illustrated on the right. The weight of a path is the sum of the integers in each of the n cells of the matrix that are visited. For example, two slightly different 5×6 matrices are shown below (the only difference is the numbers in the bottom row). The minimal path is illustrated for each matrix. Note that the path for the matrix on the right takes advantage of the adjacency property of the first and last rows.
The minimal path is illustrated for each matrix. Note that the path for the matrix on the right takes advantage of the adjacency property of the first and last rows.
Input
The input consists of a sequence of matrix specifications. Each matrix specification consists of the row and column dimensions in that order on a line followed by m · n integers where m is the row dimension and n is the column dimension. The integers appear in the input in row major order, i.e., the first n integers constitute the first row of the matrix, the second n integers constitute the second row and so on. The integers on a line will be separated from other integers by one or more spaces. Note: integers are not restricted to being positive. There will be one or more matrix specifications in an input file. Input is terminated by end-of-file. For each specification the number of rows will be between 1 and 10 inclusive; the number of columns will be between 1 and 100 inclusive. No path’s weight will exceed integer values representable using 30 bits.
Output
Two lines should be output for each matrix specification in the input file, the first line represents a minimal-weight path, and the second line is the cost of a minimal path. The path consists of a sequence of n integers (separated by one or more spaces) representing the rows that constitute the minimal path. If there is more than one path of minimal weight the path that is lexicographically smallest should be output. Note: Lexicographically means the natural order on sequences induced by the order on their elements.
//Input_1.txt
5 6
3 4 1 2 8 6
6 1 8 2 7 4
5 9 3 9 9 5
8 4 1 3 2 6
3 7 2 8 6 4
5 6
3 4 1 2 8 6
6 1 8 2 7 4
5 9 3 9 9 5
8 4 1 3 2 6
3 7 2 1 2 3
//Output_1.txt
3 1 3 3 2 4
16
3 1 1 1 2 3
11
//Input_2.txt
5 6
3 4 1 -10 8 6
6 1 100 2 7 4
5 -1 3 9 9 5000
8 4 1 3 200 6
3 7 2 8 6 4
//Output_2.txt
3 1 1 -10 6 4
5
//Input_3.txt
5 6
3 4 1 0 8 6
6 1 0 2 7 0
5 0 0 9 0 0
8 0 1 3 0 6
3 7 2 0 6 4
//Output_3.txt
3 0 1 0 0 0
4
//Input_4.txt
5 8
3 4 1 -11 8 6 9 5
6 1 33 2 7 -88 8 6
5 11 -44 9 77 99 7 7
8 22 1 3 -66 6 6 8
3 7 2 55 6 4 5 9
//Output_4.txt
3 1 -44 2 7 -88 8 5
-106
//Input_5.txt
1 1
3
2 1
3
6
1 2
3 6
5 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
//Output_5.txt
3
3
3
3
3 6
9
1 2 3 4 5 6 7 8
36
//Input_6.txt
5 8
1 2 3 4 4 6 7 8
1 2 3 3 5 6 7 8
5 3 2 4 5 6 7 7
5 1 3 4 5 6 6 8
5 3 3 4 5 5 7 8
5 8
-1 -2 -3 -4 -4 -6 -7 -8
-1 -2 -3 -3 -5 -6 -7 -8
-5 -3 -2 -4 -5 -6 -7 -7
-5 -1 -3 -4 -5 -6 -6 -8
-5 -3 -3 -4 -5 -5 -7 -8
//Output_6.txt
1 2 2 3 4 5 6 7
30
-5 -3 -3 -4 -5 -6 -7 -8
-41
#define _CRT_SECURE_NO_WARNINGS
#define NUMBER_OF_FILES 7
#define MAX 100000;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Matrix {
int data[10][100];
int path[10][100];
int dp[10][100];
}matrix;
int result[5][10] = { 0, };
int tot_path[5];
void File_input(int);
void File_output(int, int, int);
void Algorithm(int, int, int, int);
void File_input(int number_of_files) {
char Input[7][15] = {
"Input_1.txt", "Input_2.txt", "Input_3.txt", "Input_4.txt", "Input_5.txt", "Input_6.txt", "Input_7.txt"
};
FILE* input;
input = fopen(Input[number_of_files], "r");
int i, j = 0, set_num = 0, next_set = 1;
int row, col = 0;
char arr[100];
if (input != NULL) {
while (1) {
int a = fscanf(input, "%d %d", &row, &col);
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
int a = fscanf(input, "%d", &matrix.data[i][j]);
}
}
if (fgets(arr, sizeof(arr), input) != NULL)
set_num++;
else {
break;
}
Algorithm(row, col, number_of_files, set_num);
}
}
else {
printf("Error.");
exit(1);
}
fclose(input);
}
void File_output(int i, int col, int set_num) {
char Output[7][15] = {
"Output_1.txt", "Output_2.txt", "Output_3.txt", "Output_4.txt", "Output_5.txt", "Output_6.txt", "Output_7.txt"
};
FILE* output;
output = fopen(Output[i], "w");
if (output != NULL) {
for (int i = 0; i < set_num; i++) {
for (int j = 0; j < col; j++) {
if (result[i][j] > -100000) {
fprintf(output, "%d ", result[i][j]);
}
}
fprintf(output, "\n%d\n", tot_path[i]);
}
}
else {
printf("Error.");
exit(1);
}
col = 0;
fclose(output);
}
void Algorithm(int row, int col, int file_num, int set_num) {
int r, c, left = 0, ref, x = 0, min = 10000;
int i, j;
int exception = 0;
for (int i = 0; i < row; i++) {
matrix.dp[i][col - 1] = matrix.data[i][col - 1];
}
if (col == 1)
exception = matrix.data[0][0];
for (i = col - 2; i >= 0; i--) {
for (j = 0; j < row; j++) {
ref = 100000;
for (int k = 0; k < 3; k++) {
left = (j + k + row - 1) % row;
if (matrix.dp[left][i + 1] < ref) {
ref = matrix.dp[left][i + 1];
matrix.path[j][i] = left;
}
}
matrix.dp[j][i] = matrix.data[j][i] + ref;
}
for (int a = 0; a < row; a++) {
if (matrix.dp[a][i] < min) {
min = matrix.dp[a][i];
result[set_num][i] = matrix.data[a][i];
}
}
min = 1000;
left = 100000;
//고냥 맨 왼쪽에서 작은 값 출력.
for (int i = 0; i < row; i++) {
if (matrix.dp[i][0] < left) {
left = matrix.dp[i][0];
x = i;
}
}
}
result[set_num-1][0] = matrix.data[x][0];
j = 1;
for (int i = 0; i < col-1; i++) {
x = matrix.path[x][i];
result[set_num-1][i + 1] = matrix.data[x][j];
j++;
}
tot_path[set_num-1] = left + exception;
File_output(file_num, col, set_num);
}
int main(void) {
for (int i = 0; i < NUMBER_OF_FILES; i++) {
File_input(i);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 10; j++)
result[i][j] = -100000;
}
}
}
'문제' 카테고리의 다른 글
[백준/BOJ-9251] LCS (0) | 2022.10.24 |
---|---|
[UVa-10090] Marbles (jewel box) (0) | 2022.10.10 |
[UVa-10034] Freckles (Saving ink) 백준 4386 (0) | 2022.10.08 |
[UVa - 10069] Distinct Subsequences (0) | 2022.10.03 |
[UVa - 10026] 구두수선공 문제 (shoemaker's problem) (0) | 2022.09.28 |