Sicily 1158. Pick numbers
来源:互联网 发布:西北大学网络教育登陆 编辑:程序博客网 时间:2024/05/17 08:12
Description
Given a matrix of size M*N, the elements of which are integer numbers from -10 to 10. You task is to go from the top-left corner (1, 1) to the bottom-right corner (M, N). You can only move right or down, and you can not go out of the matrix. The number in the grid you passed must be picked. The sum of numbers you picked must be positive and as minimal as possible.
Input
The input contains several test cases.
The first line of each test case are two integer numbers M, N (2<=M<=10, 2<=N<=10), indicating the number of rows and columns of the matrix. The following M lines, each contains N integer numbers.
Output
For each test case, output the sum of numbers you picked on a single line. If you can't get a positive sum, output -1.
Sample Input
2 20 21 03 30 0 00 0 00 0 0
Sample Output
1-1
// Problem#: 1158// Submission#: 2216541// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// All Copyright reserved by Informatic Lab of Sun Yat-sen University#include <stdio.h>#include <string.h>int ma[10][10];int result[4002];int s[100000][3];int n;int m;void go(int x, int y){ int sum = ma[x][y]; int pointer = 0; s[0][0] = x; s[0][1] = y; s[0][2] = sum; int counter = 1; while(counter != 0) { //printf("%d %d %d %d\n", s[pointer][0], s[pointer][1], counter, pointer); if(s[pointer][0] == m - 1 && s[pointer][1] == n - 1) { //printf("%d\n", s[pointer][2] + 1000); result[s[pointer][2] + 2000] = 1; counter--; pointer++; } else { if(s[pointer][0] + 1 < m) { s[pointer + counter][0] = s[pointer][0] + 1, s[pointer + counter][1] = s[pointer][1], s[pointer + counter][2] = s[pointer][2] + ma[s[pointer][0] + 1][s[pointer][1]]; counter++; } if(s[pointer][1] + 1 < n) { s[pointer + counter][0] = s[pointer][0], s[pointer + counter][1] = s[pointer][1] + 1, s[pointer + counter][2] = s[pointer][2] + ma[s[pointer][0]][s[pointer][1] + 1]; counter++; } pointer++; counter--; } pointer = pointer % 100000; } }int main(){ int i; int j; int x; int y; int flag; while(scanf("%d", &m) != EOF) { scanf("%d", &n); memset(result, 0, sizeof(result)); for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { scanf("%d", &ma[i][j]); } } x = 0; y = 0; go(x, y); //printf("ok\n"); flag = 0; for(i = 2001; i < 4002; i++) { if(result[i] == 1) { printf("%d\n", i - 2000); flag = 1; break; } } if(flag == 0) { printf("-1\n"); } } return 0; }
- Sicily 1158. Pick numbers
- sicily 1158.Pick numbers
- Sicily 1158 Pick numbers
- Sicily 1158 Pick numbers
- 1158. Pick numbers
- 1158. Pick numbers
- 1158. Pick numbers(DFS)
- Pick numbers
- Sicily 11600. Pick It
- 1158 pick numbers
- Sicily 1793. Numbers
- Sicily 1050. Numbers & Letters
- Sicily 1793. Numbers
- Sicily 1050. Numbers & Letters
- Sicily 1050 Numbers & Letters
- sicily 1050 Numbers & Letters
- Sicily 1050. Numbers & Letters
- Sicily 1050. Numbers & Letters
- android位置属性
- android listview 属性
- 数据程序 删除程序
- KMP字符串模式匹配详解
- Android ListView控件基本用法以及范例
- Sicily 1158. Pick numbers
- Android开发学习笔记:浅谈GridView
- 关于Sql Server数据库连接池的使用
- 最小的k个数
- 首付打算地方
- android:EditText属性/去边框
- 网络通信—udp使用领悟
- 黑马程序员_常用API
- java模式---责任链模式之Filter链条