1. 程式人生 > >Picking up Jewels

Picking up Jewels

-m mat guid back .net fcm std into 能夠

Picking up Jewels

技術分享

There is a maze that has one entrance and one exit.
Jewels are placed in passages of the maze.
You want to pick up the jewels after getting into the maze through the entrance and before getting out of it through the exit.
You want to get as many jewels as possible, but you don

’t want to take the same passage you used once.


When locations of a maze and jewels are given,
find out the greatest number of jewels you can get without taking the same passage twice, and the path taken in this case.


Time limit : 1 sec (Java : 2 sec) (If your program exceeds this time limit, the answers that have been already printed are ignored and the score becomes 0. So, it may be better to print a wrong answer when a specific test case might cause your program to exceed the time limit. One guide for the time limit excess would be the size of the input.)


[Input]
There can be more than one test case in the input file. The first line has T, the number of test cases.
Then the totally T test cases are provided in the following lines (T ≤ 10 )


In each test case,
In the first line, the size of the maze N (1 ≤ N ≤ 10) is given. The maze is N×N square-shaped.

From the second line through N lines, information of the maze is given.
“0” means a passage, “1” means a wall, and “2” means a location of a jewel.
The entrance is located on the upper-most left passage and the exit is located on the lower-most right passage.
There is no case where the path from the entrance to the exit doesn’t exist.


[Output]
For each test case, you should print "Case #T" in the first line where T means the case number.

For each test case, from the first line through N lines, mark the path with 3 and output it.
In N+1 line, output the greatest number of jewels that can be picked up.

[I/O Example]

Input
2
5
0 0 0 2 0
2 1 0 1 2
0 0 2 2 0
0 1 0 1 2
2 0 0 0 0
6
0 1 2 1 0 0
0 1 0 0 0 1
0 1 2 1 2 1
0 2 0 1 0 2
0 1 0 1 0 1
2 0 2 1 0 0


Output

Case #1

3 0 3 3 3
3 1 3 1 3
3 0 3 2 3
3 1 3 1 3
3 3 3 0 3
6

Case #2

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

/*

You should use the statndard input/output

in order to receive a score properly.

Do not use file input and output

Please be very careful.

*/

#include <stdio.h>

#define MAX_N 11

#define true 1

#define false 0

int Array[MAX_N][MAX_N];

int Path[MAX_N][MAX_N];//保存走過的路徑

int Visited[MAX_N][MAX_N];//標記走過的路徑

int Answer[MAX_N][MAX_N];

int Max_Jewes;

int N;

//int total ;

//int Answer = 0 ;

void SavePath()

{

int i,j;

for(i=0;i<N;i++)

{

for(j=0;j<N;j++)

{

Answer[i][j] = Path[i][j];

}

}

}

int Visit(int i, int j, int total)

{

int ret = 0;

Path[i][j] = 3 ;

Visited[i][j] = true ;

if((i ==0)&&(j == 0)&&(Array[i][j] == 2))//假設第一個節點為2total1

{

total++;

}

if((i == N-1)&&(j == N-1))//假設到達終點,則推斷本次遍歷的結果和之前全部次數中最大的結果最比較。假設大於之前的結果。則保存這個最大值和本次路徑

{

if(total > Max_Jewes)

{

Max_Jewes = total ;

SavePath();

}

Visited[i][j] = false ;//將終點標記成未掃描過,使下一次遍歷還能夠掃描到該點

return 1;

}

if((j+1) <= (N-1))//down

{

if((!Visited[i][j+1])&&(Array[i][j+1] != 1))//假設該點的下方的點沒有被掃描過,而且該點下方的點不為墻(1

{

if(Array[i][j+1] == 2)//假設有寶藏,則總數加1。繼續遍歷

Visit(i, j+1, total+1);

else

Visit(i, j+1, total);//假設沒有寶藏,則總數不變,繼續遍歷

Path[i][j+1] = Array[i][j+1] ;//死角,回退之後恢復path的值

}

}

if((j-1) >= 0)//up

{

if((!Visited[i][j-1])&&(Array[i][j-1] != 1))

{

if(Array[i][j-1] == 2)

Visit(i , j-1, total+1);

else

Visit(i , j-1, total);

Path[i][j-1] = Array[i][j-1] ;

}

}

if((i+1) <= (N-1))//right

{

if((!Visited[i+1][j])&&(Array[i+1][j] != 1))

{

if(Array[i+1][j] == 2)

Visit(i+1 , j, total+1);

else

Visit(i+1 , j, total);

Path[i+1][j] = Array[i+1][j] ;

}

}

if((i-1) >= 0)//left

{

if((!Visited[i-1][j])&&(Array[i-1][j] != 1))

{

if(Array[i-1][j] == 2)

Visit(i-1 , j, total+1);

else

Visit(i-1 , j, total);

Path[i-1][j] = Array[i-1][j];

}

}

Visited[i][j] = false ;

return -1;

}

int main(void)

{

int T, test_case;

/*

The freopen function below opens input.txt file in read only mode, and afterward,

the program will read from input.txt file instead of standard(keyboard) input.

To test your program, you may save input data in input.txt file,

and use freopen function to read from the file when using scanf function.

You may remove the comment symbols(//) in the below statement and use it.

But before submission, you must remove the freopen function or rewrite comment symbols(//).

*/

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

/*

If you remove the statement below, your program‘s output may not be rocorded

when your program is terminated after the time limit.

For safety, please use setbuf(stdout, NULL); statement.

*/

setbuf(stdout, NULL);

scanf("%d", &T);

for(test_case = 0; test_case < T; test_case++)

{

int i ,j ;

//total = 0 ;

Max_Jewes = -1 ;

scanf("%d",&N);

for(i=0;i<N;i++)

{

for(j=0;j<N;j++)

{

scanf("%d",&Array[i][j]);

Path[i][j] = Array[i][j];

}

}

for(i=0;i<N;i++)

{

for(j=0;j<N;j++)

{

Visited[i][j] = false ;////初始化遍歷標誌

}

}

Visit(0 , 0, 0);

printf("Case #%d\n", test_case+1);

//printf("%d\n", Answer);

for(i=0;i<N;i++)

{

for(j=0;j<N;j++)

{

printf("%d ",Answer[i][j]);

}

printf("\n");

}

printf("%d\n", Max_Jewes);

/////////////////////////////////////////////////////////////////////////////////////////////

/*

Implement your algorithm here.

The answer to the case will be stored in variable Answer.

*/

/////////////////////////////////////////////////////////////////////////////////////////////

//Answer = 0;

// Print the answer to standard output(screen).

//printf("Case #%d\n", test_case+1);

//printf("%d\n", Answer);

}

return 0;//Your program should return 0 on normal termination.

}

Picking up Jewels