标签:
1
2
3
4
5
6
7
|
5 6
0 0 0 0 1 0
0 1 0 1 0 0
0 0 0 1 0 1
0 0 1 0 0 1
1 0 0 0 1 0
|
1
2
|
5
|
1 #include <stdio.h> 2 int rows, dp[1001][1001]; 3 int main() 4 { 5 int i, j, n, m; 6 scanf("%d%d", &n, &m); 7 for (i = 0; i < n; i++) 8 for (j = 0; j < m; j++) 9 scanf("%d", &dp[i][j]); 10 for (i = n - 1; i >= 0; i--) 11 for (j = m - 1; j >= 0; j--) 12 dp[i][j] += dp[i + 1][j] > dp[i][j + 1] ? dp[i + 1][j] : dp[i][j + 1]; 13 printf("%d\r\n", dp[0][0]); 14 return 0; 15 }
其实最开始并没有想到dp(还是题做的少,没这个概念),直接两个方位的bfs+优先队列
感觉应该是对的,为啥就是wa 呢?贴出代码,求大神指教
1 #include <iostream> 2 #include <queue> 3 #include <algorithm> 4 using namespace std; 5 int map[1001][1001], vis[1001][1001], dir[][2] = { 1, 0, 0, 1 }; 6 int n, m; 7 struct node{ 8 int x, y, cur; 9 friend bool operator<(node x, node y){ 10 return x.cur < y.cur; 11 } 12 }; 13 int bfs(){ 14 priority_queue<node>Q; 15 struct node now, next; 16 now.x = now.y = 1, now.cur = map[1][1]; 17 vis[1][1] = 1; 18 Q.push(now); 19 while (!Q.empty()){ 20 now = Q.top(); 21 Q.pop(); 22 if (now.x == n&&now.y == m) 23 return now.cur; 24 for (int i = 0; i < 2; i++){ 25 next.x = now.x + dir[i][0]; 26 next.y = now.y + dir[i][1]; 27 if (next.x >= 1 && next.x <= n && next.y >= 1 && next.y <= m &&!vis[next.x][next.y]){ 28 next.cur = now.cur + map[next.x][next.y]; 29 vis[next.x][next.y] = 1; 30 Q.push(next); 31 } 32 } 33 } 34 } 35 int main(){ 36 cin >> n >> m; 37 for (int i = 1; i <= n; i++) 38 for (int j = 1; j <= m; j++) 39 cin >> map[i][j]; 40 cout << bfs() << "\r\n"; 41 return 0; 42 }
swust oj--Coin-collecting by robot
标签:
原文地址:http://www.cnblogs.com/zYx-ac/p/4541030.html