标签:整数 content 自动 思路 ges name nbsp stdio.h ems
思路:将三种走法分别加入队列,用book数组标记每个坐标点的幸运值,如果之后的坐标点的幸运值值大于该点,才将该点加入队列。
不过,这道题正解貌似是dp啊~
#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
using namespace std;
#define N 25
#define M 1100
#define inf 0x3f3f3f3f
int map[N][M],max1,ans,n,m,book[N][M];
struct node{
int x;
int y,w;
};
queue<node>q;
void BFS()
{
struct node head,tail,now;
int x,y,i,j,d;
head.x = 1;
head.y = 1;
head.w = map[1][1];
q.push(head);
while(!q.empty())
{
head = q.front();
// printf("x=%d y=%d w=%d\n",head.x,head.y,head.w );
q.pop() ;
if(head.x == n&&head.y == m)
{
if(head.w > max1)
max1 = head.w ;
}
now.x = head.x + 1;
now.y = head.y ;
now.w = head.w + map[now.x][now.y];
if(now.x >=1 &&now.y >= 1&&now.x <=n&&now.y<=m&&book[now.x][now.y] < now.w)
{
book[now.x][now.y] = now.w;
q.push(now);
}
now.x = head.x;
now.y = head.y +1;
now.w = head.w + map[now.x][now.y];
if(now.x >=1 &&now.y >= 1&&now.x <=n&&now.y<=m&&book[now.x][now.y] < now.w)
{
book[now.x][now.y] = now.w;
q.push(now);
}
now.x = head.x;
for(d = 2; d <= m; d ++)
{
now.y = head.y*d;
if(now.y > m)
break;
now.w = head.w + map[now.x][now.y];
if(now.x >=1 &&now.y >= 1&&now.x <=n&&now.y<=m&&book[now.x][now.y] < now.w)
{
book[now.x][now.y] = now.w;
q.push(now);
}
}
}
return ;
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t --)
{
scanf("%d%d",&n,&m);
memset(book,0,sizeof(book));
for(i = 1; i <= n; i ++)
for(j = 1; j <= m; j ++)
scanf("%d",&map[i][j]);
max1 = -inf;
BFS();
printf("%d\n",max1);
}
return 0;
}
标签:整数 content 自动 思路 ges name nbsp stdio.h ems
原文地址:http://www.cnblogs.com/chengdongni/p/7549913.html