(hdu2612)
来源:互联网 发布:郑州云端网络科技学院 编辑:程序博客网 时间:2024/04/29 03:28
郁闷死:
很容易理解的题,第一次果断超时;
借第一次的经验;在搜索到两个人的过程也把到他们之间的距离存了起来;
结果错误,和学哥找了一晚上,嚓,原来错着了,乖乖,字符串还没输出完我就开始查找了。。
不过经过这次找错,错一点,什么不可思议的结果都会出来。。
#include"stdio.h"
#include"string.h"
#include"queue"
#define inf 99999
char map[300][300];
int visit[300][300];
int dis[300][300][2];
using namespace std;
int n,m,flag;
int dir[4][2]={1,0, -1,0, 0,1, 0,-1};
struct point
{
int x,y;
int step;
};
int judge(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m)
return 1;
return 0;
}
void bfs(int x,int y)
{
int i;
memset(visit,0,sizeof(visit));
queue<point>q;
point cur,next;
cur.x=x;
cur.y=y;
cur.step=0;
visit[x][y]=1;
q.push(cur);
while(!q.empty())
{
next=q.front();
q.pop();
for(i=0;i<4;i++)
{
cur.x=next.x+dir[i][0];
cur.y=next.y+dir[i][1];
cur.step=next.step+1;
if(judge(cur.x,cur.y))
{
if(map[cur.x][cur.y]!='#'&&visit[cur.x][cur.y]==0)//注意不能是map【】【】==''.'
{
visit[cur.x][cur.y]=1;
if(map[cur.x][cur.y]=='@')
dis[cur.x][cur.y][flag]=cur.step;
q.push(cur);
}
}
}
}
}
int main()
{
int i,j,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
k=inf;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
dis[i][j][0]=inf;
dis[i][j][1]=inf;
}
for(i=0;i<n;i++)
scanf("%s",map[i]);
for(i=0;i<n;i++)//这里输完后再搜索。。
for(j=0;j<m;j++)
{
if(map[i][j]=='Y')
{
flag=0;
bfs(i,j);
}
if(map[i][j]=='M')
{
flag=1;
bfs(i,j);
}
}
for(i=0;i<n;i++)
for(j=0;j<m;j++)
if(map[i][j]=='@'&&k>dis[i][j][0]+dis[i][j][1])
{
k=dis[i][j][0]+dis[i][j][1];
}
printf("%d\n",11*k);
}
return 0;
}
超时代码:留念一下:
#include"stdio.h"
#include"string.h"
#include"queue"
char map[3000][1000];
int visit[3000][1000];
using namespace std;
int a[3000],b[3000];
#define inf 0x7fffffff
int n,m,x1,y1,x2,y2,ex,ey;
int dir[4][2]={1,0, -1,0, 0,1, 0,-1};
struct point
{
int x,y;
int step;
};
int judge(int x,int y)
{
if(x>=0&&x<n&&y>=0&&y<m)
return 1;
return 0;
}
int bfs(int x,int y)
{
int i;
memset(visit,0,sizeof(visit));
queue<point>q;
point cur,next;
cur.x=x;
cur.y=y;
cur.step=0;
visit[x][y]=1;
q.push(cur);
while(!q.empty())
{
next=q.front();
q.pop();
if(next.x>n&&next.y>m)
return 0;
for(i=0;i<4;i++)
{
cur.x=next.x+dir[i][0];
cur.y=next.y+dir[i][1];
if(judge(cur.x,cur.y))
{
if(map[cur.x][cur.y]!='#'&&visit[cur.x][cur.y]==0)
{
cur.step=next.step+1;
q.push(cur);
if(cur.x==ex&&cur.y==ey)
return cur.step;
visit[cur.x][cur.y]=1;
}
}
}
}
return 0;
}
int main()
{
int i,j,k,h,r;
while(scanf("%d%d",&n,&m)!=EOF)
{
k=inf; r=0;
for(i=0;i<n;i++)
{
scanf("%s",map[i]);
for(j=0;j<m;j++)
{
if(map[i][j]=='Y')
{
x1=i;
y1=j;
}
if(map[i][j]=='M')
{
x2=i;
y2=j;
}
if(map[i][j]=='@')
{
a[r]=i;
b[r]=j;
r++;
}
}
}
for(i=0;i<r;i++)
{
ex=a[i];
ey=b[i];
h=bfs(x1,y1)+bfs(x2,y2);
if(h<k)
k=h;
}
printf("%d\n",11*k);
}
return 0;
}
- hdu2612
- hdu2612
- hdu2612
- (hdu2612)
- hdu2612
- hdu2612 bfs
- Find a way hdu2612
- hdu2612简单BFS
- HDU2612---Find a way
- HDU2612---(两次BFS)
- hdu2612---Find a way
- hdu2612 两遍BFS
- HDU2612,简单广搜题
- HDU2612 Find a way
- Hdu2612 Find a way
- HDU2612 Find a way
- HDU2612-Find a way
- HDU2612 find a way
- 十进制转2进制模板
- 各种各样的api查询
- PCL点云文件生成与读取
- dijkstra,bellman-ford,floyd分析比较
- struts2配置出现404错误解决
- (hdu2612)
- hdu1503(DP)
- UVa:615 Is It A Tree?
- 虚拟机启动报错Unable to open kernel device "\\.\VMCIDev\VMX"
- Linux LCD驅動架構學習(二)
- zoj 1284||poj 1528||hdu 1323(筛选法求因子和)
- Dijkstra算法模板
- 深度缓冲区原理以及为什么要用它
- BNU The Twin Towers