joj2558

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE

8s 65536K 395 55 Standard


For a boat on a large body of water, strong currents can be dangerous, but with careful planning, they can be harnessed to help the boat reach its destination. Your job is to help in that planning.


At each location, the current flows in some direction. The captain can choose to either go with the flow of the current, using no energy, or to move one square in any other direction, at the cost of one energy unit. The boat always moves in one of the following eight directions: north, south, east, west, northeast, northwest, southeast, southwest. The boat cannot leave the boundary of the lake. You are to help him devise a strategy to reach the destination with the minimum energy consumption.


Input Specification


The input contains several test cases. Each case starts with two integers r and c(The lake is represented as a rectangular grid), the number of rows and columns in the grid. The grid has no more than 1000 rows and no more than 1000 columns. Each of the following r lines contains exactly c characters, each a digit from 0 to 7 inclusive. The character 0 means the current flows north (i.e. up in the grid, in the direction of decreasing row number), 1 means it flows northeast, 2 means east (i.e. in the direction of increasing column number), 3 means southeast, and so on in a clockwise manner:
7 0 1
 \|/
6-*-2
 /|\
5 4 3
The line after the grid contains a single integer n, the number of trips to be made, which is at most 50. Each of the following n lines describes a trip using four integers rs, cs, rd, cd, giving the row and column of the starting point and the destination of the trip. Rows and columns are numbered starting with 1.
Sample Input


5 5
04125
03355
64734
72377
02062
3
4 2 4 2
4 5 1 4
5 3 3 4
Output Specification


For each trip in a test case, output a line containing a single integer, the minimum number of energy units needed to get from the starting point to the destination. The output of each test case is ended with an empty line.
Output for Sample Input


0
2
1


Problem Source: Waterloo


This problem is used for contest: 129  149 
Submit / Problem List / Status / Discuss




Problem Set with Online Judge System Version 3.12
Jilin University


Developed by skywind, SIYEE





joj2558是至今为止让我最蛋疼的一个题,我做的3个晚上才把他搞定但是之后心情暴爽。我感觉ACM这点是最吸引我的。。在做这个题的途中我也明白了多两个变量

难度增加一倍。好了不扯淡了。说正经的了。

这是一个宽度优先搜索的问题,一开始我还认为是用优先队列做。做了一个晚上没有做出来后来意识到队列与bfs的组合的实质才想出来的。之所以用队列与bfs结合我

现在真正的理解了是通过队列将所有元素按照权重依次排成一列。所以这道题用优先队列是不行的。因为当一个点入队之后还可能跟他有相同权重的还要进去。然而

优先队列不能保证这点能够实现。。因为优先队列在一个点入队之后得向上下左右四个方向扩展然而这四个方向只有一个是跟这个点有相同权重的其他的都要比这个点大。



#include<cstdio>

#include<cstring>
#include<queue>
using namespace std;
int m,n;
int map[1020][1020];
int visited[1020][1020];
class Node
{
public:
int x,y;
int step;
};
int move[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
void  bfs(Node node1,Node node2)
{
queue<Node>q;
while(!q.empty())q.pop();
node1.step=0;
visited[node1.x][node1.y]=1;
q.push(node1);
while(!q.empty())
{
Node temp;
Node temp1;
temp=q.front();
q.pop();//printf("%d  %d   %d \n",temp.x,temp.y,temp.step);
if(temp.x==node2.x&&temp.y==node2.y)
{
printf("%d\n",temp.step);return ;
}
int xx=temp.x;
int yy=temp.y;
int step=temp.step;
while(1)
{
int x=xx+move[map[xx][yy]][0];
int y=yy+move[map[xx][yy]][1];
if(x>0 &&x<=m&&y>0&&y<=n
&&!visited[x][y])
{
visited[x][y]=1;
temp1.x=x;temp1.y=y;temp1.step=step;
q.push(temp1);
if(x==node2.x&&y==node2.y)
{
printf("%d\n",step);
return ;
}
xx=x;yy=y;
}
else break;
}
for(int i=0;i<8;i++)
{
int x=temp.x+move[i][0];
int y=temp.y+move[i][1];
if(x>0 &&x<=m&&y>0&&y<=n
&&!visited[x][y])
{
visited[x][y]=1;
temp1.x=x;
temp1.y=y;
temp1.step=temp.step+1;
q.push(temp1);
if(x==node2.x&&y==node2.y)
{
printf("%d\n",temp1.step);return ;
}
while(1)
{
xx=x+move[map[x][y]][0];
yy=y+move[map[x][y]][1];
if(xx>0&&xx<=m&&yy>0&&yy<=n&&!visited[xx][yy])
{
visited[xx][yy]=1;
temp1.x=xx;
temp1.y=yy;
temp1.step=temp.step+1;
q.push(temp1);
x=xx;y=yy;
if(xx==node2.x&&yy==node2.y)
{
printf("%d\n",temp1.step);return ;
}
}
else break;
}
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
char str[1020];
while(scanf("%d%d",&m,&n)==2)
{
getchar();
for(int i=1;i<=m;i++)
{
gets(str);
for(int j=1;j<=n;j++)
{
map[i][j]=str[j-1]-'0';
}
}
int num;
scanf("%d",&num);
Node node1,node2;
for(int i=0;i<num;i++)
{
memset(visited,0,sizeof(visited));
scanf("%d%d%d%d",&node1.x,&node1.y,&node2.x,&node2.y);
bfs(node1,node2);
}
printf("\n");
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值