杭电1026
来源:互联网 发布:windows version 1703 编辑:程序博客网 时间:2024/05/17 21:39
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1026
提交了几遍,用DFS超时,又该用BFS,但是内存又超。
交了几遍还是不知怎么解决,后来才知道,需要一个vis数组来表示已经入队的点,解决重复入队的问题。
代码如下:
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define Max 0xfffffff
#include<queue>
struct node{
int x,y;
}o;
using namespace std;
int map[110][110],dis[110][110],vis[110][110];
int N,M;
node front[110][110];
int m_x[]={-1,0,1,0},m_y[]={0,1,0,-1};
void bfs(){
for(int i=0;i<110;i++)
for(int j=0;j<110;j++)
dis[i][j]=Max;
dis[0][0]=0;
queue<node> q;
o.x=0;o.y=0;
q.push(o);
vis[0][0]=1;
while(!q.empty())
{
node tmp;
tmp=q.front();
q.pop();
vis[tmp.x][tmp.y]=0;
for(int i=0;i<4;i++)
{
int tx=tmp.x+m_x[i],ty=tmp.y+m_y[i];
if(tx>=0 && tx<N && ty>=0 && ty<M)
if(map[tx][ty]!=-1 && (dis[tmp.x][tmp.y]+map[tx][ty])<dis[tx][ty])
{
dis[tx][ty]=dis[tmp.x][tmp.y]+map[tx][ty]+1;//cout<<tx<<" "<<ty<<" "<<dis[tx][ty]<<endl;
front[tx][ty].x=tmp.x;
front[tx][ty].y=tmp.y;
o.x=tx;o.y=ty;
if(!vis[tx][ty]){
q.push(o);
vis[tx][ty]=1;
}
}
}
}
}
void disp(int n,int m,int t){
if(front[n][m].x+front[n][m].y==0)
{
printf("%ds:(%d,%d)->(%d,%d)\n",1,front[n][m].x,front[n][m].y,n,m);
for(int i=0;i<map[n][m];i++)
{
printf("%ds:FIGHT AT (%d,%d)\n",i+2,n,m);
}
return;
}
else
{
disp(front[n][m].x,front[n][m].y,t-map[n][m]-1);
printf("%ds:(%d,%d)->(%d,%d)\n",t-map[n][m],front[n][m].x,front[n][m].y,n,m);
for(int i=0;i<map[n][m];i++)
printf("%ds:FIGHT AT (%d,%d)\n",t+i+1-map[n][m],n,m);
}
}
int main(){
while(cin>>N)
{
cin>>M;
char ch;getchar();
memset(vis,0,sizeof(vis));
for(int i=0;i<N;i++){
for(int j=0;j<M;j++)
{
cin>>ch;
if(ch=='X') map[i][j]=-1;
else if(ch=='.') map[i][j]=0;
else map[i][j]=ch-'0';
}
getchar();
}
bfs();
if(dis[N-1][M-1]==Max) printf("God please help our poor hero.\n");
else{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",dis[N-1][M-1]);
disp(N-1,M-1,dis[N-1][M-1]);
}
cout<<"FINISH\n";
}
return 0;
}
- 杭电1026
- 杭电 1026
- 杭电1026
- 杭电
- 杭电
- 杭电
- 杭电 1234 和 杭电 2115
- 杭电2056之Rectangles 杭电
- 杭电ACM1061Rightmost Digit
- 杭电2099 7.11
- 杭电ACM 1003
- 杭电 ACM 2016
- 杭电ACM1466
- 杭电ACM1003
- 杭电ACM1225
- 杭电ACM2023
- 杭电ACM2602
- 杭电ACM2955
- TCP服务端和客户端(8)
- 允许跨域访问的解决方案--核心代码
- poj1321--棋盘问题(搜索练习2,变形的八皇后问题)
- HttpURLConnection用法详解
- 重新认识Java中的程序入口即主函数各组成部分
- 杭电1026
- 《Fast Traking via Spatio-Temporal Context Learning》要点整理与代码实现之二
- 1、JavaScript中的单引号、双引号的测试
- nyoj 860 又见01背包 【01-背包变形】
- android蓝牙开发---与蓝牙模块进行通信
- Snowflake Snow Snowflakes寻找相似雪花
- 二叉树的非递归遍历
- poj2251--Dungeon Master(搜索练习3-三维bfs)
- 设计模式--观察者模式