Hansel and Gretel
来源:互联网 发布:移动测速软件 编辑:程序博客网 时间:2024/06/05 10:35
Long time ago, there lived Hansel and Gretel. And a strange witch took Hansel to the deep forest to get rid of him. Smart Hansel abandoned scraps of bread that he’d got little by little on the way to the forest. But the problem is that the scraps of bread scattered here and there by the similar scraps by other mountaineers is making hard for Gretel to follow. At this time, a fairy of the forest appears to tell her how to find the way. ‘Move to where you can find as many scraps of bread as you can.’
Following to her advice, Gretel plans to use the route where she can find as many scraps of bread as she can from the departure to the destination. There is one condition that she is allowed to move up or left only along the mountain to the destination.
Create a program that she can find the way as the fairy advised.
Time limit: 1 second (java: 2 seconds)
Input format
Several test cases can be included in the inputs. T, the number of cases is given in the first row of the inputs. After that, the test cases as many as T (T ≤ 30) are given in a row.
N, the size of the forest is given in the first row of each test case. The forest is always the square shape of N×N size. The information of the forest is given by being separated as spaces over the lines of the number of N from the second row. In case a scrap of bread is in the corresponding cell, it is 1, and in case a scrap of bread is not in the corresponding cell, it is 0.
Output format
Output the number of bread when they take bread as much as they can from the departure on the first row of each test case to the destination.
Example of Input
2
5
0 1 0 0 1
0 0 1 0 0
1 0 1 1 0
1 1 0 1 0
1 0 0 0 1
10
1 0 0 1 0 1 0 0 1 0
1 1 1 0 1 1 1 1 0 0
0 0 0 1 1 0 1 0 0 0
0 1 1 1 0 0 0 0 1 0
1 0 1 1 0 0 1 0 1 0
0 0 1 1 1 0 1 0 1 0
1 1 0 0 0 0 1 0 1 0
0 1 0 1 1 1 0 0 0 0
1 0 1 1 0 1 0 0 1 0
0 0 0 1 1 0 1 0 1 0
Example of Output
6
14
Hint
Map[i][j] = max{ Map[i][j-1]+Map[i][j], Map[i-1][j]+Map[i][j] }
Do calculation when scanf.
#include <stdio.h>#define MAX(a,b) (a>b?a:b)int data[1001][1001];int F[1001][1001];int main(void){int tc, T;int N,i,j;// freopen("input.txt", "r", stdin);setbuf(stdout, NULL);scanf("%d", &T);for(tc = 0; tc < T; tc++){scanf("%d", &N);for(i=0;i<N;i++){for(j=0;j<N;j++){F[i][j] = 0;scanf("%d", &data[i][j]);}}if(data[0][0])F[0][0] = 1;for(i=1;i<N;i++){F[i][0] = F[i-1][0]+data[i][0];F[0][i] = F[0][i-1]+data[0][i];}for(i=1;i<N;i++)for(j=1;j<N;j++){F[i][j] = MAX(F[i-1][j],F[i][j-1])+data[i][j];}printf("%d\n",F[N-1][N-1]);/*********************************** Implement your algorithm here. ************************************/// Print the answer to standard output(screen).}return 0;}
- Hansel and Gretel
- 格动作韩赛尔与格蕾特:女巫猎人(Hansel and Gretel:Witch Hunters)
- 1070[Hansel and Grethel]
- 1254 Hansel and Grethel
- [sicily online]1070. Hansel and Grethel
- POJ 1254 Hansel and Grethel 笔记
- zoj 1560 Hansel and Grethel(求两条直线的交点)
- poj 1254 Hansel and Grethel 直线求交
- " and '
- $* and $@
- AND
- AND
- & and &&
- ""and ‘’
- GetMessage () and PeekMessage () and SendMessage () and PostMessage ()
- eval and $()/``and typeset and xargs
- # and #line and __FILE__ and __LINE__
- 这样的东东怎么翻译:and and and And?
- python笔记(四)
- 走进小作坊(十一)----移动web实现指南
- iOS 视频技术点【经典】
- 在JAVA中如何跳出当前的多重嵌套循环?
- Android上传文件至服务器
- Hansel and Gretel
- 学习笔记_毕向东 Java_内部类_异常_多线程 2014.7.15
- 在HADOOP中使用MRUNIT进行单元测试
- 关于C和C++分别读写TXT数据文件的源代码
- 重新排列链表Reorder List
- DIV垂直集中
- STC89C52RC内部EEPROM的读写
- Spring Timer 两种实现
- 【php入门】AppServ的安装