SDUT 1304-取數字問題(DFS),sdut1304-

來源:互聯網
上載者:User

SDUT 1304-取數字問題(DFS),sdut1304-

取數字問題Time Limit: 1000ms   Memory limit: 65536K  有疑問?點這裡^_^題目描述給定M×N的矩陣,其中的每個元素都是-10到10之間的整數。你的任務是從左上方(1,1)走到右下角(M,N),每一步只能夠向右或者向下,並且不能夠走出矩陣的範圍。你所經過的方格裡面的數字都必須被選取,請找出一條最合適的道路,使得在路上被選取的數字之和是儘可能小的正整數。輸入輸入第1行是兩個整數M和N,(2<=M<=10,2<=N<=10),分別表示矩陣的行和列的數目。接下來M行,每行包括N個整數,就是矩陣中的每一行的N個元素。輸出輸出只有一行,就是一個整數,表示所選道路上數字之和所能達到的最小正整數。如果不能達到任何正整數,輸出-1。樣本輸入
2 20 21 0
樣本輸出
1
簡單暴搜。。
資料太弱。。幾乎不用剪枝。。
#include <iostream>#include <cstdio>#include <algorithm>#include <set>#include <cctype>#include <cstring>#include <string>#include <vector>#include <stack>#include <queue>using namespace std;#define ll long longconst int maxn = 11;const int INF = 0x3f3f3f3f;int ans, m, n, ma[11][11];void dfs(int x, int y, int sum){if (sum > ans) {return ;}if (x == n && y == m) {if (sum > 0 && ans > sum) {ans = sum;}return ;}if (x < n) {dfs(x + 1, y, sum + ma[x + 1][y]);}if (y < m) {dfs(x, y + 1, sum + ma[x][y + 1]);}}int main(){scanf("%d%d", &n, &m);for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++) {scanf("%d", &ma[i][j]);}ans = INF;dfs(1, 1, ma[1][1]);if (ans != INF) {printf("%d\n", ans);} else {puts("-1");}return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.