题意 : 这个人在左下角,地铁在右上角,由很多格子组成的地图,每一条边都是一条路,每一条边都是100米。还有的可以走对角线,问你从起点到终点最短是多少。
思路 : 其实我想说一下,,,,,这个题基本都是用DP做的,这是为什么呢?好吧,这要从我最近要刷BFS题开始,于是二货自己很厉害的用BFS做完了,所以就给我推荐了,我其实没看出来能用BFS来做。。。。。。。
//URAL 1119
#include <iostream>
#include <stdio.h>
#include <queue>
#include <math.h>
#include <string.h> using namespace std; int n,m ;
bool ch[][] ;
bool vis[][] ;
int dir[][] = {{,},{,}} ;
int k ;
double sum = 0.0 ;
struct node
{
int x ;
int y;
double step ;
} s,e,temp;
queue<node>que ; void BFS()
{
que.push(s) ;
s.step = 0.0 ;
memset(vis,false ,sizeof(vis)) ;
vis[s.x][s.y] = true ;
while(!que.empty())
{
temp = que.front() ;
que.pop() ;
if(temp.x == e.x && temp.y == e.y)
{
sum = temp.step ;
return ;
}
for(int i = ; i < ; i++)
{
int xx = temp.x+dir[i][] ;
int yy = temp.y+dir[i][] ;
if(xx >= && yy >= && xx <= n && yy <= m && !vis[xx][yy])
{
node temp1 ;
temp1.x = xx ;
temp1.y = yy ;
temp1.step = temp.step+100.0 ;
que.push(temp1) ;
vis[xx][yy] = true ;
}
}
if(ch[temp.x+][temp.y+])
{
vis[temp.x+][temp.y+] = true ;
node temp1 ;
temp1.x = temp.x+ ;
temp1.y = temp.y+ ;
temp1.step = temp.step+sqrt(2.0)*100.0 ;
que.push(temp1) ;
}
} }
int main()
{
scanf("%d %d",&n,&m) ;
scanf("%d",&k) ;
int u,v ;
memset(ch,false,sizeof(ch)) ;
for(int i = ; i < k ; i++)
{
scanf("%d %d",&u,&v) ;
ch[u][v] = true ;
}
s.x = , s.y = ;
e.x = n,e.y = m ;
BFS() ;
printf("%.0lf\n",sum) ;
return ;
}