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





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



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

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

/*

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 ;

{

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

{

{

。继续遍历

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的更多相关文章

  1. hdu.1044.Collect More Jewels(bfs + 状态压缩)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  2. Google Code Jam 2010 Round 1B Problem B. Picking Up Chicks

    https://code.google.com/codejam/contest/635101/dashboard#s=p1   Problem A flock of chickens are runn ...

  3. HDU 1044 Collect More Jewels(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. 深入理解OpenGL拾取模式(OpenGL Picking)

    深入理解OpenGL拾取模式(OpenGL Picking) 本文转自:http://blog.csdn.net/zhangci226/article/details/4749526 在用OpenGL ...

  5. hdu 1044 Collect More Jewels(bfs+状态压缩)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. codechef Jewels and Stones 题解

    Soma is a fashionable girl. She absolutely loves shiny stones that she can put on as jewellery acces ...

  7. The Material Sourcing Process Failed To Create Picking Suggestions in INVTOTRX (文档 ID 2003806.1)

    In this Document Symptoms Cause Solution References Applies to: Oracle Inventory Management - Versio ...

  8. 实现一个简单的Unity3D三皮卡——3D Picking (1)

    3D Picking 其原理是从摄像机位置到空间发射的射线.基于光线碰到物体回暖. 这里我们使用了触摸屏拿起触摸,鼠标选择相同的原理,仅仅是可选API不同. 从unity3D官网Manual里找到下面 ...

  9. LeetCode --> 771. Jewels and Stones

    Jewels and Stones You're given strings J representing the types of stones that are jewels, and S rep ...

随机推荐

  1. 看云-git类的书籍写作

    看云-git类的书籍写作 https://www.kancloud.cn/explore 测试一本:https://www.kancloud.cn/stono/b001/501901

  2. Android面试过程描写叙述

    1.之前所写项目的介绍 2.android一些常见问题的问答 3.关于android平时非常少用到但实则非常重要的问题描写叙述 技术分析 1自我感觉面试中比較好的方面: 1.熟悉掌握之前所写项目 2. ...

  3. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

  4. Oracle 11g 学习3——表空间操作

    一.表空间概述 表空间是Oracle中最大的逻辑存储结构,与操作系统中的数据文件相相应: 基本表空间:一般指用户使用的永久性表空间,用于存储用户的永久性数据          暂时表空间: 主要用于存 ...

  5. c11---位运算相关

    // // main.c // 03-原码反码补码 #include <stdio.h> int main(int argc, const char * argv[]) { // int占 ...

  6. ES 内存使用和GC指标——主节点每30秒会去检查其他节点的状态,如果任何节点的垃圾回收时间超过30秒(Garbage collection duration),则会导致主节点任务该节点脱离集群。

    摘录自:http://blog.csdn.net/yangwenbo214/article/details/74000458 内存使用和GC指标 在运行Elasticsearch时,内存是您要密切监控 ...

  7. DB-MySQL:MySQL 正则表达式

    ylbtech-DB-MySQL:MySQL 正则表达式 1.返回顶部 1. MySQL 正则表达式 在前面的章节我们已经了解到MySQL可以通过 LIKE ...% 来进行模糊匹配. MySQL 同 ...

  8. 初探MVC路由

    文章目录: 1.认识理解URL,以及简单的路由 2.特性路由.传统路由.区域路由 3.路由生成URL&&绑定到操作&&路由约束 1.认识理解URL,以及简单的路由  默 ...

  9. mysql 导入数据库 命令操作

    window下 1.导出整个数据库 mysqldump -u 用户名 -p 数据库名 > 导出的文件名 mysqldump -u dbuser -p dbname > dbname.sql ...

  10. mobiscroll插件的基本使用方法

    前一阵子接触到了mobiscroll插件,用在移动端的日期选择上,感觉倍棒,于是便敲了一个小案例,与大家一起分享分享 <!DOCTYPE html> <html lang=" ...