JOJ 2558 / HDU 2757 (Ocean Currents) 解题纠错

以下来自点击打开链接

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
int m,n;
int map[2010][2010];
int visited[2010][2010];
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}
};
int bfs(Node n1,Node n2)
{
n1.step=0;
visited[n1.x][n1.y]=1;
queue<Node>q;
//while(!q.empty())q.pop();
q.push(n1);
while(!q.empty())
{
Node temp,temp1;
temp=q.front();
q.pop();
//cout<<"temp "<<temp.x<<" "<<temp.y<<" "<<temp.step<<" "<<endl;
if(temp.x==n2.x&&temp.y==n2.y)
{
return temp.step;
}
int x=temp.x;
int y=temp.y;
int step=temp.step;
while(1)
{
int x1=x+move[map[x][y]][0];
int y1=y+move[map[x][y]][1];
if(x1>=1&&x1<=m&&y1>=1&&y1<=n&&!visited[x1][y1])
{
temp1.x=x1;
temp1.y=y1;
temp1.step=step;
if(temp1.x==n2.x && temp1.y==n2.y)
return temp1.step;
q.push(temp1);
x=x1;y=y1;
// cout<<"x="<<x<<"y="<<y<<endl;
}
else break;
}
x=temp.x;
y=temp.y;
step=temp.step;
for(int i=0;i<8;i++)
{
int x1=x+move[i][0];
int y1=y+move[i][1];
if(x1>=1 && x1<=m && y1>=1 && y1<=n && !visited[x1][y1])
{
temp.x=x1;temp.y=y1;temp.step=step+1;
if(temp.x==n2.x&&temp.y==n2.y)
return temp.step;
q.push(temp);
visited[x1][y1]=1;
while(1)
{
int k1=x1+move[map[x1][y1]][0];
int k2=y1+move[map[x1][y1]][1];
if(k1>=1 && k1<=m && k2>=1 && k2<=n && !visited[k1][k2])
{
temp.x=k1;temp.y=k2;temp.step=step+1;
if(temp.x==n2.x&&temp.y==n2.y)
return temp.step;
visited[k1][k2]=1;
q.push(temp);
x1=temp.x;y1=temp.y;
}
else break;
}
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
char str[1050];
while(scanf("%d%d",&m,&n)==2)
{
getchar();
for(int i=0;i<m;i++)
{
gets(str);
//puts(str);
for(int j=0;j<n;j++)
{
map[i+1][j+1]=str[j]-'0';
//printf("%d ",map[i+1][j+1]);
}
//cout<<endl;
}
int num;
scanf("%d",&num);
Node n1,n2;
for(int i=0;i<num;i++)
{
memset(visited,0,sizeof(visited));
scanf("%d%d%d%d",&n1.x,&n1.y,&n2.x,&n2.y);
//printf("%d%d%d%d\n",n1.x,n1.y,n2.x,n2.y);
int ss=bfs(n1,n2);
printf("%d\n",ss);
}
printf("\n");
}
return 0;


问题:

(1)漏了对已用点置标志的语句visited[x1][y1]=1;

(2)在试探8个方向时没有排除已用方向,实际只要试探7个方向。

JOJ与HDU两题的区别是前者要在每组输出后再输出一个换行。

加4行在HDU上AC:

#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
using namespace std;
int m,n;
int map[2010][2010];
int visited[2010][2010];
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}
};
int bfs(Node n1,Node n2)
{
n1.step=0;
visited[n1.x][n1.y]=1;
queue<Node>q;
//while(!q.empty())q.pop();
q.push(n1);
while(!q.empty())
{
Node temp,temp1;
temp=q.front();
q.pop();
//cout<<"temp "<<temp.x<<" "<<temp.y<<" "<<temp.step<<" "<<endl;
if(temp.x==n2.x&&temp.y==n2.y)
{
return temp.step;
}
int x=temp.x;
int y=temp.y;
int step=temp.step;
int k; //加
while(1)
{
  k = map[x][y]; //加
int x1=x+move[map[x][y]][0];
int y1=y+move[map[x][y]][1];
if(x1>=1&&x1<=m&&y1>=1&&y1<=n&&!visited[x1][y1])
{
  visited[x1][y1]=1; //加
temp1.x=x1;
temp1.y=y1;
temp1.step=step;
if(temp1.x==n2.x && temp1.y==n2.y)
return temp1.step;
q.push(temp1);
x=x1;y=y1;
// cout<<"x="<<x<<"y="<<y<<endl;
}
else break;
}
x=temp.x;
y=temp.y;
step=temp.step;
for(int i=0;i<8;i++)
{
  if (i==k) continue; //加
int x1=x+move[i][0];
int y1=y+move[i][1];
if(x1>=1 && x1<=m && y1>=1 && y1<=n && !visited[x1][y1])
{
temp.x=x1;temp.y=y1;temp.step=step+1;
if(temp.x==n2.x&&temp.y==n2.y)
return temp.step;
q.push(temp);
visited[x1][y1]=1;
while(1)
{
int k1=x1+move[map[x1][y1]][0];
int k2=y1+move[map[x1][y1]][1];
if(k1>=1 && k1<=m && k2>=1 && k2<=n && !visited[k1][k2])
{
temp.x=k1;temp.y=k2;temp.step=step+1;
if(temp.x==n2.x&&temp.y==n2.y)
return temp.step;
visited[k1][k2]=1;
q.push(temp);
x1=temp.x;y1=temp.y;
}
else break;
}
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
char str[1050];
while(scanf("%d%d",&m,&n)==2)
{
getchar();
for(int i=0;i<m;i++)
{
gets(str);
//puts(str);
for(int j=0;j<n;j++)
{
map[i+1][j+1]=str[j]-'0';
//printf("%d ",map[i+1][j+1]);
}
//cout<<endl;
}
int num;
scanf("%d",&num);
Node n1,n2;
for(i=0;i<num;i++)
{
memset(visited,0,sizeof(visited));
scanf("%d%d%d%d",&n1.x,&n1.y,&n2.x,&n2.y);
//printf("%d%d%d%d\n",n1.x,n1.y,n2.x,n2.y);
int ss=bfs(n1,n2);
printf("%d\n",ss);
}
//HDU不需要此行 printf("\n");
}
return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值