优先队列怎么和不使用优先队列结果一样啊 求助

同学少年 2014-01-17 09:03:48
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1026

我的代码:

#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
char map[102][102];
bool visited[102][102];
int Min;

int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};

struct node
{
int x,y;
int fx,fy;
int step;
bool operator<(const node t)const{
return step>t.step;
}
};

node path[102][102];

node a,b,c;
int N,M;
void getmap()
{
for(int i=0;i<N;++i)
for(int j=0;j<M;++j)
cin>>map[i][j];
}

void print(int n,int m)

{
if(n==0&&m==0)
return;

print(n-path[n][m].fx,m-path[n][m].fy);
if(map[n][m]>='1'&&map[n][m]<='9')
{
int t=map[n][m]-'0';
cout<<path[n][m].step-t<<"s:"<<"("<<n-path[n][m].fx<<","<<m-path[n][m].fy<<")->("<<n<<","<<m<<")"<<endl;

while(t)
{
cout<<path[n][m].step-t+1<<"s:"<<"FIGHT AT ("<<n<<","<<m<<")"<<endl;
t--;
}
}
else
cout<<path[n][m].step<<"s:"<<"("<<n-path[n][m].fx<<","<<m-path[n][m].fy<<")->("<<n<<","<<m<<")"<<endl;

}
int main()
{
while(cin>>N>>M)
{
memset(visited,0,sizeof(visited));
priority_queue <node>q;
getmap();
a.x=0;
a.y=0;
a.fx=0;
a.fy=0;
a.step=0;
visited[0][0]=1;
q.push(a);
path[0][0]=a;
while(!q.empty())
{
b=q.top();
q.pop();
if(b.x==N-1&&b.y==M-1)
{
break;
}
for(int i=0;i<4;++i)
{
c.x=b.x+dx[i];
c.y=b.y+dy[i];
c.fx=dx[i];
c.fy=dy[i];
if(c.x>=0&&c.x<N&&c.y>=0&&c.y<M&&map[c.x][c.y]!='X'&&!visited[c.x][c.y])
{
q.push(c);
path[c.x][c.y]=c;
visited[c.x][c.y]=1;
if(map[c.x][c.y]>='1'&&map[c.x][c.y]<='9')
path[c.x][c.y].step=path[b.x][b.y].step+map[c.x][c.y]-'0'+1;
else
path[c.x][c.y].step=path[b.x][b.y].step+1;

}

}
}

/* for(int i=0;i<N;++i)
{
for(int j=0;j<M;j++)
cout<<step[i][j]<<" ";
cout<<endl;
}//测试*/
if(visited[N-1][M-1])
{
cout<<"It takes "<<path[N-1][M-1].step<<" seconds to reach the target position, let me show you the way."<<endl;
print(N-1,M-1);
}
else
cout<<"God please help our poor hero."<<endl;
cout<<"FINISH"<<endl;
}
return 0;
}



5 7
...X...
.X.X.X.
.X...X.
.XXXXX.
....9..
这组数据不能通过 应该是14
...全文
79 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
孩皮妞野 2014-01-17
  • 打赏
  • 举报
回复
直觉可以用A*, 不过好的估价函数难得。而且状态空间是网络,要避免重复状态。

64,650

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧